开发者

Load ActivityList with pre-selected rows

I have a list of custom objects that I am loading into an ActivityList that allows multiple selections and displays the checkbox on the right side. Those custom objects contain a field named "enabled". When I load the data I want to scroll through the list of objects and check the checkbox for each row that represents an object that has the enable field marked true. Currently I have all of the records loading into the ActivityList as I want but I can not make any of the rows "checked" even though objects are marked as "enabled".

Th开发者_运维问答is is the code I am using to mark a row as checked

for (int i = 0; i < sourceList.length; i++) {
    DataSource d = sourceList[i];
    view.getChildAt(i).getClass().toString());
    CheckedTextView checkView = (CheckedTextView)view.getChildAt(i);
    checkView.setChecked(Boolean.parseBoolean(d.enabled));
}

I have put this code directly after calling setListAdapter and I have put it in the onContentChanged() function. However, in both places the rows are not displayed yet so the view.getChildAt(i) returns null so obviously the row does not get checked.

Can anyone tell me where I can put this code so that it will be executed after all rows have been added and displayed on the screen?

Thank you!

Originally I did not have a custom Adapter I was just using ArrayAdapter. In order to override the getView() method I created a custom Adapter and extended ArrayAdapter. I am still allowing the ArrayAdapter class to do most of the work but I am overriding getView(). Here is my code for getView()

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = super.getView(position, convertView, parent);

    if (convertView.getClass() == CheckedTextView.class){
        CheckedTextView checkView = (CheckedTextView)convertView;
        DataSource d = getItem(position);
        checkView.setText(d.getName());
        checkView.setChecked(Boolean.parseBoolean(d.enabled));
    }

    return convertView;
}

Even with this code none of the check boxes are being checked. The DataSource's name field is being set as the text but the setChecked() method does not seem to be working for me. I also tried hard coding that to be

checkView.setChecked(true);

That did not work for me either. Do you have any more ideas on what I might be doing wrong?

Thanks again!


Can you show your adapter code? The place to call setChecked is in your getView code in your adapter. It will be something like:

@Override
getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
            R.layout.my_list_row, parent, false);
    }
    CheckedTextView checkView = (CheckedTextView)
        convertView.findViewById(R.id.check_view);
    DataSource d = sourceList[i];
    checkView.setChecked(Boolean.parseBoolean(d.enabled));
    return convertView;
}

ListView doesn't really expose its children via getChildAt. The intended interface is via constructing and populating rows in getView.


What you need to do is call myList.setItemChecked(position, Boolean.parseBoolean(d.enabled)); in your getView method (where myList is an instance of ListView).

Also make sure you've called myList.setChoiceMode(int choiceMode) before setting your list adapter.

If you have Checkable views inside another view, then the containing view needs to implement Checkable as well for checkable items in lists to work properly (see CheckableRelativeLayout for instance)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜