gpt4 book ai didi

java - 从 ListView 中删除所选项目不起作用

转载 作者:行者123 更新时间:2023-11-30 02:34:57 26 4
gpt4 key购买 nike

所以我有一个 ListView 并试图能够删除 ListView 中的选定项目。到目前为止,这是我的代码:

public class MainActivity extends Activity {

Button button1;
Button button3;
Button button2;
Button button4;
ListView listView1;
ListView listView2;
Spinner s1;
String text;
Integer i;
SparseBooleanArray checkedItemPositions ;


ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter2;
int itemPos;
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button2 = (Button) findViewById(R.id.button2);
s1 = (Spinner) findViewById(R.id.spinner1);
listView1 = (ListView) findViewById(R.id.listView1);
listView2 = (ListView) findViewById(R.id.listView2);



adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, list);
listView1.setAdapter(adapter);

button2.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

String text = s1.getSelectedItem().toString();
list.add(text);
adapter.notifyDataSetChanged();



}});

adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, list2);
listView2.setAdapter(adapter2);

button1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

String text = s1.getSelectedItem().toString();
list2.add(text);
adapter2.notifyDataSetChanged();



}});

button3.setOnClickListener(new View.OnClickListener() { //part to remove item
@Override
public void onClick(View v) {
/** Getting the checked items from the listview */
SparseBooleanArray checkedItemPositions = listView1.getCheckedItemPositions();


for(int i=itemPos-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
adapter.notifyDataSetChanged();
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();

}
});



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

view.setSelected(true);

itemPos = position;

}});
listView2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

view.setSelected(true);

itemPos = position;

}
});}}

在 xml 中定义的 ListView 是这样的:

<ListView
android:id="@+id/listView1"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/textView2"
android:choiceMode="singleChoice"
android:background="@drawable/bg_key" > >
</ListView>

但是,当我单击删除时,它只是取消标记该项目,根本不会将其从列表中删除。我不知道如何解决这个问题。我也是 android/java 的初学者。

谢谢!

最佳答案

您还应该从列表中删除该项目:

adapter.remove(list.get(i));
list.remove(i);
adapter.notifyDataSetChanged();

一般来说,它是有效的。但是,您可以使用一个技巧来创建适配器并再次添加它。

...
list.remove(i);
//adapter.notifyDataSetChanged();
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_single_choice, list);
listView1.setAdapter(adapter);

关于java - 从 ListView 中删除所选项目不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26724782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com