在使用列表视图(ListView)时,重要的是如何设置选项的内容。如果我们没有在布局文件中使用entries属性设置ListView需要显示的列表项,那么可以在代码中通过Adapter来指定需要显示的列表项。
通过Apdapter来为ListView指定要显示的列表项,可以分为两个步骤:
1.创建Apdapter对象 ArrayAdapter<T> adapter = new AarrayAdapter<T>(Context,android.R.layout.simple_list_item_1,List<T>); 或者 ArrayAdapter<T> adapter = ArrayAdapter.createFromResource(Context,R.array.etype,android.R.layout.simple_list_item_1); R.array.etype:配置的资源数组 2.创建的适配器对象与ListView相关联 listView.setAdapter(adapter);
使用ListView绑定用户信息。需显示出用户的名字,但单击某项时需要获取用户的全部信息。
布局:
listView.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.myandroid_listview.MainActivity" > <ListView android:id="@+id/list_view" android:layout_height="wrap_content" android:layout_width="match_parent" android:divider="@drawable/greendivider" android:dividerHeight="3px" ></ListView> </RelativeLayout>
接着 user_infoxml 即列表的每一项需要显示的数据。
<?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="vertical" > <TextView android:id="@+id/tv_user_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10px" android:paddingBottom="10px" android:textSize="40px" /> </LinearLayout>
用户实体:
public class UserInfo { private String id; private String name; private int age; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "id:"+this.getId()+",Name:"+this.getName()+",Age:"+this.getAge()+",Add:"+this.getAddress(); } }
自定义用户适配器,绑定数据,显示:
package com.example.myandroid_listview; import java.util.LinkedList; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class UserInfoAdapter extends BaseAdapter { private final List<UserInfo> infos = new LinkedList<UserInfo>(); private final Context context; public UserInfoAdapter(Context context, List<UserInfo> userInfos) { this.infos.clear(); this.infos.addAll(userInfos); this.context = context; } @Override public int getCount() { return this.infos.size(); } @Override public Object getItem(int position) { return this.infos.get(position); } @Override public long getItemId(int position) { return position; } /* * 绘制 * (non-Javadoc) * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) */ @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { View view = LayoutInflater.from(this.context).inflate(R.layout.user_info, null); TextView userTextView = (TextView)view.findViewById(R.id.tv_user_info); userTextView.setText(this.infos.get(position).getName()); return view; } return convertView; } }
主程序:
public class ListViewItemActivity extends Activity { /* * 声明一个List对象 */ private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.listView = (ListView) findViewById(R.id.list_view); // 设置list_view的项 List<UserInfo> infos = this.getUserInfos(); this..setAdapter(new UserInfoAdapter(this,infos)); // 设置单击事件 this.listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { UserInfo info = (UserInfo)arg0.getItemAtPosition(pos); Log.i("listView.ItemClick","用户信息:"+info.toString()); Toast.makeText(ListViewItemActivity.this,info.toString(), Toast.LENGTH_SHORT).show(); } }); } private List<UserInfo> getUserInfos() { List<UserInfo> userInfos = new ArrayList<UserInfo>(); int i = 1; while (i < 11) { UserInfo user = new UserInfo(); user.setId(i+""); user.setName("名字_"+i); user.setAddress("地址_"+i); user.setAge(i+40); userInfos.add(user); i++; } return userInfos; } }
运行效果: