【Android基础入门〖8〗】SimpleAdapter之一

目录(?)[+]

SimpleAdapter
 

Activity

主程序

listItem 中存放所有要显示的所有列数据,每 new 一个map,即 在显示的列表中 新建一行数据,Title (自定义)、Image(自定义) 只是别名,分别对应 每一行的布局 item_layout 中的 资源 ID , Title => R.id.item_text,Image => R.id.item_image。


  1. package com.michael.training;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.widget.AdapterView;  
  12. import android.widget.AdapterView.OnItemClickListener;  
  13. import android.widget.ListView;  
  14. import android.widget.SimpleAdapter;  
  15. import android.widget.Toast;  
  16.   
  17. public class MainActivity extends Activity {  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main_layout);  
  23.           
  24.         ListView listView = (ListView)findViewById(R.id.mListView);  
  25.         ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();  
  26.           
  27.         HashMap<String, Object> map = new HashMap<String, Object>();  
  28.         map.put("Title""NO.1  开发环境搭建");  
  29.         map.put("Image", R.drawable.item);  
  30.         listItem.add(map);  
  31.           
  32.         map = null;  
  33.         map = new HashMap<String, Object>();  
  34.         map.put("Title""NO.2  四大组件");  
  35.         map.put("Image", R.drawable.item);  
  36.         listItem.add(map);  
  37.           
  38.         SimpleAdapter listAdapter = new SimpleAdapter(  this,  
  39.                                 listItem,  
  40.                                 R.layout.item_layout,  
  41.                                 new String[] {"Title""Image"},  
  42.                                 new int[] {R.id.item_text,R.id.item_img});  
  43.         listView.setAdapter(listAdapter);  
  44.         listView.setOnItemClickListener(listener);  
  45.     }  
  46.       
  47.     OnItemClickListener listener=new OnItemClickListener() {  
  48.   
  49.         @Override  
  50.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {  
  51.             Toast.makeText(getApplicationContext(),((Integer)arg2).toString()+"-----"+((Integer)arg2).toString(), Toast.LENGTH_SHORT).show();  
  52.         }  
  53.     };  
  54.       
  55.       
  56.       
  57.     @Override  
  58.     public boolean onCreateOptionsMenu(Menu menu) {  
  59.         // Inflate the menu; this adds items to the action bar if it is present.  
  60.         getMenuInflater().inflate(R.menu.main, menu);  
  61.         return true;  
  62.     }  
  63.   
  64. }  

main_layout.xml

主页面布局
只放了个ListView。
  1. <RelativeLayout   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.     <ListView    
  7.          android:id = "@+id/mListView"  
  8.          android:layout_width = "match_parent"  
  9.          android:layout_height = "match_parent" />  
  10. </RelativeLayout>  

item_layout.xml

item布局
即ListView中每一行显示的布局,我定义了一个图片和一个文本。
  1. <? xml   version = "1.0"   encoding = "utf-8" ?>  
  2. <RelativeLayout   xmlns:android = "http://schemas.android.com/apk/res/android"  
  3.      android:layout_width = "fill_parent"  
  4.      android:layout_height = "match_parent"  
  5.      android:layout_gravity = "center_vertical"   xmlns:tools = "http://schemas.android.com/tools"   tools:ignore = "HardcodedText" >  
  6.     <ImageView  
  7.          android:id = "@+id/item_img"  
  8.          android:layout_width = "wrap_content"  
  9.          android:layout_height = "wrap_content"  
  10.          android:layout_alignParentTop = "true"  
  11.          android:layout_marginLeft = "10dp"  
  12.          android:src = "@drawable/item"  
  13.          android:contentDescription = "imageflag"   />  
  14.     <TextView  
  15.          android:id = "@+id/item_text"  
  16.          android:layout_width = "wrap_content"  
  17.          android:layout_height = "wrap_content"  
  18.          android:layout_alignBottom = "@+id/item_img"  
  19.          android:layout_alignParentRight = "true"  
  20.          android:layout_alignParentTop = "true"  
  21.          android:layout_marginTop = "4dp"  
  22.          android:layout_marginLeft = "24dp"  
  23.          android:layout_toRightOf = "@+id/item_img"  
  24.          android:gravity = "center_vertical"  
  25.          android:text = "环境搭建"  
  26.          android:textStyle = "bold"   />  
  27. </RelativeLayout>  


成果




转载请注明出处!http://blog.csdn.net/mkrcpp/article/details/9792191
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值