注意事项:
1.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题显示。
2. Toast.LENGTH_LONG).show(); 使用Toast消息提示框显示点击内容。
3.定义Map类型的list对数据的存储。
package cn.edu.bzu.kt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.sinatest.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView listView;
private List<Map<String, Object>> data;
SimpleAdapter adapter;
String [] names= { "photo", "name", "publish","content" }; //定义map中key
int[] show ={ R.id.photo, R.id.name, R.id.publish, R.id.content };//list_view.xml中的控件id
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题显示
setContentView(R.layout.activity_main);
findId();//查找组件
setLv();
}
/**
* @author
*获取控件id
*/
public void findId() {
listView = (ListView) findViewById(R.id.lv);
}
/**
* 使用SimpleAdapter适配器绑定属性
*/
public void setLv(){
data=getData();
adapter=new SimpleAdapter(this, data,R.layout.list_view,
names,show);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listview, View view,
int position, long id) {
// TODO Auto-generated method stub
Map<String, Object> item = data.get(position);
Toast.makeText(MainActivity.this, item.get("content").toString(),
Toast.LENGTH_LONG).show(); //使用Toast消息提示框显示点击内容
}
});
}
/**
* @return
* 定义Map类型的list对数据的存储
*/
public List<Map<String, Object>> getData() {
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("photo", R.drawable.a);
map.put("name", "小小");
map.put("publish", "1分钟前");
map.put("content", "有时候遗忘,是最好的解脱;而沉默,是最好的诉说。。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.b);
map.put("name", " 青春时光");
map.put("publish", "10分钟前");
map.put("content", "时间,不一定能证明许多东西,但一定会看透许多东西。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.c);
map.put("name", "明天");
map.put("publish", "20分钟前");
map.put("content", "最感叹的莫过于一见如故,最悲伤的莫过于再见陌路。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.d);
map.put("name", "社会");
map.put("publish", "25分钟前");
map.put("content", "有许多的事情真的没有为什么的,因为这就是现实");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.e);
map.put("name", "Tom");
map.put("publish", "开心");
map.put("content", "如果你不快乐,那就出去走走,世界这么大。风景很美、机会很多、人生很短,不要蜷缩在一处阴影中。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.f);
map.put("name", "奋斗");
map.put("publish", "45分钟前");
map.put("content", "生命中曾经有过的所有灿烂,终究都需要用寂寞来偿还。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.g);
map.put("name", "时间");
map.put("publish", "一小时前");
map.put("content", "生活就是这样,从来不彩排,不回放。");
data.add(map);
return data;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<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"
tools:context=".MainActivity" >
<!-- 标题栏 -->
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="25dp"
android:text="@string/name"
android:background="#EE4000"
/>
<!-- ListView -->
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView1"
android:dividerHeight="1px" >
</ListView>
</RelativeLayout>
<?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="horizontal" >
<!-- 左边的图片显示 -->
<ImageView
android:id="@+id/photo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="top"
android:padding="10dp" />
<!-- 右边是线性布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/publish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>
<!-- 内容的显示 -->
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
/>
</LinearLayout>
</LinearLayout>