新浪微博——随便看看

 
Strings:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">新浪微博-随便看看</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>

新浪微博——随便看看(ListView)

 MainActivity.java(java语言)代码:
package com.example.weibo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

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

import com.example.weibo.R;

import android.os.Bundle;
import android.R.anim;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

//第一步:继承ListActivity
public class MainActivity extends ListActivity {
	// 第二步:定义List集合装载所需要的数据
	private List<Map<String, ?>> data;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		data = getData();
		// 第三步:创建SimpleAdapter来装载所需要的数据
		/*
		 * SimpleAdapter adapter = new SimpleAdapter(this, data,
		 * android.R.layout.simple_list_item_2, new String[] { "name", "address"
		 * }, new int[] { android.R.id.text1, android.R.id.text2 });
		 */
		// String[] from = { "name", "address" };
		// int[] to = { android.R.id.text1, android.R.id.text2 };
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.activity_main, new String[] { "name",
						"photo", "wenzi", "time" }, new int[] { R.id.name,
						R.id.photo, R.id.wenzi, R.id.time });
		// 第四步:将ListView与SimpleAdapter绑定
		setListAdapter(adapter);
	}

	private List<Map<String, ?>> getData() {
		List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
		Map<String, Object> item1 = new HashMap<String, Object>();
		item1.put("name", "潇湘夜雨");
		item1.put("photo", R.drawable.p1);
		item1.put("time", "1分钟前");
		item1.put("wenzi", "这几天我学会了使用ListView组件。这个组件非常有用,"
				+ "希望在今后的开发中能大量的用到好好学习Android开发。" + "更多的精彩还在等着我们呢!大家加油啊。");
		data.add(item1);
		Map<String, Object> item2 = new HashMap<String, Object>();
		item2.put("name", "小小");
		item2.put("photo", R.drawable.p2);
		item2.put("time", "10分钟前");
		item2.put("wenzi", "哇。这里好好玩啊,希望在这里结实更多的朋友。");
		data.add(item2);
		Map<String, Object> item3 = new HashMap<String, Object>();
		item3.put("name", "青春无悔");
		item3.put("photo", R.drawable.p3);
		item3.put("time", "1分钟前");
		item3.put("wenzi", "今天上班累死了,幸好下午老板开恩。" + "我们提前下班休息。呵呵‘。。");
		data.add(item3);
		Map<String, Object> item4 = new HashMap<String, Object>();
		item4.put("name", "小丸子");
		item4.put("photo", R.drawable.p4);
		item4.put("time", "2分钟前");
		item4.put("wenzi", "今天班里大扫除,怎么会有这么多的垃圾呢?" + "各种无语。看来要好好主意平时的维护了。。。");
		data.add(item4);
		return data;

	}

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

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Map<String, ?> item = data.get(position);
		String name = (String) item.get("name");
		String wenzi = (String) item.get("wenzi");
		String photo = (String) item.get("photo");
		String time = (String) item.get("time");

	}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="19dp"
        android:layout_marginTop="24dp"
        android:src="@drawable/p1" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/photo"
        android:layout_marginLeft="17dp"
        android:layout_toRightOf="@+id/photo"
        android:text="潇潇夜雨"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/name"
        android:layout_marginRight="17dp"
        android:text="1分钟前" />

    <TextView
        android:id="@+id/wenzi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_below="@+id/name"
        android:text="这几天我学会了使用ListView组件。这个组件非常有用,\n希望在今后的开发中能大量的用到好好学习Android开发/n更多的精彩还在等着我们呢!大家加油啊。" />

</RelativeLayout>
结果:
 


总结:在本次实验中,采用了相对布局,起初只是照着老师给的布局写了写,结果发现图片插不进去,最后听老师讲解,慢慢尝试着去做,结果成功了。现在觉的android是个好神奇的东西,现在已经对android产生了兴趣。



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值