Android中,ListView的使用中,ArrayAdapter和SimpleAdapter的简单运用

ArrayAdapter

1、首先在mainactivity.xml中布局一个ListView

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>

2、新建一个item.xml,其中只有一个TextView

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

注意,最好在这个xml中不要使用布局,否则后面可能出错

3、新建一个item_1.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

4、新建一个item_2.xml

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

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="aaaa" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="bbbbb" />

</LinearLayout>

5、在MainActivity.java写适配器

package com.learn.AdapterUsing;  
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
String objects[] = {"老张","老方","老李","老毕","老刘","老韩"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //【1】找到我们关心的控件
        ListView lv = (ListView) findViewById(R.id.lv);
        //【2】创建一个arrayAdapter	
        //方法1:
        //ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item, objects);
        //方法2:
        // ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item_1, R.id.textView1, objects);
        //方法3:
        List<Map<String,String>> data = new ArrayList<Map<String,String>>();
        
        Map<String,String> map1 = new HashMap<String, String>();
        map1.put("name", "张飞");
        map1.put("phone", "13888");
        
        Map<String,String> map2 = new HashMap<String, String>();
        map2.put("name", "赵云");
        map2.put("phone", "110");
        
        Map<String,String> map3 = new HashMap<String, String>();
        map3.put("name", "貂蝉");
        map3.put("phone", "138222");
        
        data.add(map1);
        data.add(map2);
        data.add(map3);
        
        SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),data,R.layout.item_2,
        		new String[]{"name","phone"},new int[]{R.id.tv_name,R.id.tv_phone});
        //【3】绑定适配器
        lv.setAdapter(adapter);
    }   
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android StudioArrayAdapterSimpleAdapter都是常用的适配器类,用于将数据源与UI控件进行绑定。下面我来分别介绍一下这两个适配器类的使用方法。 ### ArrayAdapter ArrayAdapter是一个简单的适配器,它可以将一个数组或List的数据与ListView等UI控件进行绑定。使用ArrayAdapter时,需要创建一个包含数据的数组或List,并将其传入ArrayAdapter的构造函数,然后将ArrayAdapter设置到对应的UI控件即可。 下面是一个简单使用ArrayAdapter的示例代码: ```java String[] data = {"apple", "banana", "orange", "pear"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); ``` 在上面的代码,我们创建了一个包含4个字符串的数组data,然后将其传入ArrayAdapter的构造函数,同时指定了一个简单ListView的布局文件`android.R.layout.simple_list_item_1`,最后将ArrayAdapter设置到ListView。 ### SimpleAdapter SimpleAdapter是一个稍微复杂一些的适配器,它可以将一个List的Map与ListView等UI控件进行绑定。使用SimpleAdapter时,需要创建一个包含多个Map的List,并将其传入SimpleAdapter的构造函数,然后将SimpleAdapter设置到对应的UI控件即可。 下面是一个简单使用SimpleAdapter的示例代码: ```java List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("name", "apple"); map1.put("image", R.drawable.apple); data.add(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("name", "banana"); map2.put("image", R.drawable.banana); data.add(map2); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("name", "orange"); map3.put("image", R.drawable.orange); data.add(map3); Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("name", "pear"); map4.put("image", R.drawable.pear); data.add(map4); SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_simple_adapter, new String[]{"name", "image"}, new int[]{R.id.tv_name, R.id.iv_image}); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); ``` 在上面的代码,我们创建了一个包含4个Map的List,每个Map包含两个键值对(name和image),分别对应水果的名称和图片资源的ID。然后将List传入SimpleAdapter的构造函数,同时指定了一个ListView的布局文件`R.layout.item_simple_adapter`,以及将name和image映射到ListView的两个控件(tv_name和iv_image)上。 总的来说,ArrayAdapterSimpleAdapter都是非常实用的适配器类,用于将数据源与UI控件进行绑定。在实际开发,我们可以根据具体的需求来选择使用哪个适配器类。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值