如何使用asynctask更新OnResume()中的列表视图

时间:2015-11-16 11:01:47

标签: android listview android-asynctask

我无法更新使用OnResume()上的ArrayAdapter通过Async TAsk加载的listview。

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

从活动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();
            }
    }
}

2 个答案:

答案 0 :(得分:0)

如果您想要更新,请尝试使用

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

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

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

答案 1 :(得分:0)

  @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


        }
   }