2013-05-27 77 views
0

我將文本文件的內容加載到列表視圖中。文本文件(his.his)包含文本行。現在我想應用上下文菜單從文本文件中刪除列表行和等效的文本行。但是,我只能從列表視圖中刪除列表行,並且不能從文本文件中刪除等效的文本行。你能幫忙嗎?提前致謝。從列表視圖及其文本文件中刪除一行

這裏是我的完整代碼:

public class HistoryView extends Activity { 
private static final String History_TAG = "[MyApp - 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 String fileLocation= Environment.getExternalStorageDirectory().getPath()+"/his.his"; 
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 = null; 
     try { 
      strHistory = new Scanner(new File(fileLocation)).useDelimiter("\\z").next(); 
     } catch (FileNotFoundException e) {    
      e.printStackTrace();   } 

     Log.i(History_TAG, "History loaded"); 
     if (strHistory != null && !strHistory.equals("")) 
     { 
      mWordHistory = new ArrayList<String>(Arrays.asList(strHistory.split("\n"))); 
     } 
     else 
     { 
      mWordHistory = new ArrayList<String>(); 
     } 
    } 
    else 
    { 
     mWordHistory = new ArrayList<String>(); 
    }  
    Log.d(History_TAG,"mWordHistory = " + mWordHistory.size()); 

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

    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) 
       { 
        lstDict.add(i,arrPart[0]); 
        lstId.add(i,Integer.parseInt(arrPart[1])); 
        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.setAdapter(aptList);  

} 
@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.delete: 
      String content = (String) mLSTHistory.getItemAtPosition(info.position); 

       aptList.remove(content); 

       aptList.notifyDataSetChanged(); 

      deleteNote(info.id); 
      return true; 
     default: 
      return super.onContextItemSelected(item); 
    } 
} 
private void deleteNote(long id) { 
    //I have the problem here to remove the text line which is populated into the list view 
    PrintWriter writer = null; 
    try { 
     writer = new PrintWriter(fileLocation); 
    } catch (FileNotFoundException e) {   
     e.printStackTrace(); 
    } 
    writer.print(""); 
    writer.close(); 
} 
} 

回答

1

而不是試圖刪除文本文件,你可以打開它進行寫作和寫一遍所有線中的一條單行 - 除了刪除的記錄。迭代您的記錄並將每個記錄寫入文件。

+0

謝謝。但請更具體的關於我的代碼。 –