4.27Android学习

一、今日学习内容

Android实现ListView显示信息,点击每个item,跳转到相应界面

 

实现的方法比较简单,就是定义一个ListView,然后设置监听,ListView对每个条目的监听是setOnItemClickListener。

onItemClick(AdapterView<?> parent, View view, int position, long id)
这段代码中,
parent           发生点击动作的AdapterView。

view              在AdapterView中被点击的视图(它是由adapter提供的一个视图)。

position     视图在adapter中的位置。

id                   被点击元素的行id。

 

接下来给大家看看XML里面的代码吧,

1.activity_main.xml

<LinearLayout 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"
tools:context=".MainActivity" >

<ListView
android:id="@+id/Lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>
2.item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/p1" />

<TextView
android:id="@+id/tv_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="30sp"
android:text="@string/name" />

</LinearLayout>
3.model.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/Iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/p1" />

<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈啊哈哈" />

</LinearLayout>
下面是MainActivity.java,这个类的功能是显示ListView,然后用户一点击其中一个item,我们就可以得到用户歌手信息,然后通过Bundle方法,将信息发送至跳转界面,然后跳转界面再进行相应展示
package com.example.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
private ListView Lv = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Lv = (ListView) findViewById(R.id.Lv);

final String[] name = new String[] { "张国荣", "张学友", "谭咏麟" };
final String[] message = new String[] {
"张国荣[1],1956年9月12日生于香港,歌手、演员、音乐人;影视歌多栖发展的代表之一。1977年正式出道。1983年以《风继续吹》成名。1984年演唱的《Monica》是香港歌坛第一支同获十大中文金曲、十大劲歌金曲的舞曲 。 1986年、1987年获劲歌金曲金奖",
"张学友,歌手、演员,1961年7月10日出生于香港,1984年获得香港首届十八区业余歌唱大赛冠军,正式出道,1993年发行的国语唱片《吻别》年度销量超过400万张,1995年、1996年连续两年获得世界音乐大奖全球销量最高亚洲流行乐歌手奖",
"谭咏麟,1950年8月23日出生于香港,籍贯广东新会,中国香港男歌手、音乐人、演员。[1]20世纪60年代末为Loosers乐队成员。1973年任温拿乐队主音歌手。1975年参演首部电影《大家乐》。1978年温拿乐队宣布解散,谭咏麟以个人身份发展。1979年赴台湾发展事业,推出首张个人专辑《反斗星》" };
final int[] photo = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3 };
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("photo", R.drawable.p1);
map1.put("name", name[0]);
data.add(map1);

Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("photo", R.drawable.p2);
map2.put("name", name[1]);
data.add(map2);

Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("photo", R.drawable.p3);
map3.put("name", name[2]);
data.add(map3);

Lv.setAdapter(new SimpleAdapter(this, data, R.layout.item,new String[] { "photo", "name" }, new int[] { R.id.iv,R.id.tv_name }));
Lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

Bundle bundle = new Bundle();
bundle.putInt("photo", photo[arg2]);
bundle.putString("message", message[arg2]);
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setClass(MainActivity.this, MoveList.class);
Log.i("message", message[arg2]);
startActivity(intent);
}
});
}

}


这是用户点击后的界面,如下

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MoveList extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.model);

Bundle bundle=getIntent().getExtras();
int id=bundle.getInt("photo");
String message=bundle.getString("message");
ImageView Iv=(ImageView) findViewById(R.id.Iv);
Iv.setImageResource(id);
TextView tv=(TextView) findViewById(R.id.tv_message);
tv.setText(message);


}

}

二、遇到的问题

 暂无

三、明日计划

继续学习吧

posted @ 2021-04-27 15:58  居酿圆子  阅读(39)  评论(0编辑  收藏  举报