0

My list fragment code is

public class LanguageFragment extends ListFragment {
String[] language_list = new String[] { "English", "Tamil" };
ListView lv;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.language_fragment, container,
            false);
    lv = (ListView) view.findViewById(android.R.id.list);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            inflater.getContext(), android.R.layout.simple_list_item_1,
            language_list) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (convertView == null) {
                LayoutInflater li = (LayoutInflater) getActivity()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = li.inflate(android.R.layout.simple_list_item_1, null);
            }               
            if (lv.isItemChecked(position)) {
                v.setBackgroundColor(Color.DKGRAY);
            } else {
                v.setBackgroundColor(0);
            }
            return super.getView(position, v, parent);
        }
    };
    setListAdapter(adapter);
    lv.setItemChecked(1, true);
    return view;
}
}

Here my second row(index 1) will highlighted in Color.DKGRAY color when I load the Fragment. So I used setChoiceMode. But this code set choice mode as second row. But it didn't highlighted in Color.DKGRAY color when I load the Fragment. While I click the list item only it will get highlighted in Color.DKGRAY color. I dont know what is problem. Help me friends.

Update#1

I think setItemChecked is didn't work in fragment or this is not right way to do it.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • did you check if there is some trouble that some counts start with 0 and some start with 1 as first value of a collection? – bofredo Aug 12 '13 at 13:37
  • I checked with both 0 and 1. Same problem friend. – Gunaseelan Aug 12 '13 at 13:39
  • One thing that puzzles me is why you're setting the background on an inflated view only to return the view from the `super` call? – user Aug 14 '13 at 07:49
  • @Luksprog No friend. To highlight the selected record. – Gunaseelan Aug 14 '13 at 09:24
  • if u mean u need to highlight the selected row with a particular color??...then use custom listitem selector for listview. – ASP Aug 14 '13 at 10:49
  • I don't really understand what you want, please explain short – dumazy Aug 14 '13 at 10:49
  • @dumazy I just want [this](http://stackoverflow.com/questions/16989662/android-listviewsetitemchecked-doesnt-work/16990047#16990047) option to work on ListFragment. – Gunaseelan Aug 14 '13 at 11:05

1 Answers1

0

Thank you so much friends. Task is successfully achieved by changing onCreateView method to onActivityCreated. Now the app works like a charm.

Thanks.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128