1. 程式人生 > >ListView(2018.5.11)

ListView(2018.5.11)

ListView理解

ListView是一種用來顯示多個可滑動項(Item)列表ViewGroup

需要使用Adapter集合資料和每一個Item所對應的佈局動態適配到ListView中顯示

顯示列表: listView.setAdapter(adapter)

更新列表:adapter.notifyDataSetChanged()

———————————————————————————

ListView效果樣例

  

Adapter

ArrayAdapter顯示最簡單的列表(文字)

集合資料為List<String>String[]

SimpleAdapter:顯示覆雜的列表

集合資料必須是

List<Map<String,Object>>型別

BaseAdapter:顯示覆雜的列表

集合資料可以是任意型別的集合List<Xxx>

SimpleCursorAdapter:顯示覆雜的列表

集合資料是資料庫查詢結果集

————————————————————————————————————————————————————

ListView+ ArrayAdapter


context: 上下檔案物件, 一般為Acivity物件

resource:Item的佈局檔案標識

objects: 需要顯示的資料集合(ArrayList)

————————————————————————————————————————————————————

ListView+ SimpleAdapter

context: 上下檔案物件, 一般為Acivity物件

data: 需要顯示的資料集合

resource:Item佈局檔案標識

from: map物件中的key的陣列, 用於得到對應的value

to:Item佈局檔案中的子viewid的陣列


————————————————————————————————————————————————————

ListView+ BaseAdapter

classMyBaseAdapterextends BaseAdapter{

@Override

public intgetCount() {

  return0;

}

@Override

public Object getItem(int position) {

  returnnull;

}

@Override

public long getItemId(int position) {

  return0;

}

@Override

public View getView(int position,

  ViewconvertView, ViewGroup parent) {

  returnnull;

}

}

————————————————————————————————————————————————————

package com.example.test08_listvieew;


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






import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class CopyOfMainActivity_Simple extends Activity {
private ListView lv_main;
private List<ShoppInfo> data;//商品資訊資料來源
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


lv_main=(ListView) findViewById(R.id.lv_main);
//準備集合資料
data=new ArrayList<ShoppInfo>();
data.add(new ShoppInfo(R.drawable.f1,"美食名稱--1","美食的內容---1"));
data.add(new ShoppInfo(R.drawable.f2,"美食名稱--2","美食的內容---2"));
data.add(new ShoppInfo(R.drawable.f3,"美食名稱--3","美食的內容---3"));
data.add(new ShoppInfo(R.drawable.f4,"美食名稱--4","美食的內容---4"));
data.add(new ShoppInfo(R.drawable.f5,"美食名稱--5","美食的內容---5"));
data.add(new ShoppInfo(R.drawable.f6,"美食名稱--6","美食的內容---6"));
data.add(new ShoppInfo(R.drawable.f7,"美食名稱--7","美食的內容---7"));
data.add(new ShoppInfo(R.drawable.f8,"美食名稱--8","美食的內容---8"));
data.add(new ShoppInfo(R.drawable.f9,"美食名稱--9","美食的內容---9"));
data.add(new ShoppInfo(R.drawable.f10,"美食名稱--10","美食的內容---10"));


//準備介面卡物件   BaseAdatper
MyAdapter adapter = new MyAdapter();
//
lv_main.setAdapter(adapter);

}

package com.example.test08_listvieew;


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


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


public class MainActivity_Array extends Activity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lv_main=(ListView) findViewById(R.id.lv_main);
//準備集合 
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();

HashMap<String, Object> mp = new HashMap<String, Object>();
mp.put("icon", R.drawable.f1);
mp.put("name", "美食---1");
mp.put("content", "內容--1");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f2);
mp.put("name", "美食---2");
mp.put("content", "內容--2");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f3);
mp.put("name", "美食---3");
mp.put("content", "內容--3");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon",R.drawable.f4);
mp.put("name", "美食---4");
mp.put("content", "內容--4");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f5);
mp.put("name", "美食---5");
mp.put("content", "內容--5");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f6);
mp.put("name", "美食---6");
mp.put("content", "內容--6");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f7);
mp.put("name", "美食---7");
mp.put("content", "內容--7");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f8);
mp.put("name", "美食---8");
mp.put("content", "內容--8");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f9);
mp.put("name", "美食---9");
mp.put("content", "內容--9");
data.add(mp);

mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f10);
mp.put("name", "美食---10");
mp.put("content", "內容--10");
data.add(mp);

//map物件的key陣列 ,用於得到對應的value
String[] from={"icon","name","content"};
//item佈局檔案中的子View的id的陣列
int[] to={R.id.iv_item_icon,R.id.iv_item_name,R.id.iv_item_content};

//準備SimpleAdapter物件
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_simple_adapter, from, to);
//設定Adapter顯示列表
lv_main.setAdapter(adapter);
}

}

class MyAdapter extends BaseAdapter{


@Override//返回集合的總數
public int getCount() {
return data.size();
}


@Override//返回指定下標對應的資料物件
public Object getItem(int position) {
return data.get(position);
}


@Override//返回每個條目的id 下標
public long getItemId(int position) {
return position;
}


@Override//返回指定下標所對應(Item)的檢視物件
//position 下標           parent  listView物件
//convertView 可複用Item的檢視物件   前n+1個為null
public View getView(int position, View convertView, ViewGroup parent) {
ViewHodler  holder=null;
//如果沒有複用
if (convertView==null) {
//載入條目(Item)佈局 得到檢視(View)物件
convertView= View.inflate(CopyOfMainActivity_Simple.this,
R.layout.item_simple_adapter, null);
holder=new ViewHodler();

holder.inv = (ImageView) convertView.findViewById(R.id.iv_item_icon);
holder.nametext = (TextView) convertView.findViewById(R.id.iv_item_name);
holder.inv2 = (TextView) convertView.findViewById(R.id.iv_item_content);
//將holder 儲存在convertView上
convertView.setTag(holder);
}else{
//複用時     取出holder
holder=(ViewHodler) convertView.getTag();
}
//2. 得到當前行的物件
ShoppInfo shoppInfo = data.get(position);
//3.給ViewHolder物件的資料設定資料
holder.inv.setImageResource(shoppInfo.getIcon());
holder.nametext.setText(shoppInfo.getName());
holder.inv2.setText(shoppInfo.getContent());
return convertView;
}//檢視容器類
class ViewHodler{
public ImageView inv;
public TextView  nametext;
public TextView  inv2;
}
}

}

package com.example.test08_listvieew;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends Activity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lv_main=(ListView) findViewById(R.id.lv_main);
//準備集合 
String[] data={"A","B","C","D","E","F","G","H","J","K","L","O"};

//準備介面卡ArrayAdapter物件
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this, R.layout.item_array,data);
//設定adapter顯示列表
lv_main.setAdapter(adapter);
}

}

<?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:orientation="horizontal" >


    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/ic_launcher" />


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:gravity="center_vertical"
        android:orientation="vertical" 
        android:layout_marginLeft="10dp"
        >


        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />


        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>


</LinearLayout>

<?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:orientation="horizontal" >


    <ImageView
        android:id="@+id/iv_item_icon"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/f1" />


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"
         >


        <TextView
            android:id="@+id/iv_item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" 
            
            />


        <TextView
            android:id="@+id/iv_item_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />


    </LinearLayout>


</LinearLayout>

package com.example.test08_listvieew;


public class ShoppInfo {
 private int icon;
 private String name;
 private String content;
public ShoppInfo() {
super();
}
public ShoppInfo(int icon, String name, String content) {
super();
this.icon = icon;
this.name = name;
this.content = content;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "ShoppInfo [icon=" + icon + ", name=" + name + ", content="
+ content + "]";
}
 
}