模拟新浪微博随便看看栏目
来源:互联网 发布:知乎日报 rss源 编辑:程序博客网 时间:2024/05/16 10:31
Strings.xml
<resources>
<string name="app_name">ListView03</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">新浪微博——随便看看</string>
</resources>
MainActivity.java
package cn.bzu.listview03;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
//第一步:extends Activity
public class MainActivity extends Activity {
// 第二步:定义数据集合
List<Map<String, ?>> data;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = getData();
// 第三步:创建SimpleAdapter绑定数据
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.list_item, new String[] { "photo", "name", "publish",
"content" }, new int[] { R.id.photo, R.id.name,
R.id.publish, R.id.content });
listView=(ListView) this.findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ListClickHandler());
}
//第四步:添加单击事件
private class ListClickHandler implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
Map<String, String> item=(Map<String, String>) data.get(position);
Toast.makeText(MainActivity.this, item.get("name").toString(), Toast.LENGTH_LONG).show();
}
}
private List<Map<String, ?>> getData() {
List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
Map<String, Object> item = new HashMap<String, Object>();
item.put("photo", R.drawable.p1);
item.put("name", "如月");
item.put("publish", "1分钟前");
item.put("content", "你吃饭了吗?");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p2);
item.put("name", "伟伟");
item.put("publish", "10分钟前");
item.put("content", "心情有点小失落");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p3);
item.put("name", "姗姗");
item.put("publish", "5分钟前");
item.put("content", "天气好好达");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p4);
item.put("name", "玲玲");
item.put("publish", "1分钟前");
item.put("content", "今天上的什么课");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p5);
item.put("name", "小颖子");
item.put("publish", "2分钟前");
item.put("content", "今天天气真好哈!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p6);
item.put("name", "丹阳");
item.put("publish", "10分钟前");
item.put("content", "今天真高兴啊!");
data.add(item);
return data;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
Activity_mian.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:background="#FFBAD9"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
List_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- 左边图片 -->
<ImageView
android:id="@+id/photo"
android:padding="10dp"
android:layout_width="48dp"
android:layout_height="48dp" />
<!-- 右边布局 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 上边布局 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
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="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>
<!-- 下边发布内容 -->
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
}
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博的随便看看栏目(ListView自定义)
- 模拟新浪微博的随便看看栏目
- LVS学习笔记 1LVS基本概念
- JVM调优总结 -Xms -Xmx -Xmn -Xss
- 不容错过!开发者必备的十二大Android开发资源
- 请求jquery.min.js的时候,log显示浏览器还请求了jquery.min.map,该请求导致服务端报错
- Android textView控件自动识别电话号码,网址,邮箱,点击自动跳转到相应的功能
- 模拟新浪微博随便看看栏目
- 时钟
- 劳伤解(一) 中气
- 数据结构:最短路径(Dijkstra c++实现)
- PHPCMS添加文章失败原因
- Android SDK (包括Android 5.1.1,SDKTools 24.2)
- 使用命令行创建并运行android apk
- 第八周项目 三 (2) 终于完成了
- 美化开放厨房