都知道微信主机面 有个界面会一行一一行的聊天记录,那个效果就可以用listview来实现(当然这只是其中的一种)
listView是一种比较常见的组件它用来展示列的view,它是根据数据的长度来显示数据,要做出效果还要用到适配器。适配器就是把一些数据给弄得适当,适合以便于在View上显示。可以看作是界面数据绑定的一种理解。它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等。适配器就像显示器,把复杂的东西按人可以接受的方式来展现,更详细解释请看http://wenku.baidu.com/link?url=SH9GU2Mn2xP-RDcw_RkgIsh59qY-vFGzK7LLj6qwsoThT-Lbae-ro33VdBJBd2qlsh_L8LL6_k5pd2JvehWs2-zb48a-luiIVKbwJ4SToV3(转载)
package com.example.winxin2; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.example.entity.Message;
import com.j256.ormlite.dao.Dao; import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.provider.SyncStateContract.Helpers;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private SimpleAdapter sa; private ormdatabase DBorm;
private static SQLiteDatabase db;
private ListView lv;
private List<Message> messageList = new ArrayList<Message>();
private List<Map<String,Object> > messageList2 = new ArrayList<Map<String,Object> >(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weixin); //模拟读取数据库或者互联网
for (int i = 0; i < 1000; i++) {
Message m = new Message();
m.setTou1("xxx");
m.setUserName("张三疯"+i);
m.setLastMessage("今晚有约,一起华山论剑");
m.setDatetime("11月11日");
messageList.add(m);//上周 //定义一个界面与数据的混合体,一个item代表一行记录
Map<String,Object> item = new HashMap<String,Object>();
//一行记录,包含多个控件
item.put("tou", R.drawable.tou1);
item.put("userName", "张三疯"+i);
item.put("lastMessage", "今晚有约,一起华山论剑");
item.put("datetime","11月11日");
messageList2.add(item);
} lv = (ListView)findViewById(R.id.listView1);
sa = new SimpleAdapter(this,
messageList2,//data 不仅仅是数据,而是一个与界面耦合的数据混合体
R.layout.listview_item_layout,
new String[] {"tou","userName","lastMessage","datetime"},//from 从那来
new int[] {R.id.imageView1,R.id.tv_userName,R.id.tv_lastMessage,R.id.tv_datetime}//to 到那里去
);
lv.setAdapter(sa);
lv.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Map<String,Object> item = messageList2.get(position); sa.notifyDataSetChanged(); Toast.makeText(MainActivity.this, (String)item.get("userName"), Toast.LENGTH_SHORT).show(); } }); //简单理解为VC绑在一起
/*
* baseAdapter,店长推荐 5星级
lv.setAdapter( new BaseAdapter(){ //返回多少条记录
@Override
public int getCount() {
// TODO Auto-generated method stub
return messageList.size();
}
//每一个item项,返回一次界面
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null; //布局不变,数据变 //如果缓存为空,我们生成新的布局作为1个item
if(convertView==null){
Log.i("info:", "没有缓存,重新生成"+position);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
//因为getView()返回的对象,adapter会自动赋给ListView
view = inflater.inflate(R.layout.listview_item_layout, null);
}else{
Log.i("info:", "有缓存,不需要重新生成"+position);
view = convertView;
}
Message m = messageList.get(position); TextView tv_userName = (TextView)view.findViewById(R.id.tv_userName);
tv_userName.setText( m.getUserName() );
tv_userName.setTextSize(15); TextView tv_lastMessage = (TextView)view.findViewById(R.id.tv_lastMessage);
tv_lastMessage.setText( m.getLastMessage() );
tv_lastMessage.setTextSize(12); TextView tv_datetime = (T xtView)view.findViewById(R.id.tv_datetime);
tv_datetime.setText( m.getDatetime() );
tv_datetime.setTextSize(12); return view;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} } );
*/
} } }
布局文件
weixin.xml
<?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="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<include layout="@layout/listview_layout"/>
</LinearLayout> </LinearLayout>
listview_item_layout.xml
<?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/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/> <LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="vertical"
android:layout_margin="5dp"
>
<TextView android:id="@+id/tv_userName"
android:layout_width="match_parent"
android:layout_height="20dp"
/> <TextView
android:id="@+id/tv_lastMessage"
android:layout_width="match_parent"
android:layout_height="15dp"
/> </LinearLayout> <TextView
android:id="@+id/tv_datetime"
android:layout_width="150dp"
android:layout_height="15dp"
/>
</LinearLayout>
listview_layout.xml
<?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="vertical" > <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" > </ListView> </LinearLayout>