1

我正在尝试将CheckedTextViewOnItemClickListener一起使用,以便能够单击列表项并“检查”刚刚单击的行。我在 SherlockListFragment 中使用 SimpleAdapter。CheckedTextView 状态随后将保存在 SharedPreferences 中,以供未来用户在应用程序中使用。

我已经到处寻找一个明确的解决方案,但我不明白。我的代码基于:Listview with checkedtextview。这是我收到的错误消息:

“方法 setOnItemClickListener(new AdapterView.OnItemClickListener(){}) 未为 View 类型定义”

我确实导入了 android.view.View.OnClickListener、android.widget.AdapterView.OnItemClickListener、android.widget.CheckedTextView、android.widget.AdapterView。这是我的代码:

非常感谢你的帮助!

公共类 SettingsFragment 扩展 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 >
4

1 回答 1

0

该方法setOnItemClickListener属于AdapterView该类。 AdapterView延伸View

但是,您的对象thelist不是AdapterView对象,而是View对象。更改thelistAdapterView

如果以后需要,您可以暂时向下转换thelist,如下所示:

(View) thelist = ...;
(View) thelist.someMethod(args);
于 2013-06-24T00:51:28.953 回答