适用于Android ListView的适配器类getView()函数

赋料扬雄敌,诗看子建亲。这篇文章主要讲述适用于Android ListView的适配器类getView()函数相关的知识,希望能为你提供帮助。
我正在尝试创建一个ListView,其中包含一个项目列表,每个项目都有三个组件。

  1. 标题(季节性)
  2. 描述
  3. 图像(SezonThumb)
以下是布局文件“layout_album”的代码,列表中的每个项目如何看起来如下:
< ?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; }


    推荐阅读