从列表视图及其文本文件中删除一行

时间:2013-05-27 16:35:43

标签: java android listview

我将文本文件的内容加载到列表视图中。文本文件(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 :(得分:1)

除了尝试从文本文件中删除一行之外,您可以打开它进行写入并再次写入所有行 - 除了已删除的记录。迭代记录并将每个记录写入文件。