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 8907055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:48:39+00:00 2026-06-15T02:48:39+00:00

Can somebody please give me an example code of removing a selected item in

  • 0

Can somebody please give me an example code of removing a selected item in a listview ?.
i am using simple adapter to store the data.
My code is..

static final String KEY_ITEM = "finance"; // parent node
static final String KEY_ID = "finance";
static final String KEY_NAME = "company";
static final String KEY_COST = "high";
static final String KEY_DESC = "volume";
static final String KEY_SYMBOL="symbol";
static final String KEY = "low";
private String selectedItem; 
private ListAdapter adapter;  
Context context;
ListView lv;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView titleBar = (TextView)getWindow().findViewById(android.R.id.title);
   titleBar.setTextColor(Color.GREEN);
   final ArrayList<HashMap<String, String>> menuItems = new    ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);

    for (int i = 0; i < nl.getLength(); i++) {

      // HashMap<String, String> map = new HashMap<String, String>();

       Node theAttribute;
        Element e = (Element) nl.item(i);


         NodeList nl1=e.getElementsByTagName(KEY_ID);
         System.out.println("keyId"+nl1.getLength());
         for(int j=0;j<nl1.getLength();j++)
         {
             Element e1 = (Element) nl1.item(j);
             HashMap<String, String> map = new HashMap<String, String>();
             NodeList n = e1.getElementsByTagName(KEY_NAME);

              for (int k = 0; k < n.getLength(); k++) {

                  Element e2 = (Element) n.item(k);
            //  System.out.println("node Title value"+e2.getNodeName());
                 NamedNodeMap attributes2 = e2.getAttributes();
                // System.out.println("attrlength"+attributes2.getLength());
                 for (int a = 0; a < attributes2.getLength(); a++) 
                 {
                         theAttribute = attributes2.item(a);

                     String s=theAttribute.getNodeValue();
                   //  lblName.setTypeface(hindiFont);
                        //s = s.replaceAll("[-;#39&amp:,]","");
                         map.put(KEY_NAME,s);

                 }

              }
              NodeList n4 = e1.getElementsByTagName(KEY_SYMBOL);
              // System.out.println("title "+n.getLength());
                for (int k = 0; k < n4.getLength(); k++) {

                    Element e2 = (Element) n4.item(k);
              //    System.out.println("node snippet value"+e2.getNodeName());
                   NamedNodeMap attributes2 = e2.getAttributes();

                   for (int a = 0; a < attributes2.getLength(); a++) 
                   { 
                    // HashMap<String, String> map = new HashMap<String, String>();
                         theAttribute = attributes2.item(a);
                         String s=theAttribute.getNodeValue();


                           map.put(KEY_SYMBOL,s);
                          // menuItems.add(map);
                   }

                } 

              NodeList n1 = e1.getElementsByTagName(KEY_COST);
            // System.out.println("title "+n.getLength());
              for (int k = 0; k < n1.getLength(); k++) {

                  Element e2 = (Element) n1.item(k);
            //    System.out.println("node Url value");
                 NamedNodeMap attributes2 = e2.getAttributes();
              //   System.out.println("attrlength"+attributes2.getLength());
                 for (int a = 0; a < attributes2.getLength(); a++) 
                 {  
                     //HashMap<String, String> map = new HashMap<String, String>();
                         theAttribute = attributes2.item(a);

                         map.put(KEY_COST,theAttribute.getNodeValue());
                 }}

                 NodeList n2 = e1.getElementsByTagName(KEY_DESC);
                // System.out.println("title "+n.getLength());
                  for (int k = 0; k < n2.getLength(); k++) {

                      Element e2 = (Element) n2.item(k);
                //    System.out.println("node snippet value"+e2.getNodeName());
                     NamedNodeMap attributes2 = e2.getAttributes();

                     for (int a = 0; a < attributes2.getLength(); a++) 
                     { 
                        // HashMap<String, String> map = new HashMap<String, String>();
                           theAttribute = attributes2.item(a);
                           String s=theAttribute.getNodeValue();


                             map.put(KEY_DESC,s);
                            // menuItems.add(map);
                     }

                  }   NodeList n3 = e1.getElementsByTagName(KEY);
                // System.out.println("title "+n.getLength());
                  for (int k = 0; k < n3.getLength(); k++) {

                      Element e2 = (Element) n3.item(k);
                //    System.out.println("node snippet value"+e2.getNodeName());
                     NamedNodeMap attributes2 = e2.getAttributes();

                     for (int a = 0; a < attributes2.getLength(); a++) 
                     { 
                        // HashMap<String, String> map = new HashMap<String, String>();
                           theAttribute = attributes2.item(a);
                           String s=theAttribute.getNodeValue();


                             map.put(KEY,s);
                            // menuItems.add(map);
                     }

                  }  

                  menuItems.add(map);
         }

    }

 // Adding menuItems to ListView

         adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_COST,KEY,KEY_SYMBOL }, new int[] {
                        R.id.name, R.id.desciption, R.id.cost,R.id.low,R.id.symbol }){


            };


        setListAdapter(adapter);


        // selecting single ListView item
         lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
            /*  String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
                String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
                String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
                */
                // Starting new intent
                /*Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(KEY_NAME, name);
                in.putExtra(KEY_COST, cost);
                in.putExtra(KEY_DESC, description);
                startActivity(in);*/
                 Toast.makeText(getApplicationContext(), "on clicked",Toast.LENGTH_LONG).show();
            }
        });
         // Create the listener for long item clicks
        OnItemLongClickListener itemLongListener = new OnItemLongClickListener() {


            public boolean onItemLongClick(AdapterView<?> parent, View v, final int position, long rowid) {
                final View view=v;
                // Store selected item in global variable 
                selectedItem = parent.getItemAtPosition(position).toString();
       //  Toast.makeText(getApplicationContext(), "select item"+selectedItem,Toast.LENGTH_LONG).show();

               AlertDialog.Builder builder = new AlertDialog.Builder(AndroidXMLParsingActivity.this);
                builder.setMessage("Do you want to remove " + "?");
                final int positionToRemove = position;
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {



                    public void onClick(DialogInterface dialog, int which) {
                          adapter.remove(selectedItem);
                          adapter.notifyDataSetChanged();

                        Toast.makeText(
                                getApplicationContext(),
                                selectedItem + " has been removed.",
                                Toast.LENGTH_SHORT).show();
                        dialog.cancel();
                    }


                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {


                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                // Create and show the dialog
                builder.show();

                // Signal OK to avoid further processing of the long click
                return true;
            }
        };



        getListView().setOnItemLongClickListener(itemLongListener);
}

}

it show the error in these line

   adapter.remove(selectedItem);
 adapter.notifyDataSetChanged();

it show
The method remove(String) is undefined for the type ListAdapter
add to cast adapter

please help me with example code!!!!
thanks in advance!!!!!!!!

  • 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-15T02:48:40+00:00Added an answer on June 15, 2026 at 2:48 am

    Change from ListAdapter SimpleAdapter.

    private SimpleAdapter adapter;
    

    Make menuItems global.

    And then you can do

    menuItems.remove(selectedItem);
    adapter.notifyDataSetChanged();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody please give me an example of a unidirectional @OneToOne primary-key mapping in
Can somebody please give me the code to be able to create a new
Can somebody please give me some links / tutorials to using SQLite file thatis
Can somebody please help me with the if statement and arrays. So for example
Can somebody give advice, how to test AJAX in Ruby on Rails? For example:
I can't find the Html.Image method in new MVC RC version. Please somebody give
I'm pretty new to c#. Can somebody please give me the right direction on
I'm trying to optimize this mysql query using EXPLAIN. Can somebody please help me
Can somebody please give an explanation to why I am getting this error with
Can somebody please post here an example how to host CLR in Delphi? I

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.