简体   繁体   中英

Android listview item deletion changes its position

Hello I am working with android list view . I have a custom list view and I am looking to delete on long click of list view item. I used base adapter to set array list data in list view. Now the problem is that when I am looking to delete an item in the list view another item get deleted.How can I solve this.Please help me thanks in advance :)

 l1.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id) {
                // TODO Auto-generated method stub



                AlertDialog.Builder builder = new AlertDialog.Builder(ViewList.this);

                ListView modeList = new ListView(ViewList.this);
                String[] stringArray = new String[] { "Edit", "Delete" };
                ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(ViewList.this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
                modeList.setAdapter(modeAdapter);

                builder.setView(modeList);
                final Dialog dialog = builder.create();

                modeList.setOnItemClickListener(new OnItemClickListener() {    
                    public void onItemClick(AdapterView<?> parent,View view,final int position,long id) {


                        switch (position) {
                        case 0:Toast.makeText(getApplicationContext(), "edit", 5000).show();
                        dialog.dismiss();break;
                        case 1: Toast.makeText(getApplicationContext(), "delete", 5000).show();


                        new AlertDialog.Builder(ViewList.this)
                        .setTitle("Delete entry")
                        .setMessage("Are you sure you want to delete this entry?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) { 
                                Toast.makeText(getApplicationContext(), "deleted", 5000).show();

                                date_array.remove(position);
                                day_array.remove(position); 
                                month_array.remove(position);
                                sent_array.remove(position);

                                 l1.setAdapter(new dataListAdapter(month_array,date_array,day_array,sent_array));
                            }
                         })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) { 
                                Toast.makeText(getApplicationContext(), "cancelled", 5000).show();
                            }
                         })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                         .show();
                        dialog.dismiss();
                        break;

                        default: System.out.println("Sunday");break;
                    }

                    }
                });


                 dialog.show();
                 Toast.makeText(getApplicationContext(), "Long clicked at"+pos, 5000).show();

                return true;
            }
        });  

Update I can't delete the top most item.When I delete another item, the item just below the topmost item get deleted

position is the position of your "delete" button, so is always 1, which would correspond to the second item in your arrays. pos is the index for which you want to delete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM