ArrayAdapter,SimpleAdapter

Posted 尚文韬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ArrayAdapter,SimpleAdapter相关的知识,希望对你有一定的参考价值。

1--ArrayAdapter

  ListViewActivity.java
package com.example.lenovo.textapp4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListViewActivity extends AppCompatActivity {

    ListView lv_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        ListView lv_1=(ListView)findViewById(R.id.lv_1);

        //1-数据集合   Layout文件
        String []str={"A1","A2","A3","A4","A5","A6","A7","A8","A9","B1","B2","B3","B4","B5","B6","B7","B8","B9"};

        //2-创建Adapter
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,R.layout.array_adapter,str);

        //3-绑定到ListView
        lv_1.setAdapter(arrayAdapter);
    }
}
View Code

 

  activity_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lenovo.textapp4.ListViewActivity">

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

    </ListView>

</LinearLayout>
View Code

 

  array_adapter.xml

<?xml version="1.0" encoding="utf-8"?>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    android:textSize="20sp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"/>
View Code

 

 

 

2--SimpleAdapter

 

  List2Activity.java

package com.example.lenovo.textapp4;

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

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

public class List2Activity extends AppCompatActivity {

    ListView lv_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list2);

        lv_2=(ListView)findViewById(R.id.lv_2);

        //1-数据集合   Layout文件
        List<Map<String,Object>> lm=new ArrayList<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String,Object>();

        map.put("img",R.drawable.f1);
        map.put("name","美食1");
        map.put("content","美食1的美食介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f2);
        map.put("name", "美食2");
        map.put("content","美食2的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f3);
        map.put("name", "美食3");
        map.put("content","美食3的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f4);
        map.put("name", "美食4");
        map.put("content","美食4的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f5);
        map.put("name", "美食5");
        map.put("content","美食5的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f6);
        map.put("name", "美食6");
        map.put("content","美食6的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f7);
        map.put("name", "美食7");
        map.put("content","美食7的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f8);
        map.put("name", "美食8");
        map.put("content","美食8的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f9);
        map.put("name", "美食9");
        map.put("content","美食9的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f10);
        map.put("name", "美食10");
        map.put("content","美食10的美食介绍");
        lm.add(map);

        //数组 key的数组
        String [] strings={"img","name","content"};

        int [] ids={R.id.iv_2,R.id.tv_7,R.id.tv_8};

        //2-创建simpleadapter
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,lm,R.layout.simpleadapter_activity,strings,ids);

        //绑定

        lv_2.setAdapter(simpleAdapter);
    }
}
package com.example.lenovo.textapp4;

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

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

public class List2Activity extends AppCompatActivity {

    ListView lv_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list2);

        lv_2=(ListView)findViewById(R.id.lv_2);

        //1-数据集合   Layout文件
        List<Map<String,Object>> lm=new ArrayList<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String,Object>();

        map.put("img",R.drawable.f1);
        map.put("name","美食1");
        map.put("content","美食1的美食介绍");
        lm.add(map);

        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f2);
        map.put("name", "美食2");
        map.put("content","美食2的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f3);
        map.put("name", "美食3");
        map.put("content","美食3的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f4);
        map.put("name", "美食4");
        map.put("content","美食4的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f5);
        map.put("name", "美食5");
        map.put("content","美食5的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f6);
        map.put("name", "美食6");
        map.put("content","美食6的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f7);
        map.put("name", "美食7");
        map.put("content","美食7的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f8);
        map.put("name", "美食8");
        map.put("content","美食8的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f9);
        map.put("name", "美食9");
        map.put("content","美食9的美食介绍");
        lm.add(map);
        map=new HashMap<String,Object>();
        map.put("img",R.drawable.f10);
        map.put("name", "美食10");
        map.put("content","美食10的美食介绍");
        lm.add(map);

        //数组 key的数组
        String [] strings={"img","name","content"};

        int [] ids={R.id.iv_2,R.id.tv_7,R.id.tv_8};

        //2-创建simpleadapter
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,lm,R.layout.simpleadapter_activity,strings,ids);

        //绑定

        lv_2.setAdapter(simpleAdapter);
    }
}
View Code

 

  activity_list2.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lenovo.textapp4.List2Activity">

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


    </ListView>

</LinearLayout>
View Code

 

  simpleadapter_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/f1"
        android:id="@+id/iv_2"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_margin="20dp"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="名字"
            android:id="@+id/tv_7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="内容"
            android:id="@+id/tv_8"/>


    </LinearLayout>
</LinearLayout>
View Code

 

 




以上是关于ArrayAdapter,SimpleAdapter的主要内容,如果未能解决你的问题,请参考以下文章

ArrayAdapter,SimpleAdapter

android笔记:ListView及ArrayAdapter

Android之ArrayAdapter使用

ArrayAdapter的使用

Android_适配器(adapter)之ArrayAdapter

使用带有 ArrayAdapter<Objects> 而不是 ArrayAdapter<Strings> 的 Android AutoCompleteTextView