Julien TK
- Forum posts: 1
Jun 24, 2013, 3:32:18 AM via Website
Jun 24, 2013 3:32:18 AM via Website
Hi,
I'm trying to use a *CheckedTextView* with *OnItemClickListener* in order to be able to click on a list item and "check" the row that was just clicked. I'm using a SimpleAdapter in a SherlockListFragment. The CheckedTextView state will then be saved in SharedPreferences for future user usage in the app.
I have looked all over to find a clear solution, but I don't figure it out. Here is the error message I get:
*"The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type View"*
I do import android.view.View.OnClickListener, android.widget.AdapterView.OnItemClickListener,android.widget.CheckedTextView, android.widget.AdapterView. Here is my code:
Thanks a lot for your help!
public class SettingsFragment extends SherlockListFragment implements ActionBar.TabListener {
// Array of strings storing settings names
String[] settings_names = new String[] {
"Bluetooth",
"Speakerphone",
"test3",
"test4"
};
// Array of integers points to icons stored in /res/drawable-ldpi/
int[] settings_icons = new int[]{
R.drawable.device_access_bluetooth,
R.drawable.device_access_call,
R.drawable.device_access_alarms,
R.drawable.device_access_location_found
};
// Array of strings to store settings description
String[] settings_desc = new String[]{
"Activate Bluetooth connection",
"Enable Speakerphone",
"Enable ...",
"Enable ..."
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<4;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("set", settings_names[i]);
hm.put("desc", settings_desc[i]);
hm.put("icon", Integer.toString(settings_icons[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "icon","set","desc" };
// Ids of views in settingsrow_layout
int[] to = { R.id.setting_icon,R.id.setting_name,R.id.setting_desc};
// Instantiating an adapter to store each items
// R.layout.settingsrow_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.settingsrow_layout, from, to);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
// The list is identify by and ID in the xml file
final View thelist = container.findViewById(android.R.id.list);
thelist.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,int position,long id) {
View v = ((ViewGroup) thelist).getChildAt(position);
CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.setting_name);
ctv.setChecked(!ctv.isChecked());
}
});
LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.settingsfragment_layout,
container, false);
return mLinearLayout;
}
}
There CheckedTextView is a child in a linear layout.
I'm trying to use a *CheckedTextView* with *OnItemClickListener* in order to be able to click on a list item and "check" the row that was just clicked. I'm using a SimpleAdapter in a SherlockListFragment. The CheckedTextView state will then be saved in SharedPreferences for future user usage in the app.
I have looked all over to find a clear solution, but I don't figure it out. Here is the error message I get:
*"The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type View"*
I do import android.view.View.OnClickListener, android.widget.AdapterView.OnItemClickListener,android.widget.CheckedTextView, android.widget.AdapterView. Here is my code:
Thanks a lot for your help!
public class SettingsFragment extends SherlockListFragment implements ActionBar.TabListener {
// Array of strings storing settings names
String[] settings_names = new String[] {
"Bluetooth",
"Speakerphone",
"test3",
"test4"
};
// Array of integers points to icons stored in /res/drawable-ldpi/
int[] settings_icons = new int[]{
R.drawable.device_access_bluetooth,
R.drawable.device_access_call,
R.drawable.device_access_alarms,
R.drawable.device_access_location_found
};
// Array of strings to store settings description
String[] settings_desc = new String[]{
"Activate Bluetooth connection",
"Enable Speakerphone",
"Enable ...",
"Enable ..."
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<4;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("set", settings_names[i]);
hm.put("desc", settings_desc[i]);
hm.put("icon", Integer.toString(settings_icons[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "icon","set","desc" };
// Ids of views in settingsrow_layout
int[] to = { R.id.setting_icon,R.id.setting_name,R.id.setting_desc};
// Instantiating an adapter to store each items
// R.layout.settingsrow_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.settingsrow_layout, from, to);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
// The list is identify by and ID in the xml file
final View thelist = container.findViewById(android.R.id.list);
thelist.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,int position,long id) {
View v = ((ViewGroup) thelist).getChildAt(position);
CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.setting_name);
ctv.setChecked(!ctv.isChecked());
}
});
LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.settingsfragment_layout,
container, false);
return mLinearLayout;
}
}
There CheckedTextView is a child in a linear layout.
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.