如何在android中删除复选框选择项并显示在listview底部
我正在列表视图中显示数据,如下所示 标题1 复选框项目1 复选框项目2 复选框项目3 标题2 复选框项目4 复选框项目5 复选框项目6 我是否可以删除复选框选择中的项目,并在列表视图底部显示所选项目 我尝试了objects.removeposition和notifydatasetchanged,但它将删除标题中的项目 谁能告诉我如何解决这个问题 代码如何在android中删除复选框选择项并显示在listview底部,android,android-listview,Android,Android Listview,我正在列表视图中显示数据,如下所示 标题1 复选框项目1 复选框项目2 复选框项目3 标题2 复选框项目4 复选框项目5 复选框项目6 我是否可以删除复选框选择中的项目,并在列表视图底部显示所选项目 我尝试了objects.removeposition和notifydatasetchanged,但它将删除标题中的项目 谁能告诉我如何解决这个问题 代码 您需要跟踪列表中的哪些复选框与哪些标题关联,以及当该标题下不再有复选框时,也可以删除它。是的,我可以这样做,但我可以动态地这样做,就像选择删除项目
您需要跟踪列表中的哪些复选框与哪些标题关联,以及当该标题下不再有复选框时,也可以删除它。是的,我可以这样做,但我可以动态地这样做,就像选择删除项目和如何删除标题一样?您似乎没有任何将复选框项目与标题项目关联的内容。它们有一个区别于其类型的名称,但该名称不会将单个类型与其应关联的项相关联。因此,您需要在检查更改侦听器中执行一些逻辑,即如果getItemViewTypeposition-1.equalSecond&&getItemViewTypeposition+1.equalSecond,则删除位置-1中的项,该位置将是您的标题。并不是说这是最好的方法,只是我的想法。ok@zgc7009会尝试类似的方法。
import java.util.ArrayList;
import java.util.List;
import com.hb.views.PinnedSectionListView.PinnedSectionListAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter implements PinnedSectionListAdapter {
MainActivity ctx;
LayoutInflater lInflater;
List<Contact> objects;
public static final int FIRST = 0;
public static final int SECOND = 1;
ListAdapter(MainActivity context, List<Contact> contacts) {
ctx = context;
objects = contacts;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
public void refill(List<Contact> events) {
objects.clear();
objects.addAll(events);
notifyDataSetChanged();
}
@Override
public Contact getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int viewType = getItemViewType(position);
if (viewType == FIRST) {
LayoutInflater inflater = (LayoutInflater) ctx
.getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, null);
holder = new ViewHolder();
holder.title = (TextView) convertView
.findViewById(R.id.tvDescr);
holder.tvprice = (TextView) convertView
.findViewById(R.id.tvPrice);
holder.checkbox=(CheckBox) convertView.findViewById(R.id.cbBox);
//CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.title.setText(getItem(position).getHeader_id());
holder.tvprice.setText(getItem(position).getName());
holder.checkbox.setOnCheckedChangeListener(myCheckChangList);
holder.checkbox.setTag(position);
holder.checkbox.setChecked(getItem(position).isChecked());
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ctx, "Clicked", Toast.LENGTH_SHORT).show();
}
});
} else if (viewType == SECOND) {
convertView = lInflater.inflate(R.layout.section, parent, false);
((TextView) convertView.findViewById(R.id.textView1))
.setText(objects.get(position).getName());
}
return convertView;
}
@Override
public int getItemViewType(int position) {
String type = objects.get(position).getName();
if (type.equals("1")) {
return FIRST;
} else {
return SECOND;
}
}
static class ViewHolder {
TextView title;
TextView tvprice;
CheckBox checkbox;
}
@Override
public int getViewTypeCount() {
return 2;
}
Contact getProduct(int position) {
return ((Contact) getItem(position));
}
ArrayList<Contact> getBox() {
ArrayList<Contact> box = new ArrayList<Contact>();
for (Contact p : objects) {
if (p.box)
box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
View row = (View) buttonView.getParent();
if (isChecked) {
objects.remove(getProduct((Integer)buttonView.getTag()));
ArrayList<Contact> box = new ArrayList<Contact>();
Contact c=getProduct((Integer)buttonView.getTag());
c.box=isChecked;
ctx.deleteitem(c);
notifyDataSetChanged();
row.setBackgroundResource(android.R.color.holo_green_light);
} else {
row.setBackgroundResource(android.R.color.transparent);
}
}
};
@Override
public boolean isItemViewTypePinned(int viewType) {
if (viewType == SECOND) {
return true;
} else {
return false;
}
}
}