Android列表视图项删除更改其位置

时间:2014-12-11 11:16:07

标签: android listview dialog

您好我正在使用android列表视图。我有一个自定义列表视图,我希望删除长按列表视图项目。我使用基本适配器在列表视图中设置数组列表数据。现在的问题是,当我想要删除列表视图中的项目时,另一个项目会被删除。如何解决这个问题。请提前帮助我:)

 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;
            }
        });  

更新我无法删除最重要的项目。当我删除其他项目时,最顶层项目下方的项目会被删除

1 个答案:

答案 0 :(得分:1)

位置是“删除”按钮的位置,因此始终为1,这将对应于数组中的第二项。 pos 是您要删除的索引。