Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:23:52+00:00 2026-06-04T21:23:52+00:00

I create a Context Menu for a ListView. On long-clicking an item in the

  • 0

I create a Context Menu for a ListView. On long-clicking an item in the ListView, a context menu with two options (Edit and Delete) will appear. My purpose is that when clicking Delete, only will the selected item in the ListView be removed from the list. But I don’t know why all the items in the list are removed when this is done.

Here below is my code lines. Can you please provide a little help? It would be nice if you could give instructions based on my code.

Thank you very much in advance.

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:

Thanks to raju, it now works (selected item removed from the list). However, the selected item is not removed from SharedPreferences. That’s why it reappears when the the list is reloaded.

I use the following code lines to save items to SharedPreferences. Could anyone tell me how to update the SharedPreferences so that it reflects the change when items are removed from the list using raju’s code lines. Thank you very much indeed.

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();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:23:54+00:00Added an answer on June 4, 2026 at 9:23 pm
    aptList.clear();
    

    this line clears adapter… so all the items are being deleted..
    instead use

                   aptlist.remove(//the selected item);
    

    in place of this

       deleteItem(info.id);
    

    put

      String content =getListView().getItemAtPosition(info.position);
       aptList.remove(content);
       aptList.notifyDataSetChanged();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView that will allow the user to long-press an item to
I'm trying to create a context menu that changes the available options depending on
I want to create a context menu that support multiple files. I read through
I managed to create a context menu that gets activated after a right click
I'm trying to create a context menu, so that when you right click the
Ok so I know you can create a context menu when a user long
I'm working with a listview and a context menu. When the user long presses
I have two activities that both contain an identical context menu built programmatically using
I'm trying to create a context menu which will be able to add extra
Hi I am new to wicket framework, I need to create a context menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.