筆記20 使用ArrayAdapter、ArrayAdapter為ListView新增資料
阿新 • • 發佈:2019-02-18
目標
1、理解ListView的基礎使用
2、學會熟練運用兩種介面卡(ArrayAdapter、SimpleAdapter)3、學會熟練運用兩種監聽器(OnScrollListener,OnItemClickListener)
4、學會熟練運用介面卡資料的重新整理(notifyDataChanged)
MainActivity
package com.example.adapter; 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; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AbsListView.OnScrollListener { private static final String TAG = "MainActivity"; private ListView mListView; private ArrayAdapter<String> mArrayAdapter; private SimpleAdapter mSimpleAdapter; private List<Map<String, Object>> dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 匹配佈局檔案中的ListView控制元件 setContentView(R.layout.main); mListView = (ListView) findViewById(R.id.listview); //1、新建一個數據介面卡 //ArrayAdapter(上下文,當前listView載入的每一個列表項對應的佈局檔案,資料來源) /** * SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, * String[] from, int[] to) * Context context: 上下文 * List<? extends Map<String, ?>> data :資料來源 一個map所組成的Listj集合 * 每一個map都會對應listview裡的一行 * 每一個map(鍵-值對)中的鍵必須包含所有from中指定的鍵 * int resource :列表項的佈局檔案 * String[] from :map中的鍵名 * int[] to :繫結資料檢視中的ID,與from成對應關係 */ //2、介面卡載入資料來源 String[] arr_data = {"ban1", "ban2", "ban3"}; mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data); dataList = new ArrayList<Map<String, Object>>(); mSimpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic", "text"}, new int[]{R.id.pic, R.id.text}); //3、檢視(ListView)載入介面卡 //mListView.setAdapter(mArrayAdapter); mListView.setAdapter(mSimpleAdapter); mListView.setOnItemClickListener(this); mListView.setOnScrollListener(this); } private List<Map<String, Object>> getData() { for (int i = 0; i < 20; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("pic", R.mipmap.ic_launcher); map.put("text", "ban" + i); dataList.add(map); } return dataList; } @Override public void onScrollStateChanged(AbsListView absListView, int scrollState) { switch (scrollState) { case SCROLL_STATE_FLING: Log.e(TAG, "onScrollStateChanged: SCROLL_STATE_FLING"); Toast.makeText(MainActivity.this, "用力滑一下",Toast.LENGTH_SHORT).show(); Map<String,Object> map = new HashMap<String,Object>(); map.put("pic",R.mipmap.ic_launcher); map.put("text","增加項"); dataList.add(map); mSimpleAdapter.notifyDataSetChanged(); break; case SCROLL_STATE_IDLE: Log.e(TAG, "onScrollStateChanged: SCROLL_STATE_IDLE"); break; case SCROLL_STATE_TOUCH_SCROLL: Log.e(TAG, "onScrollStateChanged: SCROLL_STATE_TOUCH_SCROLL"); break; } } @Override public void onScroll(AbsListView absListView, int i, int i1, int i2) { } @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { // 獲取點選ListView item中的內容資訊 String text = mListView.getItemAtPosition(i) + ""; // 彈出Toast資訊顯示點選位置和內容 Toast.makeText(MainActivity.this, "position=" + i + " content=" + text, Toast.LENGTH_SHORT).show(); } }
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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.adapter.MainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
item.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="50dp" android:background="@color/colorAccent" android:gravity="center" > <ImageView android:id="@+id/pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="demo" android:textSize="20dp" /> </LinearLayout>
執行效果: