android 之 ListView相關

來源:互聯網
上載者:User

ListView是一種列表視圖,其將ListAdapter所提供的各個控制項顯示在一個垂直且可滾動的列表中。需要注意的為建立適配器並將其設定給ListView。

1.ArrayAdapter

ArrayAdapter由3個參數進行構造,第一個為Context,第二個為在R檔案中定義的Layout,也可用系統的R檔案,第三個參數是一個數組,數組中每一項的類型沒有限制。

系統預設的布局方式可通過android.R.layout.XX定義。

private static String[] data={"a","b","c","d"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.main);

            ListView listview=new ListView(this);
            ArrayAdapter adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
            listview.setAdapter(adapter);
            setContentView(listview);
    }

 

若自訂ListView中每一項TextView的樣式arraylayout.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:gravity="center_horizontal" />

Activity中,指定ArrayAdapter第二個參數為arraylayout.xml:

private static String[] data={"a","b","c","d"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.main);

            ListView listview=new ListView(this);
            ArrayAdapter adapter=new ArrayAdapter<String>(this,R.layout.arraylayout,data);
            listview.setAdapter(adapter);
            setContentView(listview);
    }

2 SimpleAdapter

SimpleAdapter的ArrayList裡的每一項都是一個Map<String,?>類型,每一項Map對象都和ListV中的一項進行資料繫結一一對應。

private ListView listview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listview=new ListView(this);
       data2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> item;
        item = new HashMap<String, Object>();
        item.put("姓名", "張三");
        item.put("性別", "男");
        item.put("年齡", "25");
        data2.add(item);
        item = new HashMap<String, Object>();
        item.put("姓名", "李四");
        item.put("性別", "男");
        item.put("年齡", "33");
        data2.add(item);
        item = new HashMap<String, Object>();
        item.put("姓名", "小王");
        item.put("性別", "女");
        item.put("年齡", "31");
        data2.add(item);
        SimpleAdapter adapter = new SimpleAdapter(this, data2,
                R.layout.simplelayout, new String[] { "姓名", "性別","年齡" }, new int[] {
                        R.id.tv01, R.id.tv02,R.id.tv03 });
        listview.setAdapter(adapter);
        setContentView(listview);

其中ListView中每項的布局檔案如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal">
    <TextView android:id="@+id/tv01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:width="150dp" />
    <TextView android:id="@+id/tv02" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:width="150dp"/>
    <TextView android:id="@+id/tv03" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:width="150dp"/>
</LinearLayout>

如果設定Activity的布局檔案包含不僅ListView,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:src="@drawable/img01"/>
    <ListView android:id="@+id/listview01" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:choiceMode="singleChoice" />
</LinearLayout>

在Activity中:

setContentView(R.layout.main);
        listview=(ListView) findViewById(R.id.listview01);
        listview.setAdapter(adapter);

3 BaseAdapter

public class mainActivity extends Activity {
    /** Called when the activity is first created. */
    int [] drawableIds={R.drawable.img01,R.drawable.img02,R.drawable.img03};
    int [] msgIds={R.string.str1,R.string.str2,R.string.str3};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.main);
        ListView listview=(ListView) findViewById(R.id.listview01);
        BaseAdapter ba=new BaseAdapter() {
           
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LinearLayout ll=new LinearLayout(mainActivity.this);
                ll.setOrientation(LinearLayout.HORIZONTAL);
                ll.setPadding(5, 5, 5, 5);
                ImageView ii=new ImageView(mainActivity.this);
                ii.setImageDrawable(getResources().getDrawable(drawableIds[position]));
                ii.setScaleType(ImageView.ScaleType.FIT_XY);
                ii.setLayoutParams(new Gallery.LayoutParams(50,50));
                ll.addView(ii);
                TextView tv=new TextView(mainActivity.this);
                tv.setText(getResources().getText(msgIds[position]));
                tv.setTextSize(24);
                tv.setTextColor(mainActivity.this.getResources().getColor(R.color.white));
                tv.setPadding(5, 5, 5, 5);
                tv.setGravity(Gravity.LEFT);
                ll.addView(tv);
                return ll;
            }
           
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }
           
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }
           
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 3;
            }
        };
        listview.setAdapter(ba);
    }
}

4 ListActivity

若使用ListActivity,則Activity裡的ListView將充滿螢幕。

在布局檔案中,必須定義一個ListView,其Id為@id/android:list;另一個需要定義但並不是必須的是id為@id/android:empty的TextView,其為ListView中無資料時顯示的內容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ListView android:id="@id/android:list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <TextView android:id="@id/android:empty" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:text="沒有任何資料" />
</LinearLayout>

Activity中,ListView每行設定和之前方法一樣。

String [] data={"a","b","c","d"};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
   
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.