2017-04-19 141 views
0

我遇到了一些困难,将值填充到列表视图,该列表视图当前不显示任何行。列表视图onPostExecute后未填充

首先,我从我的数据库表中检索值并将它们存储在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方法吗?提前致谢。

+0

add listAdapter.notifyDataSetChanged(); setAdapter后 – ZeroOne

+0

你尝试过'listAdapter.notifyDataSetChanged()'在你的'setAdapter'下面吗? –

+0

@GustavoConde我已经添加了这个,但是listview仍然没有填充。使用调试器和getView方法中的一个断点来完成整个过程,但它绝不会被击中。 –

回答

1

你重写

getCount将()

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

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

希望你的问题将得到解决。如果已经填充的列表,然后使用

adapter.notifyDataSetChanged()

方法。

+0

我没有包含getCount()方法。我现在已经添加,它完美的工作。非常感谢你。 –

0
LikedListAdapter listAdapter = new LikedListAdapter(context, R.layout.liked_list_main, restID, restName, restAddr1, restAddr2, restImgPath); 
lvPlaces.setAdapter(listAdapter); 
listAdapater.notifyDataSetChanged(); // <-- add this 
+0

我已经添加了这个,但是listview仍然没有填充。使用调试器和getView方法中的一个断点来完成整个过程,但它绝不会被击中。 –