列表视图包含图像和文本以及可检查属性

时间:2016-01-23 06:55:40

标签: android android-layout listview

我的活动中有一个列表视图,带有图像和描述,并且工作正常。我希望用户选择列表视图选项并继续下一个活动,为此我需要可检查的列表视图项。因此,可以收集用户选择的任何项目并通过JSON对象发送。我怎么能有这种组合?

这是我的列表视图XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.app.aalap.mobileapp.ThreeListView">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Image"
        android:src="@mipmap/image1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/Title"
        android:textSize="25dp"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/Description"
        android:layout_alignStart="@+id/Description" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/Description"
        android:layout_below="@+id/Title"
        android:layout_toRightOf="@+id/Image"
        android:layout_toEndOf="@+id/Image" />
</RelativeLayout>

这是我实现此列表视图格式的活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
                android:id="@+id/relativeLayout">

    <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Next"
            android:id="@+id/bNext_Three"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Step 2 of 3"
            android:id="@+id/bTo_Step_Two"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

    <TextView android:text="Select options from below"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/textView1"
              android:textStyle="bold"
              android:textSize="40px"
        android:layout_below="@+id/bNext_Three"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/onBoardList"
        android:layout_below="@+id/textView1"
        android:layout_alignRight="@+id/bNext_Three"
        android:layout_alignEnd="@+id/bNext_Three"
        android:layout_marginTop="45dp">
    </ListView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

ListView yourListView = (ListView) findViewById(R.id.listViewID);
yourListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        // When clicked, show a toast with the TextView text

        // Code specific to the first list item
        Intent myIntent = new Intent(MainActivity.this, ActivityB.class);
        startActivity(myIntent);
    }
});