Android ListView 不调用适配器的类 getView() 函数
Posted
技术标签:
【中文标题】Android ListView 不调用适配器的类 getView() 函数【英文标题】:Adapter's class getView() function not called for Android ListView 【发布时间】:2019-09-09 02:40:13 【问题描述】:我正在尝试创建一个包含项目列表的 ListView,每个项目都包含三个组件。
-
标题(SezonNo)
说明
图像 (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_
android:layout_
tools:context=".Activities.SeasonsActivity">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<ImageView
android:layout_
android:layout_
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_
android:layout_
android:background="@color/Gray"></View>
</LinearLayout>
</ScrollView>
以下是Activity的代码:
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;
适配器类的构造函数被调用,但 getView()
根本没有被调用。
【问题讨论】:
view=getLayoutInflater().inflate(R.layout.layout_album,null);将此更改为 view=getLayoutInflater().inflate(R.layout.layout_album,parent,false); 首先不要用listview代替,用recyclerview。 那是因为你的适配器的项目数静态设置为 0 【参考方案1】:您的 getView()
未被调用,因为您在 getCount()
中返回的大小为 0
请在您的getCount()
中输入:
@Override
public int getCount()
return sezonNo.length;
同样getItemId
不应该是0
,简单的解决方案可以是返回列表中项目的位置。返回position
喜欢
@Override
public long getItemId(int position)
return position;
【讨论】:
已解决。谢谢!以上是关于Android ListView 不调用适配器的类 getView() 函数的主要内容,如果未能解决你的问题,请参考以下文章
android ListView使用适配器,怎么让指定的项居中?
Android中适配器的notifyDataSetChanged()为何有时不刷新
Android ListView 适配器 notifyDataSetInvalidated() 与 notifyDataSetChanged()