ListView和SimpleAdapter的入门用法

ListView所在布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.yin.simpleadapter2.MainActivity" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
 		>
        
    </ListView>
 </LinearLayout>

子条目的布局文件:

<?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"
    android:orientation="horizontal" >
    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
    <TextView 
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:text="李四"
        />
	
</LinearLayout>

Activity代码:

package com.yin.simpleadapter2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//找到listView控件
		ListView listView = (ListView) findViewById(R.id.listView);
		//创建一个simpleAdapter
		/**
		 *     public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to) {
        mData = data;
        mResource = mDropDownResource = resource;
        mFrom = from;
        mTo = to;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
		 */
		List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		map=new HashMap<String, Object>();
		map.put("img", R.drawable.ic_launcher);
		map.put("name", "成龙1");
		data.add(map);
		SimpleAdapter adapter=new SimpleAdapter(this, //上下文
												data, //Map类型的list集合,list中的每一个元素(map)就是ListView中的一个条目
												R.layout.list_item,//指向条目布局的xml文件 
												new String[]{"img","name"},//map中的key组成的字符串数组 
												new int[]{R.id.imageView,R.id.textView}//条目中各个子内容,注意要与map中的key组成的String数组顺序对应
		);
		listView.setAdapter(adapter);
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Studio中,要使用ListViewSimpleAdapter数据适配器,可以按照以下步骤进行: 1. 在布局文件中添加ListView组件。 2. 创建一个ArrayList对象来保存ListView中的数据。 3. 创建一个SimpleAdapter对象,将ListView中的数据绑定到适配器中。 4. 在Activity或Fragment中设置ListView的适配器,以便将数据显示在ListView中。 以下是一个示例代码: ```java // 获取ListView组件 ListView listView = findViewById(R.id.list_view); // 创建一个ArrayList对象来保存数据 ArrayList<HashMap<String, String>> dataList = new ArrayList<>(); // 添加数据 HashMap<String, String> dataItem1 = new HashMap<>(); dataItem1.put("title", "标题1"); dataItem1.put("content", "内容1"); dataList.add(dataItem1); HashMap<String, String> dataItem2 = new HashMap<>(); dataItem2.put("title", "标题2"); dataItem2.put("content", "内容2"); dataList.add(dataItem2); // 创建SimpleAdapter对象 SimpleAdapter adapter = new SimpleAdapter(this, dataList, android.R.layout.simple_list_item_2, new String[]{"title", "content"}, new int[]{android.R.id.text1, android.R.id.text2}); // 设置ListView的适配器 listView.setAdapter(adapter); ``` 在这个示例代码中,我们创建了一个包含两个列表项的ArrayList对象,然后使用SimpleAdapter将数据绑定到ListView中,其中android.R.layout.simple_list_item_2是一个内置的布局文件,可以用来显示两行文本,适合于显示标题和内容。在SimpleAdapter的构造函数中,我们指定了数据源的键名和布局文件中TextView的ID,这样SimpleAdapter就知道如何将数据绑定到ListView中了。最后,我们将适配器设置到ListView中,这样ListView就可以显示我们的数据了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值