Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> SimpleAdapter和Baseadapter填充listActivity-android學習之旅()

SimpleAdapter和Baseadapter填充listActivity-android學習之旅()

編輯:關於Android編程

簡介

SimpleAdapter的功能是能夠為AbsListView提供復雜的數據,需要構造ListView

代碼示例

package peng.liu.testview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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


public class MainActivity extends Activity {
    ListView myList = null;
    String[] names = new String[]{
            "hello","world", "hello","world"
    };
    String[] desc = new String[]{
            "java","c#","python","swift"
    };
    private int[] imageIds = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,
            R.drawable.ic_launcher,R.drawable.ic_launcher};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List> listItems = new ArrayList>();
        for (int i = 0;i map = new HashMap();
            map.put("header",imageIds[i]);
            map.put("name",names[i]);
            map.put("desc",desc[i]);
            listItems.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{
                "name","desc","header"
        },new int[]{R.id.name,R.id.desc,R.id.header});
        myList = (ListView) findViewById(R.id.myList);
        myList.setAdapter(adapter);
    }

}
 

監聽事件

有兩種方式:
1.OnItemClickListener()
2.OnItemSelectorListener()
這兩種方式都是基於AdapterView提供的。

BaseAdapter簡介

使用BaseAdapter可以獲得對Adapter的控制權,創建多少個列表項以及每個列表項的組件都可以在繼承的復寫方法裡面定義。

廢話不多說,代碼示例

package peng.liu.testview;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

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


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return 40;
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                LinearLayout line = new LinearLayout(MainActivity.this);
                line.setOrientation(LinearLayout.VERTICAL);
                ImageView image = new ImageView(MainActivity.this);
                image.setImageResource(R.drawable.ic_launcher);
                TextView text = new TextView(MainActivity.this);
                text.setTextSize(20);
                text.setTextColor(Color.RED);
                text.setText("第"+i+"個");
                line.addView(image);
                line.addView(text);
                return line;
            }
        };
        ListView myList = (ListView) findViewById(R.id.myList);
        myList.setAdapter(adapter);
    }
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved