模拟新浪微博随便看看一栏

1.运用listView创建的新浪微博随便看看界面效果图如下  


2.   LIstView 控件的使用

   理解Adapter的作用并掌控自定义

FruitAdapter的使用方法

3.代码分析

  extends Activity

  定义数据集合

  创建SimpleAdapter绑定数据

   添加单击事件

3.步骤 


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>

activity_main.xml

</resources>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/menu_settings"

        android:title="@string/menu_settings"

        android:orderInCategory="100" />

</menu>

 

 

 

strings.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>

 

4.java代码

maimActivity代码

 

package cn.bzu.weibo;

 

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.p10);

item.put("name", "叮叮");

item.put("publish", "2分钟前");

item.put("content", "嘎嘎嘎嘎嘎嘎嘎嘎嘎");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p7);

item.put("name", "迪西");

item.put("publish", "7分钟前");

item.put("content", "哈哈哈哈哈哈哈哈");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p6);

item.put("name", "拉拉");

item.put("publish", "9分钟前");

item.put("content", "嘿嘿嘿嘿嘿嘿");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p8);

item.put("name", "波儿");

item.put("publish", "14分钟前");

item.put("content", "哼哼哼哼哼哼哼");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p5);

item.put("name", "阿斯卡酱");

item.put("publish", "25分钟前");

item.put("content", "今天天气真好适合外出");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p9);

item.put("name", "ANNA");

item.put("publish", "25分钟前");

item.put("content", "I love my sister");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p1);

item.put("name", "ELSA");

item.put("publish", "26分钟前");

item.put("content", "I love my sister");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p2);

item.put("name", "明日香");

item.put("publish", "27分钟前");

item.put("content", "今天真高兴!");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p3);

item.put("name", "小兰");

item.put("publish", "28分钟前");

item.put("content", "要小心啊1");

data.add(item);

item = new HashMap<String, Object>();

item.put("photo", R.drawable.p4);

item.put("name", "嘻嘻");

item.put("publish", "29分钟前");

item.put("content", "今天真高兴!");

data.add(item);

return data;

}

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值