从ActionBarActivity更新listView

时间:2015-03-10 13:33:33

标签: android listview android-actionbaractivity

我的MainActivity,它扩展了ActionBarActivity,有一个ListView,我想用notifyDataSetChanged()重新加载,但我不知道怎么做。

到目前为止我的代码:

public class MainActivity extends ActionBarActivity {
    private JournalEntryDataSource datasource;
    private ListView listView = null;

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

        datasource = new JournalEntryDataSource(this);
        datasource.open();
        listView = (ListView) findViewById(R.id.list);
        List<JournalEntry> values = datasource.getAllJournalEntries();


        listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));



        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                Log.i("app", "Deleting item number " + position);
                datasource.deleteJournalEntry((JournalEntry) listView.getAdapter().getItem(position));

                //I would like to do this here:  
                //ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>) getListAdapter();
                //adapter.notifyDataSetChanged(); 

            }
        });


    }

但是在调用adapter.notifyDatasetChanged() ...

时出错了

任何提示?我一直在寻找类似的问题,到目前为止没有运气...... --------编辑代码---------

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datasource = new JournalEntryDataSource(this);
        datasource.open();
        listView = (ListView) findViewById(R.id.list);
        values = datasource.getAllJournalEntries();
        listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
        ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>) getListAdapter();
        // use the SimpleCursorAdapter to show the
        // elements in a ListView



        listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
        listView.setLongClickable(true);
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                Log.i("hola","borrando la posición "+position);
                JournalEntry removeMe = (JournalEntry)listView.getAdapter().getItem(position);
                values.remove(removeMe);
                datasource.deleteJournalEntry(removeMe);
                //adapter.notifyDataSetChanged();
                reloadMe();


                return true;
            }
        });

    }

    public void reloadMe(){
        //need to do this better... but meh.
        Intent intent = new Intent(this, MainActivity.class);
        this.startActivity(intent);
    }

    protected ListView getListView() {
        if (listView == null) {
            listView = (ListView) findViewById(android.R.id.list);
        }
        return listView;
    }

    protected void setListAdapter(ListAdapter adapter) {
        getListView().setAdapter(adapter);
    }

    protected ListAdapter getListAdapter() {
        ListAdapter adapter = getListView().getAdapter();
        if (adapter instanceof HeaderViewListAdapter) {
            return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
        } else {
            return adapter;
        }
    }

}

1 个答案:

答案 0 :(得分:2)

更新了更新代码的答案

而不是再次创建相同的活动,您应该选择使用新值对象设置另一个新的适配器对象。

public void reloadMe(){
        //need to do this better... but meh.
        //Intent intent = new Intent(this, MainActivity.class);
        //this.startActivity(intent);
        listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values));
    }

也许,您要从

中删除
datasource.deleteJournalEntry((JournalEntry) listView.getAdapter().getItem(position));

但你没有设置&#34;数据源&#34;作为适配器的数据源。如果仔细观察,你确实设定了&#34;值&#34;列表对象 -

listView.setAdapter(new ArrayAdapter<JournalEntry>(this, R.layout.list_item, R.id.lugares, values)); 

也许您可能还需要从值列表中删除。