新浪看看

来源:互联网 发布:陌陌用什么软件定位 编辑:程序博客网 时间:2024/05/07 13:03

1.编写activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</LinearLayout>

 2.在menu下新建main.xml进行连接

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

   <item android:id="@+id/menu_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100" />
</menu>

3.在layout下新建buju.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.对MainActivity.java进行编写

package com.example.listview;

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;
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 adapter = new SimpleAdapter(this, data,
    R.layout.buju, 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);
  item = new HashMap<String, Object>();
  item.put("photo", R.drawable.p7);
  item.put("name", "美美");
  item.put("publish", "2分钟前");
  item.put("content", "今天又可以去逛街啦!");
  data.add(item);
  item = new HashMap<String, Object>();
  item.put("photo", R.drawable.p8);
  item.put("name", "小西");
  item.put("publish", "5分钟前");
  item.put("content", "我要好好学习,坚决不逛淘宝!");
  data.add(item);
  item = new HashMap<String, Object>();
  item.put("photo", R.drawable.p9);
  item.put("name", "小蜗");
  item.put("publish", "4分钟前");
  item.put("content", "不玩游戏,不玩游戏");
  data.add(item);
  item = new HashMap<String, Object>();
  item.put("photo", R.drawable.p10);
  item.put("name", "蟹老板");
  item.put("publish", "1分钟前");
  item.put("content", "今天又卖了好多汉堡,又赚了好多的钱。");
  data.add(item);
  return data;
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

 

 

0 0
原创粉丝点击