2012-05-30 244 views
1

我爲ListView創建了一個上下文菜單。在長按ListView中的某個項目時,將出現一個包含兩個選項(編輯和刪除)的上下文菜單。我的目的是當單擊Delete時,只有ListView中的選定項目纔會從列表中移除。但是我不知道爲什麼當這樣做完成後,列表中的所有項目都會被刪除。從Android ListView刪除項目

下面是我的代碼行。 你能提供一些幫助嗎?如果您可以根據我的代碼給出說明,那將會很好。

非常感謝您提前。

public class HistoryView extends Activity { 
private static final String HISTORY_TAG = "[AppName - HistoryView] "; 
private ListView mLSTHistory = null; 
private ArrayList<String> lstDict = null; 
private ArrayList<Integer> lstId = null; 
private ArrayAdapter<String> aptList = null; 
private ArrayList<String> mWordHistory = null; 
private SharedPreferences prefs; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.history); 
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 

    if (prefs.getBoolean("saveHistory", true)) 
    { 
     String strHistory = prefs.getString("history", ""); 
     Log.i(HISTORY_TAG, "History loaded"); 
     if (strHistory != null && !strHistory.equals("")) 
     { 
      mWordHistory = new ArrayList<String>(Arrays.asList(strHistory.split(","))); 
     } 
     else 
     { 
      mWordHistory = new ArrayList<String>(); 
     } 
    } 
    else 
    { 
     mWordHistory = new ArrayList<String>(); 
    } 

    Log.d(HISTORY_TAG,"mWordHistory = " + mWordHistory.size()); 

    if (lstDict == null) 
    { 
     lstDict = new ArrayList<String>(); 
     lstId = new ArrayList<Integer>(); 
     aptList = new ArrayAdapter<String>(getApplicationContext(),R.layout.customlist); 
    } 
    lstDict.clear(); 
    lstId.clear(); 
    aptList.clear(); 
    if (mWordHistory != null && mWordHistory.size() > 0) 
    { 
     try 
     { 
      for (int i=0; i < mWordHistory.size(); i++) 
      { 
       Log.i(HISTORY_TAG,"item = " + mWordHistory.get(i)); 
       String arrPart[] = mWordHistory.get(i).split("::"); 
       if (arrPart.length == 3) 
       { 
        //Log.i(CONTENT_TAG, "loaded content " +arrPart.length + ", wordId = " + arrPart[1]); 
        //Log.i(CONTENT_TAG, "loaded 0"); 
        lstDict.add(i,arrPart[0]); 
        //Log.i(CONTENT_TAG, "loaded 1"); 
        lstId.add(i,Integer.parseInt(arrPart[1])); 
        //Log.i(CONTENT_TAG, "loaded 2"); 
        aptList.add(arrPart[2]); 
       } 
       else 
       { 
        Log.i(HISTORY_TAG,"Wrong entry: " + mWordHistory.get(i)); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      Log.i(HISTORY_TAG,"Wrong entry found!"); 
     } 
    } 

    mLSTHistory = (ListView) findViewById(R.id.lstHistory); 
    registerForContextMenu(mLSTHistory); 
    mLSTHistory.setAdapter(aptList); 

    mLSTHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) 
     { 
      Intent i = new Intent(); 
      i.putExtra("dict", lstDict.get(arg2)); 
      i.putExtra("wordId", lstId.get(arg2)); 
      setResult(RESULT_OK,i); 
      finish(); 
     } 
    }); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.context_menu, menu); 
} 
@Override 
public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    switch (item.getItemId()) { 
     case R.id.edit: 
      editItem(info.id); 
      return true; 
     case R.id.delete: 
      deleteItem(info.id); 
      return true; 
     default: 
      return super.onContextItemSelected(item); 
    } 
    } 
private void deleteItem(long id) { 
    // TODO Auto-generated method stub 
    mWordHistory.clear(); 
    aptList.clear(); 
    mLSTHistory.setAdapter(aptList); 
    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putString("history", ""); 
    editor.commit(); 
    setResult(RESULT_OK); 
    finish(); 
    } 
private void editItem(long id) { 
    //Edit item... 
    } 
} 

UPDATE:

由於拉朱,現在它工作(所選項目從列表中刪除)。但是,所選項目不會從SharedPreferences中刪除。這就是重新加載列表時重新出現的原因。

我使用以下代碼行將項目保存到SharedPreferences。任何人都可以告訴我如何更新SharedPreferences,以便它反映了使用raju的代碼行從列表中刪除項目時的更改。非常感謝你。

if (prefs.getBoolean("saveHistory", true) && mWordHistory != null && mWordHistory.size() >= 1) 
     prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     StringBuilder sbHistory = new StringBuilder(); 
     for (String item : mWordHistory) 
     { 
      sbHistory.append(item); 
      sbHistory.append(","); 

     String strHistory = sbHistory.substring(0, sbHistory.length()-1); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("history", strHistory); 
     editor.commit(); 
     } 
+0

說實話,我看了你的代碼確實很快,但

代碼你不清除完全mWordHistory和aptList? – seth

+0

aptlist.clear()是罪魁禍首 – Orlymee

+0

call list.remove();而不是list.clear() – Shrikant

回答

3
aptList.clear(); 

這一行清除適配器......因此,所有的項目都被刪除.. 改用

   aptlist.remove(//the selected item); 

代替這個

deleteItem(info.id); 

String content =getListView().getItemAtPosition(info.position); 
    aptList.remove(content); 
    aptList.notifyDataSetChanged(); 
+0

@naju:這是一個!非常感謝您指出。但我怎麼能得到選定的項目,我的意思是具體的?我試過:'aptList.remove(「wordId」);'但它仍然消除了一切。 –

+0

查看編輯答案 – 5hssba

+0

非常感謝。我遵循你的指示如下:'String content =(String)mLSTHistory.getItemAtPosition(info.position); \t \t aptList.remove(content); \t \t aptList.notifyDataSetChanged(); \t deleteNote(info.id);'。但問題似乎在於共享首選項。我使用共享首選項保存項目,並且在應用代碼時,它似乎刪除項目,但該項目仍然存在於共享首選項中,並且在重新加載ListView時它會重新出現。 –

0

我按照你的建議,在我的情況下,它看起來像下面;在活動課

適配器聲明:

private MyObjectAdapter adapter; 

適配器的創建和設置適配器的onCreate在活動類:

adapter = new MyObjectAdapter(MainActivity.this, MyObject.getList(this)); 
setListAdapter(adapter); 

的GetList是我自己的方法。從列表視圖中刪除項

MyObject myObjectToRemove = (MyObject)getListView().getItemAtPosition(positionAtList); 
adapter.remove(myObjectToRemove); 
adapter.notifyDataSetChanged();