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

Java 从列表视图及其文本文件中删除行,java,android,listview,Java,Android,Listview,我将文本文件的内容加载到列表视图中。文本文件(his.his)包含多行文本。现在我想应用上下文菜单从文本文件中删除列表行和等效文本行。但我只能成功地从列表视图中删除列表行,并且无法从文本文件中删除等效的文本行。你能帮忙吗?先谢谢你 这是我的完整代码: public class HistoryView extends Activity { private static final String History_TAG = "[MyApp - HistoryView] "; private List

我将文本文件的内容加载到列表视图中。文本文件(
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();
}   
}
公共类历史视图扩展活动{
私有静态最终字符串历史记录_TAG=“[MyApp-HistoryView]”;
私有ListView mLSTHistory=null;
private ArrayList lstDict=null;
私有ArrayList lstId=null;
private ArrayAdapter aptList=null;
private ArrayList mWordHistory=null;
私有字符串fileLocation=Environment.getExternalStorageDirectory().getPath()+“/his.his”;
私人共享参考优先权;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
prefs=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());
if(prefs.getBoolean(“saveHistory”,true))
{
字符串strHistory=null;
试一试{
strHistory=new Scanner(新文件(fileLocation)).useDelimiter(“\\z”).next();
}catch(filenotfound异常){
e、 printStackTrace();}
Log.i(History_标签,“History loaded”);
if(strHistory!=null&!strHistory.equals(“”)
{
mWordHistory=newarraylist(Arrays.asList(strHistory.split(“\n”));
}
其他的
{
mWordHistory=newarraylist();
}
}
其他的
{
mWordHistory=newarraylist();
}       
Log.d(History_标记,“mWordHistory=“+mWordHistory.size());
mLSTHistory=(ListView)findViewById(R.id.lstHistory);
registerForContextMenu(mLSTHistory);
if(lstDict==null)
{
lstDict=newarraylist();
lstId=newarraylist();
aptList=newarrayadapter(getApplicationContext(),R.layout.customlist);
}
lstDict.clear();
lstId.clear();
aptList.clear();
if(mWordHistory!=null&&mWordHistory.size()>0)
{
尝试
{
对于(int i=0;i
您可以打开文本文件进行写入,然后再次写入除已删除记录之外的所有行,而不是尝试从文本文件中删除一行。遍历记录并将每个记录写入文件