【问题标题】:How to update the listview in OnResume() using asynctask如何使用异步任务更新 OnResume() 中的列表视图
【发布时间】:2015-11-16 11:01:47
【问题描述】:

我在使用 OnResume() 上的 ArrayAdapter 更新通过 Async TAsk 加载的列表视图时遇到问题。

我将列表发送到数组适配器构造函数。

从活动 2 开始,一个项目被添加到数据库并返回到活动 1。在此添加的项目应在列表视图中更新。

下面是代码。

public class ActivityListView extends ActionBarActivity {

    ListView listView;
    MemoDBHandler dbHandler;
    List<MemoInfo> memoList = null;
    ListAdapter adapter = null;
    Cursor cursor;
    ListFetchAsyncTask asyncList = null;
    private static final String TAG = "Msg";

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

        // Get ListView object from xml
        listView = (ListView) findViewById(R.id.list);
        dbHandler = new MemoDBHandler(this, null, null, 1);
        cursor = dbHandler.getFullListCursor();


        asyncList = new ListFetchAsyncTask();
        asyncList.execute();

        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener()

                {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        viewMemo(position);
                    }
                }

        );
    }

    public void viewMemo(int position) {
        MemoInfo selectedMemo = memoList.get(position);
        Intent memoIntent = new Intent(getBaseContext(), MemoTextDetailActivity.class);
        memoIntent.putExtra("memoID", selectedMemo.get_id());
        startActivity(memoIntent);
        //finish();
    }


    public void deleteSingleMemo(int position) {
        MemoInfo selectedMemo = memoList.get(position);
        memoList.remove(position);

        dbHandler.deleteMemo(selectedMemo.get_id());
        //This will refresh the list after deletion
        ((BaseAdapter) adapter).notifyDataSetChanged();


    }

    class ListFetchAsyncTask extends AsyncTask<String, Integer, List<MemoInfo>> {

        @Override
        protected void onPreExecute() {

            listView.setFastScrollAlwaysVisible(false);
            listView.removeAllViewsInLayout();

        }

        protected List<MemoInfo> doInBackground(String... params) {
            Cursor cursor = dbHandler.getFullListCursor();
            if (cursor != null) {
                if (memoList != null) {
                    if (!memoList.isEmpty()) {
                        memoList.clear();
                    }
                } else
                    memoList = new ArrayList<MemoInfo>();

                if (cursor.moveToFirst()) {
                    do {

                        // Get version from Cursor
                        String memoText = cursor.getString(cursor.getColumnIndex("text"));
                        String createdDate = cursor.getString(cursor.getColumnIndex("createdDate"));

                        Integer id = cursor.getInt(cursor.getColumnIndex("_id"));
                        Log.i(TAG, "ID = " + id);
                        MemoInfo memo = new MemoInfo();
                        memo.set_id(id);
                        memo.set_memotext(memoText);
                        memo.set_createdDate(createdDate);
                        memoList.add(memo);

                        // move to next row
                    } while (cursor.moveToNext());
                }
            }

            return memoList;

        }

        @Override
        protected void onPostExecute(List<MemoInfo> list) {
            if (!list.isEmpty()) {

                adapter = new CustomAdapter(getApplicationContext(), list);
                listView.setAdapter(adapter);
            }
        }
    }


    @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_activity_list_view, 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;
        } else if (id == R.id.action_create) {
            Intent memoIntent = new Intent(getBaseContext(), MainActivity.class);
            startActivity(memoIntent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onResume() {
        super.onResume();
            if (!memoList.isEmpty()) {

                ((BaseAdapter) adapter).notifyDataSetChanged();
            }
    }
}

【问题讨论】:

  • if (!memoList.isEmpty()) { ((BaseAdapter) adapter).notifyDataSetChanged(); }你不需要游标,你需要从数据库中获取数据然后更新列表。

标签: android listview android-asynctask


【解决方案1】:

如果你想更新你应该试试这个

 @Override
    protected void onResume() {
        super.onResume();

    new ListFetchAsyncTask.execute();// asyncList.execute();
            if (!memoList.isEmpty()) {

                ((BaseAdapter) adapter).notifyDataSetChanged();
            }
    }

【讨论】:

  • 更新列表的方式不对,AsyncTask是一个后台进程,如果你在调用AsyncTask后立即尝试更新列表,它永远不会更新列表中的内容这个操作需要在AsyncTaskonPostExecuteMethod 中完成if (!memoList.isEmpty()) { ((BaseAdapter) adapter).notifyDataSetChanged(); }
【解决方案2】:
  @Override
  protected void onResume()
  {
    super.onResume();
       new ListFetchAsyncTask.execute();
        if (!memoList.isEmpty()) {
         ///Use following steps 

           ///1.Here u can call the web service on the onResume method
           ////2. On post exceute of yours web service calling, you can update your listview


        }
   }

【讨论】:

  • 更新列表的方式不对,AsyncTask是一个后台进程,如果你在调用AsyncTask后立即尝试更新列表,它永远不会更新列表中的内容这个操作需要在AsyncTaskonPostExecuteMethod 中完成if (!memoList.isEmpty()) { ((BaseAdapter) adapter).notifyDataSetChanged(); }
  • 是的,你是对的......列表总是在调用网络服务后更新,或者你可以在 postExcecute 上说,我已经更新了我的代码.. @NitinMisra
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多