【发布时间】:2017-04-19 15:27:09
【问题描述】:
我在将值填充到当前不显示任何行的列表视图时遇到了一些困难。
首先,我从我的数据库表中检索值并将它们存储在 onPostExecute 内的数组中。
LikedListAdapter listAdapter = new LikedListAdapter(context, R.layout.liked_list_main, restID, restName, restAddr1, restAddr2, restImgPath);
lvPlaces.setAdapter(listAdapter);
这些值随后成功传递到我的 ListAdapter。
public LikedListAdapter(Context context, int resource, String[] restID, String[] restName, String[] restAddr1, String[] restAddr2, String[] restImgPath) {
super(context, resource);
this.context = context;
this.resource = resource;
this.restID = restID;
this.restName = restName;
this.restAddr1 = restAddr1;
this.restAddr2 = restAddr2;
this.restImgPath = restImgPath;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout likedItemsView;
if(convertView==null) {
likedItemsView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, likedItemsView, true);
}
else {
likedItemsView = (LinearLayout) convertView;
}
ImageView restaurantImg = (ImageView)likedItemsView.findViewById(R.id.listItemThumbImg);
TextView restaurantName =(TextView)likedItemsView.findViewById(R.id.listItemTitle);
TextView restaurantDesc = (TextView)likedItemsView.findViewById(R.id.listItemSubText);
restaurantName.setText(restName[position]);
restaurantDesc.setText(restAddr1[position] + ", " + restAddr2[position]);
Picasso
.with(getContext())
.load(restImgPath[position])
.into(restaurantImg);
return likedItemsView;
}
但是,当我运行应用程序时,Listview 是空的。调试时,我注意到这些值已成功传递给我的 listAdapter(在调试时它显示检索到的值正在显示在构造函数中)但是它从未命中方法 getView,其中值设置为每个 listview 小部件。
我有什么误解,还是我需要在某个时候调用 getView 方法?提前致谢。
【问题讨论】:
-
添加 listAdapter.notifyDataSetChanged();在 setAdapter 之后
-
你试过在你的
setAdapter下面加上listAdapter.notifyDataSetChanged()吗? -
@GustavoConde 我已经添加了这个,但列表视图仍然没有填充。在 getView 方法中使用调试器和断点遍历整个过程,但从未命中。
-
正如@MMbarno 所说,您必须实现 getCount() 来告诉 ListView 您的适配器有多少项目。否则,假设适配器有 0 个项目,因此永远不会调用 getView()。
标签: java android listview listadapter