Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to remove the selected item from Listview

Tags:

android

I have used a List and an Arrayadapter to put strings in a Listview. Now I want to remove the selected Item. I have tried the following code, but it's not working well. How can I fix this?

Here's my code:

TextView t1;
String[] temp;
mylist = (ListView) findViewById(R.id.list);
final List<String> wordList = Arrays.asList(temp);
adapter = new ArrayAdapter<String>(this,
                                   android.R.layout.simple_list_item_multiple_choice, 
                                   wordList);
delete.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
       int index = mylist.getSelectedItemPosition();
       int count=mylist.getCount();
       for(int i=0;i<=count;i++) {
           if (index >= 0) {
               t1.setText(wordList.remove(index));
           }
           adapter.notifyDataSetChanged();
       } 
   }
});

Here's more source code:

    public class Edit extends Activity {
        SharedPreferences sharedpref;
        ListView mylist;
        String[] temp;
        String name, 
        ArrayAdapter<String> adapter;
        Button save, delete, cancel;
        TextView t1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.edit);
            mylist = (ListView) findViewById(R.id.list);
            cancel = (Button) findViewById(R.id.cancel1);
            save = (Button) findViewById(R.id.save1);
            delete = (Button) findViewById(R.id.delete1);
            t1=(TextView)findViewById(R.id.textView1);
            sharedpref = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            name = sharedpref.getString("Visible_selected", "");

            String delimiter = "\n";
            temp = name.split(delimiter);

            mylist.setItemsCanFocus(true);
            mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
             final List<String> wordList = Arrays.asList(temp);  

            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_multiple_choice, wordList);
            mylist.setAdapter(adapter);
            save.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                }
            });

            delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    mylist.setOnItemClickListener(new OnItemClickListener() {
                        private String getSelectedItemOfList;
                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int arg2, long arg3) {
                            // TODO Auto-generated method stub
                             getSelectedItemOfList = sortedList.get(arg2).getStr_movieParam();


                }
            });

            cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });

        }

    }

Here's my updates code:

enter code here

delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
         adapter.remove(getSelectedItemOfList);
         adapter.notifyDataSetChanged();
            }
        });
        mylist.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                SparseBooleanArray checkedPositions =mylist.getCheckedItemPositions();
                if(checkedPositions.get(arg2)==true){
                getSelectedItemOfList = (String) mylist.getItemAtPosition(arg2);
                t1.append(getSelectedItemOfList.toString()+"\n");}
            }
        });
like image 557
user1580945 Avatar asked Dec 27 '22 19:12

user1580945


2 Answers

Try to implement the onitemclickListener and get the item id and delete the item clicked in the arrayadapter and next call adapter.notifyDataSetChanged();

like image 109
G_S Avatar answered Dec 29 '22 09:12

G_S


Try this...

        mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position,
                                long arg3) {
            String value = (String) adapter.getItemAtPosition(position);
            adapter.remove(value);
            adapter.notifyDataSetChanged();               
        }
    });
like image 38
Apollon Avatar answered Dec 29 '22 07:12

Apollon