0

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

5
  • you're trying to just do status.setChecked(true)? Commented Feb 18, 2014 at 18:43
  • @Eluvatar i can't just do status.setChecked(true) if i do that all of the checkbox in array list will be checked. i need just suitable with my result and code in my list will be checked. Commented Feb 18, 2014 at 18:45
  • Check this answer Commented Feb 18, 2014 at 18:47
  • I don't understand what you mean, where is the result coming from? you should be setting checked in getView then just call notifyDataSetChanged on the adapter to rebuild after you've changed the condition that causes setChecked to be true in getView Commented Feb 18, 2014 at 18:47
  • 1
    @eluvatar hmm i'm using scan barcode to get the result, so i don't show the code, sorry. the result i mean in 'scanAsset'. i don't want click the checkbox to set checked, but i want the checkbox set checked automatically when the result and contents in my code same. Commented Feb 18, 2014 at 18:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow it is work. Thank you very much it's solve my case, thanks again eluvatar. You're the best.
Glad I could help, just a note because you're new to SO, usually when you mark a question as answered you also up-vote it with the up arrow near the green check.
yes i'm new on SO, i want vote up your answer but, i still don't have enough rep to vote-up. so i still can't vote up your answer.
0

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;
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.