ArrayList Adapter仅在手动添加对象后更新View

时间:2014-03-17 01:01:52

标签: android android-arrayadapter android-view

对不起,标题非常糟糕,

基本上我有一个适配器aa = new tweetAdapter(this, resID, tweetListFull);,只有在我使用aa.add然后aa.notifyDataSetChanged()之后才更新,我的理解是这应该适用于tweetListFull.add然后{{1}还有...

因此,当我使用aa.notifyDataSetChanged()aa.add(tweetListFull.get(0))时,视图会按预期添加。虽然从我的想法,我认为将项目添加到tweetListFull列表沙,然后aa.notifyDataSetChanged()也应该工作但它没有?

MainActivity

aa.notifyDataSetChanged()

tweetAdapter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    render = new tweetView(this);

    LinearLayout canvas = (LinearLayout)findViewById(R.id.content_frame);
    ListView tweetList = (ListView)findViewById(R.id.tweetView);

    tweetListFull = new ArrayList<Tweet>();
    int resID = R.layout.tweet;
    aa = new tweetAdapter(this, resID, tweetListFull);

    Log.w("Check","Here1");        
    tweetList.setAdapter(aa);
    Log.w("Check","Here1");
    //tweetList.addView()
    canvas.addView(render);



    // UI //        
    drawerListViewItems = getResources().getStringArray(R.array.items); 
    // get ListView defined in activity_main.xml
    drawerListView = (ListView) findViewById(R.id.left_drawer); 
    // Set the adapter for the list view
    drawerListView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_listview_item, drawerListViewItems));



    Button.OnClickListener listener = new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.Refresh){                  
                twitterTest tweet = new twitterTest();
                tweet.execute(-3.19523741,56.21523879); // The argument for this is the query to search for.
            }
            else if(v.getId() == R.id.Load){
                Log.w("Load1",String.valueOf(tweetListFull));
                Log.w("Load1",String.valueOf(tweetListFull.size()));
                //aa.add(tweetListFull.get(2));
                aa.notifyDataSetInvalidated();
                aa.notifyDataSetChanged();
            }               
        }
    };
    ((Button)findViewById(R.id.Refresh)).setOnClickListener(listener);  
    ((Button)findViewById(R.id.Load)).setOnClickListener(listener);  
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:1)

notifyDataSetChanged仅在向tweetListFull添加一个或多个项目时有效;你可以这样做:

((Button)findViewById(R.id.Load)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 tweetListFull.add(yourNewItem);
                 aa.notifyDataSetChanged();
            }
        });

}