我正在尝试获取已检查的值,但它没有显示值,我已经实现了自定义适配器,我没有得到如何更新位置
我的代码是..
public Add_member_adapter(Context a, ArrayList<ItemsModel> d) {
context = a;
data = d;
itemChecked = new boolean[data.size()];
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
public final Object getItem(int position) {
return data.get(getCount() - position - 1);
}
public final long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView Name, UserId_Fk;
CheckBox chkbox;
}
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("getview:", "position=" + position);
vi = convertView;
final ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.add_members_list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) vi.findViewById(R.id.Name);
holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
vi.setVisibility(View.GONE);
} else {
tempValues = null;
tempValues = (ItemsModel) data.get(position);
holder.Name.setText(tempValues.getItemName().toString());
holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
holder.chkbox.setChecked(false);
holder.chkbox.setChecked(itemChecked[position]);
holder.chkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked[position] = isChecked;
}
});
}
return vi;
}
我正在活动
中声明List setOnItemClickListenerlist.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckBox checkbox = ((CheckBox) view.findViewById(R.id.chkbox));
String Name = list.getItemAtPosition(position).toString();
}
});
答案 0 :(得分:3)
列表视图项内的任何可点击视图都会窃取列表视图onItemClickListener click。
如果要在复选框上捕获检查事件,则应使用CompoundButton.OnCheckedChangeListener而不是View.OnClickListener。
由于ListView上的convertView可重复使用,因此您必须意识到您的数据不准确。您必须创建一个数据集来保存您想要保留的值。
此代码来自您的适配器
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("getview:", "position=" + position);
vi = convertView;
final ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.add_members_list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) vi.findViewById(R.id.Name);
holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
vi.setVisibility(View.GONE);
} else {
tempValues = null;
tempValues = (ItemsModel) data.get(position);
holder.Name.setText(tempValues.getItemName().toString());
holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
holder.chkbox.setChecked(checkedHolder[position]);
holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedHolder[position] = isChecked;
}
});
}
return vi;
}
public boolean[] checkedHolder;
private void createCheckedHolder() {
checkedHolder = new boolean[getCount()];
}
在适配器的构造函数上调用createCheckedHolder。如果你想获得检查值,你可以从checkedHolder
获得在checkedHolder中,您可以获取用户选择的任何项目的位置,然后您可以调用活动中的以下代码
private ArrayList<String> getSelectedNames(){
List<String> names = new ArrayList<String>();
for (int i = 0; i < adapter.getCount(); i++) {
if(adapter.checkedHolder[i]){
//get all name values that checked by user
names.get(i).add(yourDataSet.getItemName().toString());
}
}
return names
}
答案 1 :(得分:0)
查看我的代码。我使用这种方式从listview获取选中的项目:
public class MovieAdapter extends ArrayAdapter<MovieVO>{
Context context;
List<MovieVO> movieList;
public MovieAdapter(Context context, int resource, List<MovieVO> objects) {
super(context, resource, objects);
this.context = context;
movieList = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row, null);
TextView tvMovieName = (TextView) convertView.findViewById(R.id.tvMovieName);
TextView tvRating = (TextView) convertView.findViewById(R.id.tvRating);
TextView tvGenre = (TextView) convertView.findViewById(R.id.tvGenre);
TextView tvYear = (TextView) convertView.findViewById(R.id.tvYear);
ImageView ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
CheckBox cbSelect = (CheckBox) convertView.findViewById(R.id.cbSelect);
if(movieList.get(position).isSelected())
{
cbSelect.setChecked(true);
}
else
{
cbSelect.setChecked(false);
}
cbSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
movieList.get(position).setSelected(true);
}
else
{
movieList.get(position).setSelected(false);
}
}
});
tvMovieName.setText(movieList.get(position).getName());
tvRating.setText(movieList.get(position).getRating());
tvGenre.setText(Arrays.toString(movieList.get(position).getGenre()).replace("[", "").replace("]", ""));
tvYear.setText(movieList.get(position).getYear());
Picasso.with(context).load(movieList.get(position).getImageUrl()).into(ivImage);
return convertView;
}
在这里,我根据项目检查添加true或false值并取消选中。如果希望列表中的所有选定值,则只需迭代列表并检查真假值。