<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">使用Android的列表控件有几点要注意: </span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">一、要设计一个类来代表控件中的一行所要展示的数据,例如User类;</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">二、要设计一个layout来展示控件中的一行,例如user_item.xml;</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">三、要继承BaseAdapter类来包含列表控件中所有的数据列表,以及其展现方法;</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">四、要在列表控件所在的Activity中加入设置代码。</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">五、刷新列表控件时请使用BaseAdapter的notifyDataSetChanged方法和ListView类的invalidate方法。</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">具体使用分为以下五步:</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.在需要使用listview控件的layout中加入该控件;</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">2.listview控件的每一行对应一组数据,可以将这一组数据组成一个类,例如User类:</span>
public class User { private String id; private String name; private String phone; private String passwd; 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 String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } }
3.为listview控件的每一行设计一个layout,这个layout就是为了展示User类的实例,例如:
<?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" > <TextView android:id="@+id/id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="id" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" /> <TextView android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="phone" /> <TextView android:id="@+id/passwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="passwd" /> </LinearLayout>
4.为该listview设计一个Adapter类,这个类包含了listview所要展示的对象列表,并在其getView(int, View, ViewGroup)方法中详细描述了如何展示每一行数据,例如:
package com.dumaisoft.kidscore.adapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.dumaisoft.kidscore.R; import com.dumaisoft.kidscore.api.User; import com.dumaisoft.kidscore.db.dao.UserDao; /** * @author wxb * 用户的列表Adapter */ public class UserAdapter extends BaseAdapter { private Context context; private List<User> userlist; public UserAdapter(Context c){ context = c; userlist = new ArrayList<User>(); refreshData(); } @Override public void notifyDataSetChanged() { // TODO Auto-generated method stub super.notifyDataSetChanged(); refreshData(); } /** * 刷新用户列表,从数据库中重新读取 */ public void refreshData(){ userlist.clear(); //列出每一个User,加入队列 UserDao userdao = new UserDao(context); List<Map<String, String>> listmap = userdao.listUserMaps(null,null); for(int i =0;i<listmap.size();i++){ Map<String,String > map = new HashMap<String, String>(); map = listmap.get(i); User user = new User(); user.setId(map.get("user_id")); user.setName(map.get("user_name")); user.setPhone(map.get("user_phone")); user.setPasswd(map.get("user_password")); userlist.add(user); } } @Override public int getCount() { // TODO Auto-generated method stub return userlist.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return userlist.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate( R.layout.user_item, null); } User user = userlist.get(position); TextView id = (TextView) convertView.findViewById(R.id.id); id.setText(user.getId()); TextView name = (TextView) convertView.findViewById(R.id.name); name.setText(user.getName()); TextView phone = (TextView) convertView.findViewById(R.id.phone); phone.setText(user.getPhone()); TextView passwd = (TextView) convertView.findViewById(R.id.passwd); passwd.setText(user.getPasswd()); return convertView; } }
5.在Activity类中添加listview的设置代码:
package com.dumaisoft.kidscore.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import com.dumaisoft.kidscore.R; import com.dumaisoft.kidscore.adapter.UserAdapter; public class UserActivity extends Activity { private UserActivity _c = this; private ListView listview; private UserAdapter useradapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user_activity); this.findViewById(R.id.btn_user_add).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(_c,UserAddActivity.class)); } }); this.findViewById(R.id.btn_user_refresh).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((UserAdapter) listview.getAdapter()).notifyDataSetChanged(); listview.invalidate(); } }); useradapter = new UserAdapter(_c); listview = (ListView) this.findViewById(R.id.listUser); listview.setAdapter(useradapter); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }
时间: 2024-12-28 18:44:19