Niamh Doyle
Niamh Doyle

Reputation: 1949

Delete a row from list view and from its text file

I load the content of a text file into a list view. The text file (his.his) contain lines of text. Now I want to apply a context menu to delete a list row and the equivalent text line from the text file. But I only succeed in removing list row from the list view, and equivalent text line cannot be deleted from the text file. Can you please help? Thanks a lot in advance.

Here is my complete code:

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();
}   
}

Upvotes: 0

Views: 137

Answers (1)

smuk
smuk

Reputation: 351

Instead of trying to delete one single line from the text file you can open it for writing and write all lines again - except deleted record. Iterate through your records and write each to the file.

Upvotes: 1

Related Questions