I have a list view that is populated with check boxes. The text on each box is set with the ListAdapter. When everything loads up I need a few of the check boxes to be checked by default and some of them to not be checked. I am able to call setChecked on the check boxes but it is not working. I believe this is because in the onCreate() the view is not visible yet. So I moved it to onResume() and onCreate() and so far it still does not work. It seems that they are still not yet loaded and on the screen yet.
private void markAllTags()//this will put a check mark on the ones that are already a tag of that question
{
String tags[] = mUserDb.fetchTagById(mQid);//These are the tags that are already associated with this question
for(int i = 0; i < this.getListAdapter().getCount(); i++)
{
View v = this.getListAdapter().getView(i, null, null);
//Log.w("NME", "I = " + this.getListAdapter().getView(i, null, null).findViewById(R.id.checkBox1));
if(v != null)
{
CheckBox cb = (CheckBox)v.findViewById(R.id.checkBox1);
String cbTags = "" + cb.getText();
for(int j = 0; j < tags.length; j++)
{
//Log.w("NME", "cbTag = " + cbTags + " tags[j] = " + tags[j]);
if(cbTags.equals(tags[j]))
{
cb.setChecked(true);
}
}
}
}
}
This is currently being called from onStart.
If I use listview.getchildAt() I receive a null value.
EDIT: There is no crash so nothing really coming up in logcat. So the problem is that when the final if statement is met it doesn’t mark it as checked.
EDIT 2: So I have tried a few more things. I linked the function above to a button and it still did not work. When I changed the line where View v is set. To this View v = mListView.getChildAt(i); It worked when I pressed the button. However it still does not work when the activity starts. The problem is now that v is null.
I have found a solution to my problem there may be a better way and I would love to hear it if there is. I used something I found in an answer to this When I call
markAllTags()insideonStart()I have surrounded it with this.also now my
markAllTags()function looks like this:As mentioned in my last edit I changed where I set
View vother than that it should be about the same.