2016-07-28 125 views
0

我在android上使用listView小部件,並在預覽列表內容中選擇了「檢查列表」項目 基本上它是一個項目列表,我應該能夠檢查一些項目,當我做的項目旁邊的複選標記變得可見(這不是一個複選框,這是許多其他可檢查列表之間的區別) 我不知道如何使用它,我想知道至少我怎麼能檢查一些項目,也就是使複選標記可見,因爲當我點擊一個項目,它是可以點擊,但沒有任何反應......如何在android上設置檢查列表項目列表視圖

image of listview in simulator

image of listview in editor

這裏是我的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); 

回答

0

//適配器代碼,我只是把從我的項目適配器來源之一,注意到ImageView的, iv_item_fragment_dashboard_country_list_select,在您選擇時切換。

public class DashboardCountryListAdapter extends BaseAdapter { 

    private Context mContext; 
    private List<Country> mCountryList = new ArrayList<>(); 

    public DashboardCountryListAdapter(Context context, List<Country> countryList) { 
     mContext = context; 
     mCountryList = countryList; 
    } 

    @Override 
    public int getCount() { 
     return mCountryList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return mCountryList.isEmpty() ? null : mCountryList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder viewHolder; 
     if (convertView == null) { 
      convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_dashboard_country_list, parent, false); 
      viewHolder = new ViewHolder(convertView); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     if (!mCountryList.isEmpty()) { 
      Country country = mCountryList.get(position); 
      int image = country.getImage(); 
      if (image != -1) { 
       viewHolder.iv_item_fragment_dashboard_country_list.setImageResource(image); 
       if (country.getSelected()){ 
        viewHolder.iv_item_fragment_dashboard_country_list_select.setVisibility(View.VISIBLE); 
       } 
       else 
        viewHolder.iv_item_fragment_dashboard_country_list_select.setVisibility(View.GONE); 
      } 

      viewHolder.tv_item_fragment_dashboard_country_list.setText(country.getName()); 
     } 

     return convertView; 
    } 

    class ViewHolder { 
     @Bind(R.id.iv_item_fragment_dashboard_country_list) 
     ImageView iv_item_fragment_dashboard_country_list; 
     @Bind(R.id.tv_item_fragment_dashboard_country_list) 
     TextView tv_item_fragment_dashboard_country_list; 
     @Bind(R.id.iv_item_fragment_dashboard_country_list_select) 
     ImageView iv_item_fragment_dashboard_country_list_select; 

     public ViewHolder(View view) { 
      ButterKnife.bind(this, view); 
     } 
    } 

    public void updateList(List<Country> list) { 
     mCountryList = list; 
     notifyDataSetChanged(); 
    } 
} 

// XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="50dp"> 

    <ImageView 
     android:id="@+id/iv_item_fragment_dashboard_country_list" 
     android:layout_gravity="center" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:adjustViewBounds="true" 
     android:layout_width="20dp" 
     android:layout_height="15dp" /> 

    <com.UTU.View.UtuTextView 
     android:id="@+id/tv_item_fragment_dashboard_country_list" 
     android:textColor="@color/chic_black" 
     android:textSize="18sp" 
     android:gravity="center_vertical" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" /> 

    <ImageView 
     android:id="@+id/iv_item_fragment_dashboard_country_list_select" 
     android:src="@drawable/icon_check_teal" 
     android:layout_gravity="center" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:adjustViewBounds="true" 
     android:layout_width="13dp" 
     android:layout_height="10dp" /> 
</LinearLayout> 
0

我可以告訴你爲什麼編輯從正在運行的應用程序看起來不一樣:

在你的佈局XML列表視圖有這個屬性:

tools:listitem="@android:layout/simple_list_item_checked" 

但在您的代碼中,您有

ArrayAdapter<String> adapter = new ArrayAdapter<String>(Popneighbourhood.this, 
      android.R.layout.simple_list_item_1, neighbourhood); 

因此您對列表項使用完全不同的佈局。

您需要將R.layout.simple_list_item_checked放在適配器構造函數中,您可能需要更改爲指定要使用的TextView的ID的構造函數。

+0

謝謝你,我已經改變了屬性,在模擬器上,我現在可以選擇不同的項目,我該如何處理這些項目,我想知道如何獲得所選項目的計數......函數getCheckedItemCount不會返回任何東西,看起來好像檢查標記只是圖像的存在,但它們不被識別......我對此很新,而且我很難用這個列表視圖 – Arthur

+0

I'好吧,沒關係:)謝謝你的幫助 – Arthur

相關問題