我有一個適配器,我想將數據添加到adapter.This是我的源代碼:Android的適配器無法更新
private static class Random{
public JsonObject randomData=null;
public Integer badgeNumber=null;
private Random(JsonObject randomData,Integer badgeNumber) {
this.randomData=randomData;
this.badgeNumber=badgeNumber;
}
}
private static class Randoms{
private List<Random> randoms=null;
public Randoms(List<Random> randoms) {
this.randoms=randoms;
}
public Random get(int position) {
return randoms.get(position);
}
public int size() {
return randoms.size();
}
}
private static class RandomsAdapter extends BaseAdapter{
private Randoms randoms;
private LayoutInflater inflater;
private RandomsAdapter(Context context,Randoms randoms) {
this.randoms=randoms;
this.inflater = LayoutInflater.from(context);
}
public void updateRandoms(Randoms randoms) {
this.randoms=randoms;
notifyDataSetChanged();
}
@Override
public int getCount() {
return randoms.size();
}
@Override
public Random getItem(int position) {
return randoms.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position,View convertView,ViewGroup parent) {
View view=convertView;
if (convertView == null) {
convertView=inflater.inflate(R.layout.random_bars,parent,false);
}
Random item=getItem(position);
Log.w("testt",""+item);
return convertView;
}
}
而且我這個代碼定義適配器:
Randoms randoms=new Randoms(randomsList);
randomsAdapter=new RandomsAdapter(this,randoms);
listView = (ListView)findViewById(R.id.list);
listView.setAdapter(randomsAdapter);
當我嘗試添加我使用這行
randomsList.add(new Random(result.get(i).getAsJsonObject(),1));
但這不工作數據,我看不到任何日誌以「testt」 tag.H我可以解決這個問題嗎?
我的意思是getView
方法不工作。
你在哪裏調用'RandomsAdapter.updateRandoms(randomsList)'? – 2014-09-06 16:44:54
當我調用這個方法時,我得到:類型MainActivity.RandomsAdapter中的updateRandoms(MainActivity.Randoms)方法不適用於參數(List) –
2014-09-06 16:58:10
好吧,試試'Randoms randoms = new Randoms randomsList); RandomsAdapter.updateRandoms(randoms)' – 2014-09-06 17:04:18