ListView和数据适配器SimpleAdapter例子

一:activity_main.xml文件,主屏幕

<?xml version="1.0" encoding="utf-8"?>
<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"
    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.demo.yaokj.demo.MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

二、因为 SimpleAdapter简单数据适配器,会用到一个样式的xml,也先放出来,item.xm

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


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:src="@drawable/qq20160429002618"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:text="demo"
        />

</LinearLayout>

三、activity类的实现:

package com.demo.yaokj.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener{
    private ListView listView;
    private ArrayAdapter<String> arrayAdapter;
    private SimpleAdapter simpleAdapter;
    private List<Map<String,Object>> datas;

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

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


        listView.setOnItemClickListener(this);//这个必须要有
        listView.setOnScrollListener(this);
        //generateArrayAdapter();

        generateSimpleAdapter();


    }

    /**
     * 生成简单适配器
     */
    private void generateSimpleAdapter(){
        simpleAdapter = new SimpleAdapter(this,getDatas(),
                R.layout.item,new String[]{"pic","text"},
                new int[]{R.id.imageView,R.id.textView});

        listView.setAdapter(simpleAdapter);
    }

    private List<Map<String,Object>> getDatas(){
        datas = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            Map<String,Object> data = new HashMap<>();
            data.put("pic",R.drawable.qq20160429002618);
            data.put("text","simpleAdapter yaokj_"+i);
            datas.add(data);
        }

        return datas;
    }

    /**
     * 生成数组适配器
     */
    private void generateArrayAdapter(){
        List<String> objects = new ArrayList<>();
        objects.add("arrayAdapter yaokj 1");
        objects.add("arrayAdapter yaokj 2");
        objects.add("arrayAdapter yaokj 3");
        objects.add("arrayAdapter yaokj 4");
        objects.add("arrayAdapter yaokj 5");
        arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,objects);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String text = listView.getCheckedItemPosition()+"";
        Toast.makeText(this,"positin="+position+" text="+text,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case SCROLL_STATE_FLING:
                Log.i("main","scroll_fling,手指没有放开");
                break;
            case SCROLL_STATE_IDLE:
                Log.i("main","scroll_idle,拖动结束时触发,手指放开了");
                Map<String,Object> data = new HashMap<>();
                data.put("pic",R.drawable.qq20160429002618);
                data.put("text","拖动时添加的选项");
                datas.add(data);
                simpleAdapter.notifyDataSetChanged();//必须要通知简单适配器数据发生变化了
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.i("main","scroll_touch_scroll:触摸并拖动时触发,手指没有放开");
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        Log.i("main","on scroll滚动");
    }
}


你可能感兴趣的:(android,ListView,SimpleAdapter)