【问题标题】:Listview not populated after onPostExecuteonPostExecute 后未填充列表视图
【发布时间】: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


【解决方案1】:

你是否覆盖了

getCount()

方法?如果不是,则返回行的大小。前-

@Override
public int getCount() {
    return restName.length;
}

希望您的问题能够得到解决。如果已填充列表,则使用

adapter.notifyDataSetChanged()

方法。

【讨论】:

  • 我没有包含 getCount() 方法。我现在添加了它,它工作得很好。非常感谢。
【解决方案2】:
LikedListAdapter listAdapter = new LikedListAdapter(context, R.layout.liked_list_main, restID, restName, restAddr1, restAddr2, restImgPath);
lvPlaces.setAdapter(listAdapter);
listAdapater.notifyDataSetChanged();  // <-- add this

【讨论】:

  • 我已经添加了这个,但是列表视图仍然没有填充。在 getView 方法中使用调试器和断点遍历整个过程,但从未命中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多