Adapter

ArrayAdapter Demo:

public class ArrayAdapterDemo extends Activity {

    private ListView lv;
    ArrayAdapter<String> adapter;
    String [] data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.arrayadapter);
        lv = (ListView) findViewById(R.id.lv);
        
//        data = new String[] { "1", "2", "3"};
//        adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tv, data);
//        to find the textview and show it's groups
//        lv.setAdapter(adapter);
//        绑定数据源 entry情况下不需要设定数据源,系统已经默认提供(this time give you a default adapter)
        
    }
}
View Code


arrayadapter.xml

1 <ListView
2         android:id="@+id/lv"
3         android:layout_width="match_parent"
4         android:layout_height="match_parent" >
5 </ListView>
6 <!-- android:entries="@array/datas" -->
View Code

 

item.xml

1     <TextView
2         android:id="@+id/tv"
3         android:layout_width="match_parent"
4         android:layout_height="match_parent" />
5     <ImageView 
6         android:layout_width="match_parent"
7         android:layout_height="match_parent"
8         android:src="@drawable/ic_launcher"/>
View Code

 

SimpleAdapter Demo:

 1 public class SimpleAdapterDemo extends Activity{
 2     private SimpleAdapter simpleAdapter;
 3     private ArrayList<HashMap<String, Object>> dataList;
 4     private ListView lv;
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         // TODO Auto-generated method stub
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.arrayadapter);
10         lv = (ListView) findViewById(R.id.lv);
11         HashMap<String, Object> map;
12         dataList = new ArrayList<HashMap<String, Object>>();
13         String[] text = new String[] { "first1", "second1", "third1" };
14         String[] text2 = new String[] { "first2", "second2", "third2" };
15         int[] resourceId = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
16         
17         for (int i = 0; i < 3; i++) {
18             map = new HashMap<String, Object>();
19             map.put("img", resourceId[i]);
20             map.put("text1", text[i]);
21             map.put("text2", text2[i]);
22             dataList.add(map);
23         }
24         
25         String[] key = new String[] { "img", "text1", "text2"};
26         int[] id = new int[] { R.id.img, R.id.text1, R.id.text2 };
27         simpleAdapter = new SimpleAdapter(this, dataList, R.layout.simple, key, id);
28         lv.setAdapter(simpleAdapter);
29     }
30     
31 }
View Code

 

CustomAdapter Demo:

  1 public class BlogsAdapter extends BaseAdapter {
  2 
  3     private List<BlogsInfo> blogsList;
  4     private Context cxt;
  5     Boolean isEyeShield = false;
  6 
  7     public BlogsAdapter() {
  8         // TODO Auto-generated constructor stub
  9     }
 10 
 11     /**
 12      * @param blogsList
 13      * @param cxt
 14      */
 15     public BlogsAdapter(Context cxt, List<BlogsInfo> blogsList) {
 16         super();
 17         this.blogsList = blogsList;
 18         this.cxt = cxt;
 19 
 20         IntentFilter filter = new IntentFilter();
 21         filter.addAction(Constants.ACTION_EYE_SHIELD);
 22         filter.setPriority(Integer.MAX_VALUE);
 23         cxt.registerReceiver(myReceiver, filter);
 24 
 25         getPreference();
 26     }
 27 
 28     private BroadcastReceiver myReceiver = new BroadcastReceiver() {
 29         @Override
 30         public void onReceive(Context context, Intent intent) {
 31             isEyeShield = intent.getBooleanExtra("eyeshield", false);
 32             notifyDataSetChanged();
 33         }
 34     };
 35 
 36     public void setBlogsList(List<BlogsInfo> blogsList) {
 37         this.blogsList = blogsList;
 38     }
 39 
 40     @Override
 41     public int getCount() {
 42         // TODO Auto-generated method stub
 43         return blogsList.size();
 44     }
 45 
 46     @Override
 47     public BlogsInfo getItem(int position) {
 48         // TODO Auto-generated method stub
 49         return blogsList.get(position);
 50     }
 51 
 52     @Override
 53     public long getItemId(int position) {
 54         // TODO Auto-generated method stub
 55         return 0;
 56     }
 57 
 58     @Override
 59     public View getView(int position, View convertView, ViewGroup parent) {
 60         ViewHolder holder;
 61         if (null == convertView) {
 62             convertView = View.inflate(cxt, R.layout.blog_list_item, null);
 63             holder = new ViewHolder();
 64 
 65             holder.title = (TextView) convertView
 66                     .findViewById(R.id.tv_blog_list_title);
 67             holder.authorName = (TextView) convertView
 68                     .findViewById(R.id.tv_blog_list_authorname);
 69             holder.summary = (TextView) convertView
 70                     .findViewById(R.id.tv_blog_list_summary);
 71             holder.published_views_comments = (TextView) convertView
 72                     .findViewById(R.id.tv_blog_list_published_views_comments);
 73             convertView.setTag(holder);
 74         } else {
 75             holder = (ViewHolder) convertView.getTag();
 76         }
 77         // TODO Auto-generated method stub
 78         // accidently find the recycle princile
 79         if (isEyeShield) {
 80             if (position % 2 == 0) {
 81                 convertView.setBackgroundColor(Color.parseColor("#8E8E8E"));
 82             } else {
 83                 convertView.setBackgroundColor(Color.parseColor("#D3D3D3"));
 84             }
 85         } else {
 86             if (position % 2 == 0) {
 87                 convertView.setBackgroundColor(Color.parseColor("#BCD2EE"));
 88             } else {
 89                 convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
 90             }
 91         }
 92 
 93         holder.title.setText(getItem(position).getTitle());
 94         holder.authorName.setText(getItem(position).getAuthorName());
 95         holder.summary.setText(getItem(position).getSummary());
 96         holder.published_views_comments.setText(getItem(position)
 97                 .getPublished()
 98                 + "     |浏览:"
 99                 + getItem(position).getViews()
100                 + " |评论:" + getItem(position).getComments());
101         return convertView;
102     }
103 
104     static class ViewHolder {
105         TextView title;
106         TextView authorName;
107         TextView summary;
108         TextView published_views_comments;
109     }
110 
111     private void getPreference() {
112         SharedPreferences preferences = cxt.getSharedPreferences(
113                 "mypreferences", Activity.MODE_PRIVATE);
114         Boolean eyeshield = preferences.getBoolean("eyeshield", false);
115         isEyeShield = eyeshield;
116     }
117     
118     public void destroyBlogsReceiver() {
119         cxt.unregisterReceiver(myReceiver);
120     }
121 }
View Code


blog_list_item.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/tv_blog_list_title"
 9         style="@style/main_news_title"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content" />
12 
13     <TextView
14         android:id="@+id/tv_blog_list_authorname"
15         style="@style/main_news_sourceNam_pub"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content" />
18 
19     <TextView
20         android:id="@+id/tv_blog_list_summary"
21         style="@style/main_news_summary"
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content" />
24 
25     <TextView
26         android:id="@+id/tv_blog_list_published_views_comments"
27         style="@style/main_news_sourceNam_pub"
28         android:layout_width="match_parent"
29         android:layout_height="wrap_content" />
30 
31 </LinearLayout>
View Code

 

posted @ 2014-12-26 13:14  coolguy  阅读(129)  评论(0)    收藏  举报