ArrayAdapted 和 simpleAdapted

<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=".MainActivity" >

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

</RelativeLayout>
package com.example.listviewdemo;

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

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener,
        OnScrollListener {

    private ListView listView;
    private SimpleAdapter simple_adapter;
    private List<Map<String, Object>> list;
    private int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Æ¥Åä²¼¾ÖÎļþÖеÄListView¿Ø¼þ
        listView = (ListView) findViewById(R.id.listView);
        // Êý¾ÝÊÊÅäÆ÷µÄ¶¨Òå
        String[] data = new String[] { "java", "C++", "JavaScript", "Php",
                "Python" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1, data);
        // ¸øListViewÉèÖÃÊý¾ÝÊÊÅäÆ÷
         listView.setAdapter(adapter);
        // ÉèÖÃListViewµÄÔªËر»Ñ¡ÖÐʱµÄʼþ´¦Àí¼àÌýÆ÷
        listView.setOnItemClickListener(this);
//        getData();
        // ÉèÖÃSimpleAdapter¼àÌýÆ÷
//        simple_adapter = new SimpleAdapter(MainActivity.this,
//                list, R.layout.list_item,
//                new String[] { "image", "text" }, new int[] { R.id.image,
//                        R.id.text });
//        listView.setAdapter(simple_adapter);
//        listView.setOnScrollListener(this);
    }

    // ¼ÓÔØSimpleAdapterÊý¾Ý¼¯
    private List<Map<String, Object>> getData() {
        list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("text", "java");
        map.put("image", R.drawable.ic_launcher);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("text", "C++");
        map2.put("image", R.drawable.ic_launcher);
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("text", "JavaScript");
        map3.put("image", R.drawable.ic_launcher);
        Map<String, Object> map4 = new HashMap<String, Object>();
        map4.put("text", "Php");
        map4.put("image", R.drawable.ic_launcher);
        Map<String, Object> map5 = new HashMap<String, Object>();
        map5.put("text", "Python2");
        map5.put("image", R.drawable.ic_launcher);
        list.add(map);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map5);
        Log.i("Main", list.size() + "");
        return list;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // (5)ʼþ´¦Àí¼àÌýÆ÷·½·¨
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        // »ñÈ¡µã»÷ListView itemÖеÄÄÚÈÝÐÅÏ¢
        String text = listView.getItemAtPosition(position) + "";
        // µ¯³öToastÐÅÏ¢ÏÔʾµã»÷λÖúÍÄÚÈÝ
        Toast.makeText(MainActivity.this,
                "position=" + position + " content=" + text, 0).show();

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        // ÊÖÖ¸À뿪ÆÁĻǰ£¬ÓÃÁ¦»¬ÁËÒ»ÏÂ
        if (scrollState == SCROLL_STATE_FLING) {
            Toast.makeText(MainActivity.this, "ÓÃÁ¦»¬Ò»ÏÂ",0).show();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("text", "¹ö¶¯Ìí¼Ó "+i++);
            map.put("image", R.drawable.ic_launcher);
            list.add(map);
            listView.setAdapter(simple_adapter);
            simple_adapter.notifyDataSetChanged();
        } else
        // Í£Ö¹¹ö¶¯
        if (scrollState == SCROLL_STATE_IDLE) {

        } else
        // ÕýÔÚ¹ö¶¯
        if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {

        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub

    }

}
<?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:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="25sp" />

</LinearLayout>
时间: 2024-11-05 10:43:57

ArrayAdapted 和 simpleAdapted的相关文章