«

Android中动态生成ListView及SimpleAdapter的使用

时间:2024-3-2 19:34     作者:韩俊     分类: Android


SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文

参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致

参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id

参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称

参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应

--------------------------------------------------------直接上例子了----------------------------------------------------------------------

activity_fujin_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minHeight="@dimen/dp_150"
    android:paddingTop="@dimen/dp_8"
    android:background="@drawable/list_item_click_bg_color" >

    <TextView 
        android:id="@+id/fujin_list_top_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/shiliuhao"
        android:textColor="#000000"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="@dimen/dp_8"
        android:layout_marginLeft="@dimen/dp_10"/>
    <TextView 
        android:id="@+id/fujin_list_top_distance"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textColor="#888"
        android:layout_alignParentRight="true"
        android:textSize="@dimen/jiuhao"
        android:layout_marginBottom="@dimen/dp_8"
        android:layout_marginRight="@dimen/dp_10"
        android:paddingTop="@dimen/dp_2"/>
    <TextView 
        android:id="@+id/fujin_list_top_fengexian"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/dp0_3"
        android:background="#888"
        android:layout_below="@+id/fujin_list_top_title"/>

    <ImageView 
        android:id="@+id/fujin_list_img"
        android:layout_height="@dimen/dp_85"
        android:layout_width="@dimen/dp_133"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fujin_list_top_fengexian"
        android:layout_marginTop="@dimen/dp_13"
        android:layout_marginLeft="@dimen/dp_7"/>
    <TextView 
        android:id="@+id/fujin_list_text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textColor="#7e7e7e"
        android:textSize="@dimen/shisanhao"
        android:layout_below="@+id/fujin_list_top_fengexian"
        android:layout_toRightOf="@+id/fujin_list_img"
        android:layout_marginLeft="@dimen/dp_10"
        android:layout_marginTop="@dimen/dp_13"
        android:layout_marginRight="@dimen/dp_10"
        android:ellipsize="end"
        android:singleLine="false" 
        android:maxLines="3"
        android:ems="12"/>

    <TextView 
        android:id="@+id/rmb_logo_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_10"
        android:layout_toRightOf="@+id/fujin_list_img"
        android:layout_below="@+id/fujin_list_text"
        android:text="@string/money_rmb_logo"
        android:textSize="@dimen/shisihao"
        android:layout_marginTop="@dimen/dp_13"
        android:layout_alignBottom="@+id/fujin_list_img"/>
    <TextView 
        android:id="@+id/fujin_list_red_money"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/rmb_logo_1"
        android:layout_below="@+id/fujin_list_text"
        android:textColor="@color/red_money_textcolor"
        android:textSize="@dimen/shisihao"
        android:layout_marginLeft="@dimen/dp_2"
        android:layout_marginTop="@dimen/dp_13"
        android:layout_alignBottom="@+id/fujin_list_img"/>

    <TextView 
        android:id="@+id/rmb_logo_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_8"
        android:layout_toRightOf="@+id/fujin_list_red_money"
        android:layout_below="@+id/fujin_list_text"
        android:text="@string/money_rmb_logo"
        android:textSize="@dimen/shisihao"
        android:layout_marginTop="@dimen/dp_13"
        android:layout_alignBottom="@+id/fujin_list_img"/>
    <TextView 
        android:id="@+id/fujin_list_old_money"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="@dimen/shisihao"
        android:textColor="@color/old_money_textcolor"
        android:layout_below="@+id/fujin_list_text"
        android:layout_toRightOf="@+id/rmb_logo_2"
        android:layout_marginTop="@dimen/dp_13"
        android:layout_marginLeft="@dimen/dp_2"
        android:layout_alignBottom="@+id/fujin_list_img"/>

</RelativeLayout>

activity_fujin.xml(不是全部布局代码,跟listview无关的就不贴了)
<LinearLayout
        android:id="@+id/fujin_lovelist_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/fujin_lovelist_lv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/general_back_white"
            android:cacheColorHint="#00000000"
            android:divider="@color/general_back_gary"
            android:dividerHeight="@dimen/dp_11"
            android:fastScrollEnabled="true"
            android:scrollbars="none" >
        </ListView>
    </LinearLayout>

FujinActivity.java(同样跟listview无关的就不贴了)
private void initGoodsList() {

        fujin_lovelist_llLayout = (LinearLayout) findViewById(R.id.fujin_lovelist_ll);
        fujin_lovelist_llLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        lovelv = (ListView) findViewById(R.id.fujin_lovelist_lv);   
     SimpleAdapter lovelistSimpleAdapter = new SimpleAdapter(this, getList(
                page_index, page_size, count),
                R.layout.activity_fujin_list_item, new String[] { "bigimage",
                        "goodname", "comment", "nowprice", "oldprice" },
                new int[] { R.id.fujin_list_img, R.id.fujin_list_top_title,
                        R.id.fujin_list_text, R.id.fujin_list_red_money,
                        R.id.fujin_list_old_money });

        lovelv.setAdapter(lovelistSimpleAdapter);

        //lovelv.setOnTouchListener(new MyTouchListener());
        //lovelv.setOnScrollListener(new MyScrollListener());//这两个调用时我做的监听滚动加载的调用,请无视...

//OnItemClickListener获取listview中每个item的监听,并实现传参跳转</span>
        lovelv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                String goods_index = String.valueOf(arg2);
                SharedPreferences settings = getSharedPreferences("GoodsXML",
                        Activity.MODE_PRIVATE);
                SharedPreferences.Editor localEditor = settings.edit();
                localEditor.putString("goods_index", goods_index);
                localEditor.commit();

                Bundle bundle = new Bundle();
                int PreviousPage = 1;
                bundle.putInt("PreviousPage", PreviousPage);

                Intent intent = new Intent(FujinActivity.this,
                        GoodReviewsActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
</span>


效果图:



请尊重原创,转载请注明出处。

如有疑问或意见欢迎评论,有不合理的地方妄勿喷。

版权声明:本文为博主原创文章,未经博主允许不得转载。

标签: android

热门推荐