2015-11-16 79 views
0

我在使用OnResume()上的ArrayAdapter更新通過Async Task加載的listview時遇到問題。如何使用asynctask更新OnResume()中的列表視圖

我將列表發送到數組適配器構造函數。

從活動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(); 
      } 
    } 
} 
+0

(memoList.isEmpty()!); }' 你不需要光標,你需要從數據庫獲取數據,然後更新列表。 –

回答

0

如果你想更新你應該 `如果{ ((BaseAdapter)適配器).notifyDataSetChanged()試試這個

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

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

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

這是更新列表的錯誤方法,'AsyncTask'是一個後臺進程,如果您在調用'AsyncTask'後立即更新列表,它將永遠不會更新列表中的內容 此操作需要在'onPostExecuteMethod' '(AsyncTask) 'if(!memoList.isEmpty()){(BaseAdapter)adapter).notifyDataSetChanged(); }' –

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 


     } 
    } 
+1

這是更新列表的錯誤方法,'AsyncTask'是一個後臺進程,如果您在調用'AsyncTask'後立即更新列表,它將永遠不會更新列表中的內容。 此操作需要完成AsyncTask的onPostExecuteMethod if(!memoList.isEmpty()){(BaseAdapter)適配器).notifyDataSetChanged(); }' –

+0

你是對的......列表總是在調用Web服務之後更新,或者你可以在postExcecute上說我已更新我的代碼.. @NitinMisra –

相關問題