4

我使用 List 和 Arrayadapter 将字符串放入 Listview 中。现在我想删除选定的项目。我尝试了以下代码,但效果不佳。我怎样才能解决这个问题?

这是我的代码:

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

这里有更多源代码:

    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

                }
            });

        }

    }

这是我的更新代码:

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

10 回答 10

6

尝试实现 onitemclickListener 并获取项目 id 并删除在 arrayadapter 中单击的项目,然后调用 adapter.notifyDataSetChanged();

于 2012-08-14T06:21:58.187 回答
2

试试这个...

        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();               
        }
    });
于 2015-12-28T20:13:05.967 回答
1

我认为您应该使用OnItemClickListener()listview以便当您选择/单击任何列表项时,您将获得所选项目的名称,然后您可以执行删除操作。

试试这个。

    mylist.OnItemClickListener(listenerOflistView);

private OnItemClickListener listenerOflistView = new OnItemClickListener() {
    private String getSelectedItemOfList;

    public void onItemClick(AdapterView<?> view, View view1, int pos,
            long arg3) {
        // TODO Auto-generated method stub

    getSelectedItemOfList = mylist.get(pos).toString(); // here you will get selected item name.
      }
    }

希望这会帮助你。

于 2012-08-14T06:33:40.190 回答
1

请参阅此代码,它可以帮助您了解如何删除列表视图中的项目。

private ArrayList<String> students = new ArrayList<>();
private ArrayAdapter<String> arrayAdapter;
private ListView myListView;
private int getSelectedIndex = -1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myListView = (ListView) findViewById(R.id.myListView);
    students.add("rahim");
    students.add("karim");
    students.add("sumon");
    students.add("rakib");
    students.add("porag");
    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, students);
    myListView.setAdapter(arrayAdapter);
    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "hello " + students.get(position), Toast.LENGTH_LONG).show();
            MainActivity.this.getSelectedIndex = position;
            for (int i = 0; i < myListView.getChildCount(); i++) {
                if(position == i ){
                    myListView.getChildAt(i).setBackgroundColor(Color.BLUE);
                }else{
                    myListView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                }
            }


        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);//Menu Resource, Menu
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.add:
            Toast.makeText(getApplicationContext(), "Item 1 Selected", Toast.LENGTH_LONG).show();
            return true;
        case R.id.delete:
            if(this.getSelectedIndex!=-1){
                students.remove(this.getSelectedIndex);
                this.getSelectedIndex = -1;

                arrayAdapter.notifyDataSetChanged();
                myListView.setAdapter(arrayAdapter);

            }
            return true;
        default:
            return false;
    }
}
于 2017-04-27T20:15:47.967 回答
0

我想说做到这一点的最好方法(真正最好的方法是自定义适配器)是在你的类中保留你的数组列表的副本。然后,当调用 delete 时,从您的数组列表副本中删除该项目并重新初始化列表视图。

于 2012-08-14T06:24:46.037 回答
0

我想告诉你的第一件事是你编写的代码是错误的......可能是你想要listview上的上下文菜单......

如果您获得所选项目的位置,那么您可以拥有

adapter.removeItem(adapter.getItem(position));
adapter.notifyDatasetChanged()

这可以帮助你吗

于 2012-08-14T06:28:18.740 回答
0

代替

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

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

delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

                 adapter.remove(getSelectedItemOfList );
adapter.notifydatasetChanged

        }
    });

检查这个

于 2012-08-14T09:54:08.147 回答
0

公共类 ListTest 扩展 Activity {

String selectedItem;
ArrayAdapter<String> adapter;
ArrayList<String> newList = new ArrayList<String>();
ListView l1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_test);

  l1 = (ListView) findViewById(R.id.lsvSign);

    newList.add("Android");
    newList.add("iOS");
    newList.add("Mac");
    newList.add("Windows");
    newList.add("Linux");;
    Collections.sort(newList);
    adapter = new new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,newList);
    l1.setAdapter(adapter);  


  l1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {            
         selectedItem = l1.getItemAtPosition(position).toString();           
       RemoveItem(selectedItem);

    }
});  
protected void RemoveItem(String item) {
    newList.remove(item);
    adp2.notifyDataSetChanged();
}   
}

}

于 2013-09-25T13:10:45.153 回答
0

尝试工作代码: listAdapter.remove(listAdapter.getItem(position)); mainListView.setAdapter( listAdapter );

于 2015-06-24T11:15:33.823 回答