小奋斗 - 轻松学习从此开始!
IT小奋斗群 QQ群:62017228

Android 自定义listView

都知道微信主机面 有个界面会一行一一行的聊天记录,那个效果就可以用listview来实现(当然这只是其中的一种)

  listView是一种比较常见的组件它用来展示列的view,它是根据数据的长度来显示数据,要做出效果还要用到适配器。适配器就是把一些数据给弄得适当,适合以便于在View上显示。可以看作是界面数据绑定的一种理解。它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等。适配器就像显示器,把复杂的东西按人可以接受的方式来展现,更详细解释请看http://wenku.baidu.com/link?url=SH9GU2Mn2xP-RDcw_RkgIsh59qY-vFGzK7LLj6qwsoThT-Lbae-ro33VdBJBd2qlsh_L8LL6_k5pd2JvehWs2-zb48a-luiIVKbwJ4SToV3(转载)

  

  1 package com.example.winxin2;
  2 
  3 import java.sql.SQLException;
  4 import java.util.ArrayList;
  5 import java.util.HashMap;
  6 import java.util.List;
  7 import java.util.Map;
  8 
  9 import com.example.entity.Message;
 10 import com.j256.ormlite.dao.Dao;
 11 
 12 import android.app.Activity;
 13 import android.content.ContentValues;
 14 import android.database.Cursor;
 15 import android.database.sqlite.SQLiteDatabase;
 16 import android.database.sqlite.SQLiteOpenHelper;
 17 import android.os.Bundle;
 18 import android.provider.SyncStateContract.Helpers;
 19 import android.util.Log;
 20 import android.view.LayoutInflater;
 21 import android.view.Menu;
 22 import android.view.MenuItem;
 23 import android.view.View;
 24 import android.view.ViewGroup;
 25 import android.widget.AdapterView;
 26 import android.widget.AdapterView.OnItemClickListener;
 27 import android.widget.BaseAdapter;
 28 import android.widget.ImageView;
 29 import android.widget.LinearLayout;
 30 import android.widget.ListView;
 31 import android.widget.SimpleAdapter;
 32 import android.widget.TextView;
 33 import android.widget.Toast;
 34 
 35 
 36 public class MainActivity extends Activity {
 37 
 38     private SimpleAdapter sa;
 39     
 40     private ormdatabase DBorm;
 41     private static SQLiteDatabase db;
 42     private ListView lv;
 43     private List<Message> messageList = new ArrayList<Message>();
 44     private List<Map<String,Object> > messageList2 = new ArrayList<Map<String,Object> >();
 45     
 46     @Override
 47     protected void onCreate(Bundle savedInstanceState) {
 48         super.onCreate(savedInstanceState);
 49         setContentView(R.layout.weixin);
 50         
 51         //模拟读取数据库或者互联网
 52         for (int i = 0; i < 1000; i++) {
 53             Message m = new Message();
 54             m.setTou1("xxx");
 55             m.setUserName("张三疯"+i);
 56             m.setLastMessage("今晚有约,一起华山论剑");
 57             m.setDatetime("11月11日");
 58             messageList.add(m);//上周
 59             
 60             
 61             //定义一个界面与数据的混合体,一个item代表一行记录
 62             Map<String,Object> item  = new HashMap<String,Object>();
 63             //一行记录,包含多个控件
 64             item.put("tou", R.drawable.tou1);
 65             item.put("userName", "张三疯"+i);
 66             item.put("lastMessage", "今晚有约,一起华山论剑");
 67             item.put("datetime","11月11日");
 68             messageList2.add(item);
 69         } 
 70         
 71         
 72         lv = (ListView)findViewById(R.id.listView1);
 73         sa = new SimpleAdapter(this,
 74                 messageList2,//data 不仅仅是数据,而是一个与界面耦合的数据混合体
 75                 R.layout.listview_item_layout,
 76                 new String[] {"tou","userName","lastMessage","datetime"},//from 从那来
 77                 new int[] {R.id.imageView1,R.id.tv_userName,R.id.tv_lastMessage,R.id.tv_datetime}//to 到那里去
 78                 );
 79         lv.setAdapter(sa);
 80         lv.setOnItemClickListener(new OnItemClickListener() {
 81 
 82             @Override
 83             public void onItemClick(AdapterView<?> parent, View view,
 84                     int position, long id) {
 85                 Map<String,Object> item = messageList2.get(position);
 86         
 87                 
 88                 sa.notifyDataSetChanged();
 89                 
 90                 Toast.makeText(MainActivity.this, (String)item.get("userName"), Toast.LENGTH_SHORT).show();
 91                 
 92                 
 93             }
 94           
 95         });
 96         
 97             
 98         //简单理解为VC绑在一起
 99         /*
100          * baseAdapter,店长推荐 5星级
101         lv.setAdapter( new BaseAdapter(){
102 
103             //返回多少条记录
104             @Override
105             public int getCount() {
106                 // TODO Auto-generated method stub
107                 return messageList.size();
108             }
109             //每一个item项,返回一次界面
110             @Override
111             public View getView(int position, View convertView, ViewGroup parent) {
112                 View view = null;
113                 
114                 
115                 //布局不变,数据变
116                 
117                 //如果缓存为空,我们生成新的布局作为1个item
118                 if(convertView==null){
119                     Log.i("info:", "没有缓存,重新生成"+position);
120                     LayoutInflater inflater = MainActivity.this.getLayoutInflater();
121                     //因为getView()返回的对象,adapter会自动赋给ListView
122                     view = inflater.inflate(R.layout.listview_item_layout, null);
123                 }else{
124                     Log.i("info:", "有缓存,不需要重新生成"+position);
125                     view = convertView;
126                 }
127                 Message m = messageList.get(position);
128                 
129                 TextView tv_userName = (TextView)view.findViewById(R.id.tv_userName);
130                 tv_userName.setText(  m.getUserName()  );
131                 tv_userName.setTextSize(15);
132                 
133                 TextView tv_lastMessage = (TextView)view.findViewById(R.id.tv_lastMessage);
134                 tv_lastMessage.setText(  m.getLastMessage()  );
135                 tv_lastMessage.setTextSize(12);
136                 
137                 TextView tv_datetime = (T xtView)view.findViewById(R.id.tv_datetime);
138                 tv_datetime.setText( m.getDatetime()  );
139                 tv_datetime.setTextSize(12);
140 
141                 return view;
142             }
143             
144             @Override
145             public Object getItem(int position) {
146                 // TODO Auto-generated method stub
147                 return null;
148             }
149 
150             @Override
151             public long getItemId(int position) {
152                 // TODO Auto-generated method stub
153                 return 0;
154             }
155 
156             
157             
158         } );
159         */
160     }
161     
162   
163     }
164     
165 }

布局文件

  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>

 

我来评几句
登录后评论

已发表评论数(0)