我試圖以使用CheckedTextView與OnItemClickListener能夠點擊列表項和「檢查」剛單擊的行。我在SherlockListFragment中使用SimpleAdapter。 CheckedTextView狀態然後將被保存在SharedPreferences中以供將來用戶在應用程序中使用。使用CheckedTextView與setOnItemClickListener錯誤
我已經找遍各地找到一個清晰的解決方案,但我不明白。我的代碼基於:Listview with checkedtextview。以下是錯誤消息我得到:
「的方法setOnItemClickListener(新AdapterView.OnItemClickListener(){})是未定義的類型視圖」
我做進口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 >
我已經添加適配器使用的XML文件。 – JulienT