Android实现ListView显示信息,点击每个item,跳转到相应界面
时间: 2024-05-02 16:18:39 浏览: 134
Android之ListView列表视图和界面跳转实现
4星 · 用户满意度95%
1. 在layout文件夹中创建一个新的xml文件,命名为list_item.xml,用于定义ListView中每个item的布局。
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:text="Item Title"/>
<TextView
android:id="@+id/item_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#666666"
android:layout_below="@id/item_title"
android:layout_alignStart="@id/item_title"
android:text="Item Description"/>
</RelativeLayout>
```
2. 在Activity中,定义ListView和数据源。
```java
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<Item> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化数据源
itemList = new ArrayList<>();
itemList.add(new Item("Item 1", "This is item 1"));
itemList.add(new Item("Item 2", "This is item 2"));
itemList.add(new Item("Item 3", "This is item 3"));
itemList.add(new Item("Item 4", "This is item 4"));
itemList.add(new Item("Item 5", "This is item 5"));
// 初始化ListView
listView = findViewById(R.id.list_view);
ItemAdapter adapter = new ItemAdapter(MainActivity.this, R.layout.list_item, itemList);
listView.setAdapter(adapter);
// 设置item的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item item = itemList.get(position);
Intent intent = new Intent(MainActivity.this, ItemActivity.class);
intent.putExtra("title", item.getTitle());
intent.putExtra("description", item.getDescription());
startActivity(intent);
}
});
}
}
```
3. 编写Adapter,用于将数据源中的数据填充到ListView的每个item中。
```java
public class ItemAdapter extends ArrayAdapter<Item> {
private int resourceId;
public ItemAdapter(Context context, int resourceId, List<Item> itemList) {
super(context, resourceId, itemList);
this.resourceId = resourceId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Item item = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.titleTextView = view.findViewById(R.id.item_title);
viewHolder.descriptionTextView = view.findViewById(R.id.item_description);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.titleTextView.setText(item.getTitle());
viewHolder.descriptionTextView.setText(item.getDescription());
return view;
}
private static class ViewHolder {
TextView titleTextView;
TextView descriptionTextView;
}
}
```
4. 创建一个新的Activity,用于展示每个item对应的详细信息。
```java
public class ItemActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
// 获取传递过来的数据
Intent intent = getIntent();
String title = intent.getStringExtra("title");
String description = intent.getStringExtra("description");
// 设置标题和描述
TextView titleTextView = findViewById(R.id.item_title);
TextView descriptionTextView = findViewById(R.id.item_description);
titleTextView.setText(title);
descriptionTextView.setText(description);
}
}
```
5. 在AndroidManifest.xml中注册新创建的Activity。
```xml
<activity android:name=".ItemActivity"></activity>
```
6. 运行程序,在MainActivity中点击每个item,可以跳转到相应的ItemActivity界面。
阅读全文