我使用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");}
}
});
答案 0 :(得分:5)
尝试实现onitemclickListener并获取item id并删除在arrayadapter中单击的项目,然后调用adapter.notifyDataSetChanged();
答案 1 :(得分: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.
}
}
希望这会对你有所帮助。
答案 2 :(得分:1)
试试这个......
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();
}
});
答案 3 :(得分:0)
我想说最好的方法(最好的方法是自定义适配器)是保留你班级中项目的arraylist的副本。然后当调用delete时,将该项目替换为arraylist的副本并重新初始化listview。
答案 4 :(得分:0)
首先我要告诉你,你写的代码是错误的...... 可能你想要listview上的上下文菜单..
如果您获得所选项目的位置,那么您可以
adapter.removeItem(adapter.getItem(position));
adapter.notifyDatasetChanged()
这可以帮到你
答案 5 :(得分: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
}
});
检查一下
答案 6 :(得分:0)
答案 7 :(得分: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();
}
}
}
答案 8 :(得分:0)
尝试使用代码:listAdapter.remove(listAdapter.getItem(position));
mainListView.setAdapter( listAdapter );
答案 9 :(得分:0)
请参阅此代码,以帮助您了解如何删除listview中的项目。
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;
}
}