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.