这里简要介绍一下使用ArrayAdapter构建动态的ListView列表。
先看例子:
package com.example.arrayadapterdemos2;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1 = (ListView)findViewById(R.id.list1);
List<User> users = new ArrayList<User>();
Switch switch_ = new Switch(this);
Button button = new Button(this);
User user1 = new User("Chris Yang",1001,"Helo");
User user2 = new User("Kevin Li",1002,"GoodBye");
User user3 = new User("John Feng",1003,"Welcome");
users.add(user1);
users.add(user2);
users.add(user3);
UserListAdapter adapter = new UserListAdapter(this,R.layout.user_list_item,users);
listView1.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class UserListAdapter extends ArrayAdapter<User>{
private int resourceId;
public UserListAdapter(Context context, int textResourceId, List<User> objects) {
super(context, textResourceId, objects);
this.resourceId = textResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
User item = getItem(position);
LinearLayout userList = new LinearLayout(getContext());
/*
* public View inflate (int resource, ViewGroup root, boolean attachToRoot)
* resource--ID for an XML layout resource to load (e.g., R.layout.main_page)
* root--Optional view to be the parent of the generated hierarchy (if attachToRoot is true),or else simply an object
* that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRootis false.)
* attachRoot--Whether the inflated hierarchy should be attached to the root parameter.
*returns--The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root;
otherwise it is the root of the inflated XML file.
*/
/*
* 生成由资源resourceId对应的布局文件的新的布局,新布局为userList
*/
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi.inflate(resourceId, userList,true);
TextView userName = (TextView)userList.findViewById(R.id.username);
TextView askedNum = (TextView)userList.findViewById(R.id.askednum);
TextView lastMsg = (TextView)userList.findViewById(R.id.lastmsg);
userName.setText(item.getUsername());
askedNum.setText(String.valueOf(item.getAskedNum()));
lastMsg.setText(item.getLastMsg());
return userList;
}
}
}
所用到的布局文件如下:
res/layout/activity_main.xml
<?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/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#f00"
android:dividerHeight="2dp">
</ListView>
</LinearLayout>
res/layout/user_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="#f0f"
android:text = "Hello"
/>
<TextView
android:id="@+id/lastmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="#f00"
android:text="Your are welcome" />
<TextView
android:id="@+id/askednum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="#00f"
android:text="World" />
</LinearLayout>