gpt4 book ai didi

java - 知道 ListView 中单击的复选框项吗?

转载 作者:行者123 更新时间:2023-12-02 06:24:26 28 4
gpt4 key购买 nike

我有一个自定义 ListView ,其中包含一些元素和一个复选框。当我点击一个按钮时。我想知道已检查的元素的位置。下面是我的代码

public class Results extends ListActivity implements OnClickListener{
String[] donorName,donorPhone;
int totNumber;
Button callBut;
ListView listView;
List<RowItem> rowItems;
public static void main(String[] args) {
// TODO Auto-generated method stub

}

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
Intent intent = getIntent();
donorName = intent.getStringArrayExtra("name");
donorPhone = intent.getStringArrayExtra("phone");
totNumber = intent.getExtras().getInt("totDonors");
callBut = (Button)findViewById(R.id.callBut);
callBut.setOnClickListener(this);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < totNumber; i++) {
RowItem item = new RowItem(donorName[i], donorPhone[i]);
rowItems.add(item);
}

ListAdapter adapter = new MySimpleArrayAdapter(this,
R.layout.list_item, rowItems);
setListAdapter(adapter);


};
///////////////////////////////////////////////////////////////////////////////////////////
public static class MySimpleArrayAdapter extends ArrayAdapter<RowItem> implements OnCheckedChangeListener {


Context context;
static List<RowItem> donorList = new ArrayList<RowItem>();

public MySimpleArrayAdapter(Context context, int resourceId,
List<RowItem> donorList) {
super(context, resourceId, donorList);
this.context = context;
this.donorList = donorList;
}
private class ViewHolder {
Button donorCall,exp;
TextView donorName;
TextView donorPhone;
CheckBox chkBox;

}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.donorName = (TextView) convertView.findViewById(R.id.donorName);
holder.donorPhone = (TextView) convertView.findViewById(R.id.donorPhone);
holder.donorCall = (Button) convertView.findViewById(R.id.donorCall);
holder.chkBox = (CheckBox) convertView.findViewById(R.id.chkBox);
convertView.setTag(holder);

}

else
holder = (ViewHolder) convertView.getTag();

holder.donorPhone.setText(rowItem.getdonorPhoneS());
holder.donorName.setText(rowItem.getdonorNameS());
holder.chkBox.setTag(position);
holder.chkBox.setOnCheckedChangeListener(this);
holder.donorCall.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Button Clicked",position+"");
Intent startCall = new Intent(Intent.ACTION_CALL);

startCall.setData(Uri.parse("tel:" + rowItem.getdonorPhoneS()));
context.startActivity(startCall);
}

});
return convertView;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int position = (Integer) buttonView.getTag();
if (isChecked) {
donorList.get(position).setSelected(true);
Log.d("Tag",donorList.get(position).isSelected()+"");
} else {
buttonView.setSelected(false);
Log.d("Unchecked",isChecked+"");
}
notifyDataSetChanged();
}

}
////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String msgRecipient;
Log.d("MSg","Button Clicked");
for (int x = 0; x<totNumber;x++){
if(MySimpleArrayAdapter.donorList.get(x).isSelected()){
Log.d("position Checked",x+"");
}
else
Log.d("position UnChecked",x+"");
}
}
}

当我单击某个项目上的复选框时,Log 中的结果为 true。但是当我单击按钮时,所有元素都显示为未选中状态。

最佳答案

您忘记在 getView 内设置复选框的选中状态,因此如果您向下/向上滚动,您将显示旧的复选框而不更新。

您需要做的是拥有一组整数(或稀疏IntArray,这更好),在选中复选框时将项目位置添加到其中,并在取消选中它们时将其删除。

为了获得所有选中的复选框,只需使用这组整数...

关于java - 知道 ListView 中单击的复选框项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20692598/

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