如何在android上设置一个选中的列表项列表视图

时间:2016-07-28 00:38:12

标签: android listview

我在android上使用listView小部件,在预览列表内容中我选择了“checked list”项 基本上它是一个项目列表,我应该能够检查一些项目,当我做项目旁边的复选标记变得可见(它不是一个复选框,这是许多其他可检查列表之间的区别) 我不知道如何使用它,我想知道我至少可以检查一些项目,这是使复选标记可见,因为当我点击一个项目时,它是可点击的但没有任何反应... < / p>

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);

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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的构造函数。