Reputation: 25
I have an arrayList in Custom Adapter, consisting of some strings and checkbox. and i want to set checkbox checked automatically with certain condition, but how to set the code so the checkbox checked automatically.
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView == null)
vi = inflater.inflate(R.layout.list_row, null);
TextView no = (TextView)vi.findViewById(R.id.txt_no);
TextView code = (TextView)vi.findViewById(R.id.txt_code);
TextView name = (TextView)vi.findViewById(R.id.txt_asset);
TextView dept = (TextView)vi.findViewById(R.id.txt_dept);
CheckBox status = (CheckBox)vi.findViewById(R.id.cb_stat);
HashMap<String, String> asset = new HashMap<String, String>();
asset = data.get(position);
//Setting all values in listview
no.setText(asset.get(ListLocationActivity.KEY_NO));
code.setText(asset.get(ListLocationActivity.KEY_CODE));
name.setText(asset.get(ListLocationActivity.KEY_NAME));
dept.setText(asset.get(ListLocationActivity.KEY_DEPT));
return vi;
}
This is my condition when i get the result, i want set checked the checkbox which the result suitable with the code in arraylist
for (int i = 0; i < assetList.size(); i++) {
if (assetList.get(i).get(KEY_CODE).equals(scanAsset)){
//set checked in certain list asset
//(Here i confused how i can set the checkbox in list suitable with the code asset)
Log.i("Asset code is ", scanAsset);
}
}
this code is work fine without set value in checkbox, Anyone can help me to solve this case, please . . . . Thank you before
Upvotes: 0
Views: 2377
Reputation: 6605
Create a model object (here called AssetsWrapper) and set a boolean inside it.
// Model wrapper class
class AssetsWrapper {
HashMap<String, String> mAssets;
boolean mBoolean;
}
// Your code that checks the CheckBox
for (int i = 0; i < assetList.size(); i++) {
if (assetList.get(i).get(KEY_CODE).equals(scanAsset)){
assetList.get(i).mBoolean = true;
}
adapter.notifyDataSetChanged();
}
// YOUR ADAPTER CODE --------------------------------------------
// Constructor (notice that the ArrayList has changed)
public LazyAdapter(Activity a, ArrayList<AssetsWrapper> d) {
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
// ...
CheckBox status = (CheckBox)vi.findViewById(R.id.cb_stat);
AssetsWrapper aw = getItem(position);
// Here, the adapter reads the value from the model object.
status.setChecked(aw.mBoolean);
// ...
return vi;
}
Upvotes: 0
Reputation: 2295
I'd recommend using actual objects instead of a HashMap but that's outside the scope of this question
I'd adjust your getView
call to add this line before returning
status.setChecked(asset.containsKey(ListLocationActivity.KEY_CHECKED));
obviously create a new static key for checked, not sure where that's defined
then once get your scan result you referenced this above so I'll modify it:
for (int i = 0; i < assetList.size(); i++) {
HashMap<String, String> asset = assetList.get(i);
if (asset.get(KEY_CODE).equals(scanAsset)){
asset.put(KEY_CHECKED, "Checked");
Log.i("Asset code is ", scanAsset);
}
}
adapter.notifyDataSetChanged();
so what we did is we created a new property for your adapter to key off, then if the asset you're currently building a view for in the adapter has that key then we want to check the check box, otherwise we want to not check it.
Upvotes: 1