我在android上使用listView小部件,並在預覽列表內容中選擇了「檢查列表」項目 基本上它是一個項目列表,我應該能夠檢查一些項目,當我做的項目旁邊的複選標記變得可見(這不是一個複選框,這是許多其他可檢查列表之間的區別) 我不知道如何使用它,我想知道至少我怎麼能檢查一些項目,也就是使複選標記可見,因爲當我點擊一個項目,它是可以點擊,但沒有任何反應......如何在android上設置檢查列表項目列表視圖
image of listview in simulator
這裏是我的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:rsb="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#fffefdff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="540dp"
android:weightSum="1"
android:id="@+id/linearLayoutPreferences"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:focusableInTouchMode="false"
android:divider="#ff080808"
android:dividerPadding="@dimen/activity_horizontal_margin"
android:showDividers="middle|beginning|end">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
tools:listitem="@android:layout/simple_list_item_checked"
android:clickable="true"
android:fastScrollAlwaysVisible="false"
android:choiceMode="multipleChoice"
android:contextClickable="false" />
</LinearLayout>
這裏是我的java文件
public class Popneighbourhood extends AppCompatActivity {
ListView listNeighbourhood;
String[] neighbourhood = new String[]{
"Alamo Square/NOPA", "Castro/Upper Market", "Central Richmond", "Cole Valley/Ashbury Heights", "Downtown/Civic/Van Ness", "Duboce Triangle",
"Financial District", "Glen Park", "Haight Ashbury", "Hayes Vallez", "Ingleside/SFSU/CCSF", "Inner Richmond",
"Inner Sunset/UCSF", "Jordan Park/Laurel Heights", "Laurel Heights/Presidio", "Lower Haight", "Lower Nob Hill", "Lower Pac Heights",
"Marina/Cow Hollow", "Mission Bay", "Mission District", "Nob Hill", "Noe Valley", "North Beach/Telegraph Hill",
"Oakland North/Temescal", "Pacific Heights"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popneighbourhood);
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.logofrontdoor);
listNeighbourhood = (ListView) findViewById(R.id.listView);
//android.R.layout.simple_list_item_1 est une vue disponible de base dans le SDK android,
//Contenant une TextView avec comme identifiant "@android:id/text1"
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Popneighbourhood.this,
android.R.layout.simple_list_item_1, neighbourhood);
listNeighbourhood.setAdapter(adapter);
謝謝你,我已經改變了屬性,在模擬器上,我現在可以選擇不同的項目,我該如何處理這些項目,我想知道如何獲得所選項目的計數......函數getCheckedItemCount不會返回任何東西,看起來好像檢查標記只是圖像的存在,但它們不被識別......我對此很新,而且我很難用這個列表視圖 – Arthur
I'好吧,沒關係:)謝謝你的幫助 – Arthur