刷新listview以更新数据?

时间:2015-03-08 06:56:16

标签: android listview

我有listview一些静态数据。刷新活动完成后,需要在listview中更新一些数据。问题是刷新完成后,新项目不会更新。

MainActivity.class

public class MainActivity extends ActionBarActivity {

SwipeRefreshLayout swipeLayout;
String[] players = {"Dravid","Ganguly","Sachin","Irfan pathan","Balaji","Bhuvi","Praveen"};
ListView listView;
ArrayAdapter<String> adapter = null;
ArrayList al = new ArrayList();

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

    listView = (ListView)findViewById(R.id.listView);


    for (int i =0; i<players.length; i++)
    {
        Bean b = new Bean();
        b.setName(players[i]);
        al.add(b);
    }

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,players);
    listView.setAdapter(adapter);

    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {

            swipeLayout.setRefreshing(true);
            new updateItems().execute();
        }

    });

    swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);
}

 class updateItems extends AsyncTask<Void, Void, Void>
{


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {

       String[] newitems = {"Ishant sharma","Ramesh power","Kaif"};
       // adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,newitems);


        for (int j=0;j<newitems.length;j++)
        {
            Bean bb = new Bean();
            bb.setName(newitems[j]);
            al.add(bb);

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);



        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
               listView.invalidateViews();
            }
        });



        swipeLayout.setRefreshing(false);
    }
}



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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Bean.class

public class Bean {

String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

我在哪里弄错了?

1 个答案:

答案 0 :(得分:2)

这是您最初设置的适配器:

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,players);

doInBackground()方法中,更新a1 ArrayList,但适配器所依赖的播放器阵列保持不变,因此即使执行adapter.notifyDataSetChanged(),列表视图中也不会反映任何更改。

其次,onPostExecute方法在ui线程上运行,因此您不需要明确地执行此操作。