我的活动中有一个带有图像和描述的列表视图,它工作得很好。我希望用户选择列表视图选项并继续下一个活动,为此我需要可检查的列表视图项。因此,用户选择的任何项目都可以通过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>
最佳答案
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);
}
});