2016-07-28 130 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

相关问题