2014-09-06 73 views
0

我有一個適配器,我想將數據添加到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方法不工作。

+0

你在哪裏調用'RandomsAdapter.updateRandoms(randomsList)'? – 2014-09-06 16:44:54

+0

當我調用這個方法時,我得到:類型MainActivity.RandomsAdapter中的updateRandoms(MainActivity.Randoms)方法不適用於參數(List ) – 2014-09-06 16:58:10

+0

好吧,試試'Randoms randoms = new Randoms randomsList); RandomsAdapter.updateRandoms(randoms)' – 2014-09-06 17:04:18

回答

0

嘗試:

randomsAdapter.invalidate(); 

OR

((RandomsAdapter) randomsAdapter)).notifyDataSetChanged(); 

如果不工作,請參閱此主題: stackoverflow.com/questions/4088862/android-list-view-refresh/4088889

+0

方法invalidate()未定義的類型ListAdapter – 2014-09-06 16:55:35

+0

方法getAdapter()未定義的類型ListAdapter我得到這個 – 2014-09-06 20:30:12

0

加入

randomsList.add(new Random(result.get(i).getAsJsonObject(),1)); 

調用之後notifyDataSetChanged()適配器

randomsAdapter.notifyDataSetChanged(); 

編輯

((RandomsAdapter) randomsAdapter)notifyDataSetChanged(); 
+0

我得到這個錯誤:方法notifyDataSetChanged()是未定義的類型ListAdapter – 2014-09-06 16:54:36

+0

編輯與答案 – 2014-09-06 17:09:39

0

正如其他人則建議,使用

randomsAdapter.notifyDataSetChanged();

如果你得到一個錯誤「notifyDataSetChanged()是未定義的類型ListAdapter」,我想這是因爲你得到你r適配器通過getAdapter()或返回ListAdapter的此類方法。在這種情況下,您應該將其投射到BaseAdapterRandomsAdapter。例如,

((RandomsAdapter) randomsAdapter).notifyDataSetChanged(); 
+0

未定義類型ListAdapter的方法getAdapter() – 2014-09-06 20:30:52