gpt4 book ai didi

java - 如何使用复选框删除 ListView 项目?

转载 作者:行者123 更新时间:2023-11-30 08:44:36 25 4
gpt4 key购买 nike

我在我的布局中获取 ListView ...当我在复选框中选择项目并单击删除按钮时,我不知道如何从 ListView 中删除多个项目。我正在添加一段代码。

public class classname extends Activity implements OnItemSelectedListener {

ListView lv;
ArrayList<x_obj> x_list = new ArrayList<x_obj>();
TextView delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_x);
del_txt = (TextView) findViewById(R.id.del_txt);

del_txt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
"code for delete selected items"

}
});
}
public OrderAdapter(Context context, int row_layout,
ArrayList<x_obj> items) {
super(context, row_layout, items);
this.items = items;
}

@Override
public View getView(final int position, View convertview, ViewGroup parent) {
View v = convertview;

LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_layout, null);
final x obj = items.get(position);

TextView txt_name = (TextView) v.findViewById(R.id.txt_name);
CheckBox checkBox1=(CheckBox)findViewById(R.id.checkBox1);

return v;
}
}

最佳答案

public class classname extends Activity implements OnItemSelectedListener {

ListView lv;
ArrayList<x_obj> new_al=new ArrayList<x_obj>();
ArrayList<x_obj> x_list = new ArrayList<x_obj>();
TextView delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_x);
del_txt = (TextView) findViewById(R.id.del_txt);

del_txt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
"code for delete selected items"

}
});
}
public OrderAdapter(Context context, int row_layout,
ArrayList<x_obj> items) {
super(context, row_layout, items);
this.items = items;
}

@Override
public View getView(final int position, View convertview, ViewGroup parent) {
View v = convertview;

LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_layout, null);
final x obj = items.get(position);

TextView txt_name = (TextView) v.findViewById(R.id.txt_name);
CheckBox checkBox1=(CheckBox)findViewById(R.id.checkBox1);
if(checkBox1.isChecked())
{
new_al.add(x_list.get(position));//add the element into arraylist

}
btn_clear.setOnItemClickListener(new Listener()
{

public void onClick(View v)
{

x_list.clear();
x_list.add(new_al);
}
}
);
notifyDataSetChanged();
return v;

}
}

这只是一个例子,有些地方可能存在语法错误,请检查。

关于java - 如何使用复选框删除 ListView 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33706467/

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