【发布时间】:2017-04-16 20:48:34
【问题描述】:
我是 android 的初学者。我正在尝试用片段中的图像制作列表视图。但它没有完成。 这是我的代码
ListoneFragment.java:(这里 name1 和 name2 是我的 strings.xml 文件中的字符串数组名称)
package fragments.h.safmical;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import safmical.h.R;
/**
* Created by hadvani on 4/14/2017.
*/
public class ListOneFragment extends Fragment {
String[] titles;
String[] shortforms;
int[] images={R.drawable.hcll,R.drawable.hno,R.drawable.hcll,R.drawable.hno,R.drawable.hcll,R.drawable.hno};
ListView listView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_listone, container, false);
Resources res =getResources();
titles=res.getStringArray(R.array.name1);
shortforms=res.getStringArray(R.array.name2);
listView = (ListView) rootview.findViewById(R.id.list1);
MyAdapter adapter=new MyAdapter(this,titles,images,shortforms);
listView.setAdapter(adapter);
return rootview;
}
}
class MyAdapter extends ArrayAdapter<String>{
Context context;
int[] images;
String[] titleArray;
String[] shortformArray;
MyAdapter(Context c,String[] titles,int imgs[],String[] shortform){
super(c,R.layout.fragment_listpattern,R.id.textView1,titles);
this.context=c;
this.images=imgs;
this.titleArray=titles;
this.shortformArray=shortform;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.fragment_listpattern,parent,false);
ImageView myImage=(ImageView)row.findViewById(R.id.imageView2);
TextView myTitle=(TextView)row.findViewById(R.id.textView1);
TextView myShortform=(TextView) row.findViewById(R.id.textView2);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
myShortform.setText(shortformArray[position]);
return row;
}
}
这里是我的 xml 文件
fragment_listone.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list1">
</ListView>
</FrameLayout>
fragment_listpattern.xml:(用于列表视图的单行模式)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="72dp">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toEndOf="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:text="TextView" />
</RelativeLayout>
</FrameLayout>
当我运行它时。它显示错误:
Error:(37, 40) error: incompatible types: ListOneFragment cannot be converted to Context
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
它在 ListoneFragment.java 行
MyAdapter adapter=new MyAdapter(this,titles,images,shortforms); 中显示建议
那
将方法 'MyAdapter' 的第一个参数从 Context 更改为 'ListoneFragment'。
当我这样做时,它会在 MyAdapter 类的构造函数中出错。 我该怎么办,有什么更正或者有其他方法可以实现吗?请帮忙?
【问题讨论】:
标签: android listview android-fragments constructor