Issue
I am attempting to select an item within a list Fragment onCreateView
. I do this as follows: listView.setItemChecked(position, true);
. Unfortunately the view is not activated, as in my ArrayAdapter
getView
is never called so I am unable to modify the checked item. Any suggestions or links are appreciated!
Fragment onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout= inflater.inflate(
R.layout.fragment, container, false);
mListView= (ListView) layout.findViewById(R.id.navigation_list);
mListView.setAdapter(new Adapter(
this,
R.layout.list_item,
titles));
mListView.setItemChecked(mCurrentSelectedPosition, true);
return layout;
}
ArrayAdapter
public class Adapter extends ArrayAdapter<String> {
private Activity mActivity;
private int mResource;
public Adapter(Fragment fragment, int resource, ArrayList<String> titles) {
super(fragment.getActivity(), resource, titles);
mActivity = fragment.getActivity();
mResource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if(convertView == null){
convertView = mActivity.getLayoutInflater().inflate(mResource, parent, false);
holder = new ViewHolder();
holder.title = (TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(getItem(position));
holder.title.setTypeface(null, convertView.isActivated() ? Typeface.BOLD : Typeface.NORMAL);
return convertView;
}
private class ViewHolder{
private TextView title;
}
}
EDIT The following code works fine for some reason in detecting an activation, why is it that my custom adapter won't work?
listView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
new String[]{
"ONE",
"TWO"
}));
Solution
You need to set the choiceMode
before the setItemChecked
can be used within your ListView
sample:
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView.setItemChecked(mCurrentSelectedPosition, true);
EDIT:
Also call mListView.notifyDataSetChange();
when you want to refresh the views from the listview after you checked the items.
EDIT:2
create a class
public class StringWrapper{
public String string;
public boolean isBold = false;
}
use that class as a wrappter to String
and change this
change this title.setTypeface(null, title.isActivated() ? Typeface.BOLD : Typeface.NORMAL);
to
title.setTypeface(null, titles.get(position).isBold ? Typeface.BOLD : Typeface.NORMAL);
and change this: mListView.setItemChecked(mCurrentSelectedPosition, true);
to
titles.get(mCurrentSelectedPosition).isBold = true
mListView.notifyDataSetChange();
Answered By - Rod_Algonquin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.