simpleAdapter的使用

simpleAdapter的使用及注意事项

本例中,使用simpleAdapter对listview进行适配,下面直接贴出代码:


快捷键

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需求:展示一个ListView或者GridView包含数据的视图
 *思路:
 * 1、需要在布局文件中添加一个ListView或者是GridView控件
 * 2、需要在XML中编写一个Item布局,作为ListView或者GridView中的Item视图
 * 2、给ListView或者GridView准备数据
 * 3、给ListView或者GridView设置适配器,用来适配数据
 */
public class SettGridViewActivity extends Activity {
    private Context mContext;
    private List<Map<String,Object>> data = null;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sett_grid_view);
        mContext = this;
        data = new ArrayList<Map<String,Object>>();
        listView = (ListView) findViewById(R.id.listview);
        for(int x=0;x<50;x++){
            Map<String,Object>  map = new HashMap<String,Object>();
            map.put("image",R.drawable.ic_launcher);
            map.put("text",x);
            data.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.item_listview,new String[]{"image","text"},new int[]{R.id.image,R.id.text});
        listView.setAdapter(adapter);
    }
}

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="1"
            android:text="hello android" />
    </LinearLayout>
</LinearLayout>
在MainActivity中需要注意的是:SimpleAdapter只能写在OnCreate中,如果写在其他地方则会抛出一下异常:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{wcn.setgridview/wcn.setgridview.SettGridViewActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2279)
at android.app.ActivityThread.access 600(ActivityThread.java:144)atandroid.app.ActivityThread H.handleMessage(ActivityThread.java:1269)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5215)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit MethodAndArgsCaller.run(ZygoteInit.java:760)atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)atdalvik.system.NativeStart.main(NativeMethod)Causedby:java.lang.IllegalStateException:SystemservicesnotavailabletoActivitiesbeforeonCreate()atandroid.app.Activity.getSystemService(Activity.java:4505)atandroid.widget.SimpleAdapter.(SimpleAdapter.java:85)atwcn.setgridview.SettGridViewActivity.(SettGridViewActivity.java:41)atjava.lang.Class.newInstanceImpl(NativeMethod)atjava.lang.Class.newInstance(Class.java:1130)atandroid.app.Instrumentation.newActivity(Instrumentation.java:1061)atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2141)atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2279)atandroid.app.ActivityThread.access 600(ActivityThread.java:144)
at android.app.ActivityThread H.handleMessage(ActivityThread.java:1269)atandroid.os.Handler.dispatchMessage(Handler.java:99)atandroid.os.Looper.loop(Looper.java:137)atandroid.app.ActivityThread.main(ActivityThread.java:5215)atjava.lang.reflect.Method.invokeNative(NativeMethod)atjava.lang.reflect.Method.invoke(Method.java:525)atcom.android.internal.os.ZygoteInit MethodAndArgsCaller.run(ZygoteInit.java:760)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
“`
另外经过查看源码得知:simpleAdapter实际上继承BaseAdapter,如果自己需要可以重写相关方法.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值