码迷,mamicode.com
首页 > 其他好文 > 详细

自定义Adapter

时间:2015-04-30 12:26:53      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

--MainActivity代码
  1 package com.example.qqdemo;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import android.os.Bundle;
  7 import android.app.Activity;
  8 import android.view.Gravity;
  9 import android.view.Menu;
 10 import android.view.View;
 11 import android.view.ViewGroup;
 12 import android.widget.BaseAdapter;
 13 import android.widget.ImageView;
 14 import android.widget.ListView;
 15 import android.widget.TextView;
 16 
 17 public class MainActivity extends Activity {
 18 
 19     private List<UserInfo> list = new ArrayList<UserInfo>();// 定义数据源
 20     private ListView mylistviwe;
 21 
 22     @Override
 23     protected void onCreate(Bundle savedInstanceState) {
 24         super.onCreate(savedInstanceState);
 25         setContentView(R.layout.activity_main);
 26         initlist();
 27         initview();
 28     }
 29 
 30     @Override
 31     public boolean onCreateOptionsMenu(Menu menu) {
 32         // Inflate the menu; this adds items to the action bar if it is present.
 33         getMenuInflater().inflate(R.menu.main, menu);
 34         return true;
 35     }
 36 
 37     private void initview() {
 38         mylistviwe = (ListView) findViewById(R.id.listview);
 39         mylistviwe.setAdapter(new Myadapter());//绑定适配器
 40     }
 41 
 42     /**
 43      * 2015-4-30 向数据源添加数据
 44      */
 45     private void initlist() {
 46 
 47         for (int i = 0; i < 20; i++) {
 48             UserInfo uif = new UserInfo();
 49             if (i % 2 == 0) {
 50                 uif.setImage(R.drawable.monkey );
 51                 uif.setMag("Hello");
 52                 uif.setFlag(true);
 53             } else {
 54                 uif.setImage(R.drawable.monkey1 );
 55                 uif.setMag("HI");
 56                 uif.setFlag(false);
 57             }
 58             list.add(uif);
 59         }
 60 
 61     }
 62 
 63     /**
 64      * 定义内部类 自定义适配器 实现数据绑定
 65      */
 66 
 67     private class Myadapter extends BaseAdapter {
 68 
 69         @Override
 70         public int getCount() {// 修改getCount()返回值为list的长度
 71 
 72             return list.size();// 返回数据的条数
 73         }
 74 
 75         @Override
 76         public Object getItem(int position) {// 修改getItem()list封装具体内容
 77 
 78             return list.get(position);// 获取item里面的内容
 79         }
 80 
 81         @Override
 82         public long getItemId(int position) { // 返回对应位置getItemId;
 83             // 返回对应位置getItemId;
 84 
 85             return position;// 返回item的位置
 86         }
 87 
 88         // 绑定数据getview->findviewbyid ->给控件添加数据
 89         @Override
 90         public View getView(int position, View convertView, ViewGroup parent) {
 91 
 92             // 手动生成View对象 查找XML中控件。
 93             // R.layout.item_xml-->找到XML控件
 94             // null-->是否把读取上来的view添加到容器当中
 95             View v = View.inflate(MainActivity.this, R.layout.item_xml, null);
 96             // 得到当先控件的ID
 97             ImageView leftimage = (ImageView) v.findViewById(R.id.leftimage);
 98             ImageView rigtimage = (ImageView) v.findViewById(R.id.rigtimage);
 99             TextView tvshow = (TextView) v.findViewById(R.id.mytextview);
100             UserInfo muif = list.get(position);// 获取到item里面的内容
101 
102             // 将item添加到ListView中
103             if (muif.isFlag()) {
104                 rigtimage.setVisibility(View.GONE);
105                 leftimage.setVisibility(View.VISIBLE);
106                 leftimage.setImageResource(muif.getImage());
107                 tvshow.setText(muif.getMag());
108                 tvshow.setGravity(Gravity.LEFT);
109 
110             } else {
111                 rigtimage.setVisibility(View.VISIBLE);
112                 leftimage.setVisibility(View.GONE);
113                 leftimage.setImageResource(muif.getImage());
114                 tvsh
115 
116 
117 
118 
119 ow.setText(muif.getMag());
120                 tvshow.setGravity(Gravity.RIGHT);
121 
122             }
123             return v;
124         }
125 
126     }
127 
128 }

 


 1 package com.example.qqdemo;
 2 /**
 3  * 封装UserInfo对象
 4  * @author wenfei
 5  */
 6 public class UserInfo {
 7     private int image;
 8     private String mag;
 9     private boolean flag; 
10     
11     
12     public int getImage() {
13         return image;
14     }
15     public void setImage(int image) {
16         this.image = image;
17     }
18     public String getMag() {
19         return mag;
20     }
21     public void setMag(String mag) {
22         this.mag = mag;
23     }
24     public boolean isFlag() {
25         return flag;
26     }
27     public void setFlag(boolean flag) {
28         this.flag = flag;
29     }
30     
31     
32 }


--XML文件
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".MainActivity" >
 6 
 7     <ListView
 8         android:id="@+id/listview"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" />
11 
12 </LinearLayout>
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <RelativeLayout
 9         android:layout_width="match_parent"
10         android:layout_height="50dp" >
11 
12         <ImageView
13             android:id="@+id/leftimage"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:layout_centerVertical="true"
17             android:src="@drawable/monkey" />
18 
19         <TextView
20             android:id="@+id/mytextview"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:layout_centerVertical="true"
24             android:layout_toLeftOf="@+id/rigtimage"
25             android:layout_toRightOf="@+id/leftimage"
26             android:gravity="right"
27             android:text="我来了" />
28 
29         <ImageView
30             android:id="@+id/rigtimage"
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:layout_alignParentRight="true"
34             android:layout_centerVertical="true"
35             android:src="@drawable/monkey1" />
36     </RelativeLayout>
37 
38 </LinearLayout>

 

自定义Adapter

标签:

原文地址:http://www.cnblogs.com/wenfei123chai/p/4468517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!