我遇到了一些困難,將值填充到列表視圖,該列表視圖當前不顯示任何行。列表視圖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方法嗎?提前致謝。
add listAdapter.notifyDataSetChanged(); setAdapter後 – ZeroOne
你嘗試過'listAdapter.notifyDataSetChanged()'在你的'setAdapter'下面嗎? –
@GustavoConde我已經添加了這個,但是listview仍然沒有填充。使用調試器和getView方法中的一個斷點來完成整個過程,但它絕不會被擊中。 –