我正在尝试将 CheckedTextView 与 OnItemClickListener 一起使用,以便能够单击列表项并“检查”刚刚单击的行。我在SherlockListFragment中使用SimpleAdapter。然后,CheckedTextView状态将保存在SharedPreferences中,以供将来用户在应用程序中使用。
我已经全神贯注地寻找一个明确的解决方案,但我不明白。我的代码基于:Listview with checkedtextview。这是我收到的错误消息:
“方法setOnItemClickListener(new AdapterView.OnItemClickListener(){})未定义类型视图”
我导入android.view.View.OnClickListener,android.widget.AdapterView.OnItemClickListener,android.widget.CheckedTextView,android.widget.AdapterView。这是我的代码:
非常感谢你的帮助!
public class SettingsFragment extends SherlockListFragment实现了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;
}
}
以下是适配器使用的行xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/setting_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"/>
<LinearLayout
android:id="@+id/center_text_zone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckedTextView
android:id="@+id/setting_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#FFFFFF"
android:focusable="false" />
<TextView
android:id="@+id/setting_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout >
答案 0 :(得分:0)
方法setOnItemClickListener
属于AdapterView
类。
AdapterView
延伸View
。
但是,您的对象thelist
不是AdapterView
对象,而是View
对象。
将thelist
更改为AdapterView
。
如果需要,您可以暂时转发thelist
,如下所示:
(View) thelist = ...;
(View) thelist.someMethod(args);