Android - delete a row from list view on button click

29.4k views Asked by At

i am creating an application in which data is got from a barcode scanner which identifies from an id, and show detail from the database through a web service. i am currently using custom list which has a text view and a button, the data which i want to embed on the textview is displaying correctly...

my question is how do i delete a particular row from the list on the button click .

here is the custom adaptor code.

 public class MyCustomAdapter extends ArrayAdapter<String> 
     {
         public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<String> items) 
         {
             super(context, textViewResourceId, items);
         }

         @Override
         public View getView(int position, View convertView, ViewGroup parent)
         {

             LayoutInflater inflater = FusionMain.this.getLayoutInflater();
             View row = inflater.inflate(R.layout.list_row, parent, false);


             return row;
         }
     } // end MyCustomAdapter

what other function should i be creating in custom adapter ? i am new in android development.. if you need further more detail i can update the question or the code. kindly respond

4

There are 4 answers

0
Mark On

The main idea of the adapter is that you pass ArrayList items to it and they will be shown on the AdapterView.

getView method with position parameter will be run for each element currently shown on the list. When you scroll the list you will get more getView() calls. From getView method you have to return a view that will be shown on the list but it already has to be filled with DATA! for example:

@Override
     public View getView(int position, View convertView, ViewGroup parent)
     {

         LayoutInflater inflater = FusionMain.this.getLayoutInflater();
         View row = inflater.inflate(R.layout.list_row, parent, false);
         TextView myTextView = (TextView)row.findViewById(R.id.myTextviewId);
         myTextView.setText(items.get(position));

         return row;
     }

When you want the list to be changed you have to change the items object (for example remove single item from this ArrayList) and then you have to use myAdapter.notifyDatasetChanged() method. This will make your AdapterView (eg ListView) to update one more time and call getView method for all visible positions one more time.

1
akelix On

The simpliest way is to recreate Adapter.

public void onClick(View view){

   mItemsList.remove(numberToDelete);
   mListView.setAdapter(new MyCustomAdapter(this, mResId,mItemsList));
}
0
Davidjburgos On

To refresh your adapter, just remove the element of your ArrayList and call to

notifyDataSetChanged()

in your adapter.

I think you should watch this example:

http://www.vogella.com/articles/AndroidListView/article.html

0
Neeraj Singh On

Simple must need to remove item from list and call function notifyDataSetChanged

 ImageButton dltbutton = (ImageButton) row.findViewById(R.id.removebtn);
        dltbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myModel.remove(position);
                notifyDataSetChanged();
            }
        });

Check my answer below:-

https://stackoverflow.com/a/39243875/1733810