赋料扬雄敌,诗看子建亲。这篇文章主要讲述适用于Android ListView的适配器类getView()函数相关的知识,希望能为你提供帮助。
我正在尝试创建一个ListView,其中包含一个项目列表,每个项目都有三个组件。
- 标题(季节性)
- 描述
- 图像(SezonThumb)
<
?xml version="1.0" encoding="utf-8"?>
<
ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.SeasonsActivity">
<
LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<
ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imgThumbnail"
android:clickable="true"
/>
<
TextView
style="@style/style_textviewsProfile"
android:layout_margin="0dp"
android:textStyle="bold"
android:id="@+id/textViewSeasonNo"
android:text="Season No:" />
<
TextView
android:id="@+id/textViewDescription"
style="@style/style_textviewsProfile"
android:text="Description:" />
<
View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/Gray">
<
/View>
<
/LinearLayout>
<
/ScrollView>
以下是活动的代码:
public void onCreate(){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seasons);
listView_seasons=findViewById(R.id.list_seasons);
txtTitle = findViewById(R.id.txtTitle);
String[] seasonNos=new String[]{"1","2","3","4","5"};
String[] seasonDescriptions=new String[]{"a","b","c","d","e"};
int[] seasonThumbs= new int[]{R.drawable.sezon1, R.drawable.sezon2, R.drawable.flag, R.drawable.flag, R.drawable.flag};
adapter=new ListAdapter(this,seasonNos,seasonDescriptions,seasonThumbs);
listView_seasons.setAdapter(adapter);
}
//____________________________Adapter Class________________________//public class ListAdapter extends ArrayAdapter<
String>
{
Context context;
String sezonNo[];
String desc[];
int[] imgs;
public ListAdapter(@NonNull Context context, String[] sezonNo, String[] desc, int[] imgs) {super(context,R.layout.layout_album,R.id.textViewSeasonNo,sezonNo);
this.context=context;
this.sezonNo = sezonNo;
this.desc = desc;
this.imgs = imgs;
Log.d("List","Adapter");
}@Override
public int getCount() {
return 0;
}@Override
public long getItemId(int position) {
return 0;
}@Override
public View getView(int position, View view, ViewGroup parent) {
Log.d("List","setting bindings...");
view=getLayoutInflater().inflate(R.layout.layout_album,null);
TextView txtSezonNo=view.findViewById(R.id.textViewSeasonNo);
TextView txtDescription=view.findViewById(R.id.textViewDescription);
ImageView imgThumbnail=view.findViewById(R.id.imgThumbnail);
progressDialog.hide();
txtSezonNo.setText(seasonNos[position]);
txtDescription.setText(seasonDescriptions[position]);
imgThumbnail.setImageResource(seasonThumbs[position]);
return view;
}
}
Adapter类的构造函数被调用,但
getView()
根本没有被调用。答案你的
getView()
没有被调用,因为你在getCount()
返回的大小是0请在你的
getCount()
把这个:@Override
public int getCount() {
return sezonNo.length;
}
【适用于Android ListView的适配器类getView()函数】
getItemId
也不应该是所有人的0
,简单的解决方案可能是列表中项目的返回位置。返回position
之类的@Override
public long getItemId(int position) {
return position;
}
推荐阅读
- Android中的方向更改时,列表视图会被清除(使用customAdapter,List )
- 如何根据android视图中的可见内容调整大小()
- Android - 使用空textview的SwipeRefreshLayout
- Android ViewPager,ListFragments以及更新列表中的元素时更新
- 需要Android数据库帮助
- 如何将ListView标题放入Android泡泡中
- Android ListView ContextMenu
- 我可以在通用Android应用程序中使用WearableListView吗()
- 如何在Android中的ListView中突出显示行()