我在我的布局中收到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;
}
}
答案 0 :(得分:1)
在你的适配器中尝试这个,我通过扩展BaseAdapter来使用它:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row1, null);
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.add_food_item);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ItemInList element = list.get(position);
holder.checkbox.setChecked(element.isSelected());
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
element.setSelected(buttonView.isChecked());
if (element.isSelected()) {
array.add(element.getName());
} else {
if (position < array.size())
array.remove(position);
}
}
});
return convertView;
}
答案 1 :(得分:1)
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;
}
}
这只是一个例子。在某些地方可能存在语法错误。请检查它们。