ArrayAdapter

1、视图

1)主视图

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".MainActivity" >
 6 
 7     <ListView 
 8          android:layout_width="match_parent"
 9          android:layout_height="match_parent"
10          android:id="@+id/lv"
11         ></ListView>
12 
13 </RelativeLayout>

2)item视图

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal">"
 6     
 7     <ImageView 
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:src="@drawable/ic_launcher"
11         />
12     <TextView 
13         android:layout_width="match_parent"
14         android:layout_height="match_parent"
15         android:id="@+id/tv"
16         />
17 
18 </LinearLayout>

2、java代码

package com.example.arrayadapter;

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

public class MainActivity extends Activity {

    private static String[] names = {"功能一","功能二","功能三","功能四","功能五","功能六",};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView lv = (ListView)findViewById(R.id.lv);
        lv.setAdapter(new ArrayAdapter(this, R.layout.list_item, R.id.tv, names));
    }

}

 注意:适配器的简单设法为

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, objects);

 

posted @ 2016-04-14 15:59  zhongyinghe  阅读(135)  评论(0)    收藏  举报