私はリストビューのチェックボックスの数を制限したいと思います。私は、Checked Checkboxを持つグローバル配列を持っています、そして、そのサイズが与えられた限界より大きいとき、チェックされていないすべてのチェックボックスを無効にしたいと思います。カスタムアダプタのチェックボックスの数を制限するにはどうすればよいですか?
最後に許可されたチェックボックスがオンになるとすぐに、チェックされていないチェックボックスを無効にしたいので、チェックボックスonclickハンドラで行う必要があります。そうしないと、ビューが再度呼び出される前にビューが更新されません。そうですか?
チェックされていないものを無効にするには、私のリストビューのすべてのチェックボックスにアクセスする必要があります。どのようにして、チェックボックスonclickハンドラからビューをリフレッシュすることができますか?
これは、これを達成するための最良の方法ではない可能性がありますので、ご意見をいただければ幸いです。
ありがとうございました。
public class FriendAdapter extends SimpleCursorAdapter implements OnClickListener {
private Context mContext;
private int mLayout;
private Cursor mCursor;
private int mNameIndex;
private int mIdIndex;
private LayoutInflater mLayoutInflater;
private final ImageDownloader imageDownloader = new ImageDownloader();
private final class ViewHolder {
public TextView name;
public ImageView image;
public CheckBox checkBox;
}
public FriendAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mCursor = c;
this.mNameIndex = mCursor.getColumnIndex(WhipemDBAdapter.KEY_NAME);
this.mIdIndex = mCursor.getColumnIndex(WhipemDBAdapter.KEY_FB_ID);
this.mLayoutInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.contact_name);
viewHolder.image = (ImageView) convertView.findViewById(R.id.contact_pic);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
viewHolder.checkBox.setOnClickListener(this);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
String name = mCursor.getString(mNameIndex);
String fb_id = mCursor.getString(mIdIndex);
boolean isChecked = ((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id);
viewHolder.name.setText(name);
imageDownloader.download("http://graph.facebook.com/"+fb_id+"/picture", viewHolder.image);
viewHolder.checkBox.setTag(fb_id);
viewHolder.checkBox.setChecked(isChecked);
}
return convertView;
}
@Override
public void onClick(View v) {
CheckBox cBox = (CheckBox) v;
String fb_id = (String) cBox.getTag();
if (cBox.isChecked()) {
if (!((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
((GlobalVars) mContext.getApplicationContext()).addSelectedFriend(fb_id);
} else {
if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
((GlobalVars) mContext.getApplicationContext()).removeSelectedFriend(fb_id);
}
int numSelectedCheckboxes = ((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id);
if(numSelectedCheckBoxes > 6)
{
/*
How can I access all the checkboxes here to disable the unckecked ones?
*/
}
}
}
ありがとうございます! notifyDatasetChanged()メソッドはFriendAdapter型のために未定義です。 – jul
Typo:http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged() –
申し訳ありません...私の目で多くのコーディングする。ありがとう。 – jul