Android复习练习十一(ListView列表显示使用BaseAdapter以及ListView动态刷新)

来源:互联网 发布:局域网网络监控 编辑:程序博客网 时间:2024/06/16 10:36

需求:结合SQLite数据库,实现一个输入个人名字和年龄信息的小应用,这里的Adapter使用BaseAdaper的实现类

效果:


ListView控件的使用步骤:

1、使用findViewById方法得到ListView对象lv

2、对lv进行绑定Adapter,这是ListView必须的,这里使用一个内部类MyAdapter来实现绑定,MyAdapter继承自BaseAdapter

3、继承BaseAdapter必须要实现四个方法,其中比较重要的两个方法是:getCount和getView,getCount没什么直接返回lv要显示的集合的大小,比如personlist.size();对于getView方法,是有些复杂的,但是基本机理其实也是ListView的机理:ListView并不是一次将所有的personlist集合中的Item元素都加载进内存,而是实现了Item的复用,屏幕显示多少个Item那么就加载多少个Item,有时会加载多一个,因为翻页时可能会在上下显示两个一半的Item,当一个Item完全消失时,他的内存空间会被空出来,来加载下一个将要显示的Item。而在这里getView的思路就是这样的:如果有空闲的缓冲view,那么直接返回这个view,如果没有,那么证明这是第一次刷新ListView,那么就新建一个view对象返回去。


本次练习中有两个小地方需要注意一下:

一、如何实现ListView的动态刷新。一般方法就是使用adapter.notifyDataSetChanged();这个方法,但是要注意的是,只在按键的点击方法中写这么一句是起不了作用的,因为这个方法是刷新原始的personlist数据,在本程序中也就是PersonDao类对象传过来的personlist,如果你在Activity中重新new过一个list赋值给personlist,那么很可能就起不了作用,因为这时personlist已经指向了另外一个数据,你再去刷新它原本指向的内存空间,是起不了作用的,那么应当如何做呢,这是应该将PersonDao类中的personlist设为public,当传过来的时候就将再不要改变他的指向,如果需要对personlist进行更新,那么使用下面两句代码,这样就可以了

personlist.clear();personlist.addAll(newlist);


二、实现listview列表中隔行样式和背景色不一样,这个原理比较简单,在adapter中的getView方法中,使用posion%2来判断即可。还有一个小细节就是,如果要设置这里的Item的左边小图标,要使用 TextView.setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)这个方法,但是如果直接传一个drawable = getResources().getDrawable(R.drawable.qq);进去是不会显示的,还需要设置一下drawable的显示大小:drawable.Drawable.setBounds(int left, int top, int right, int bottom)

代码如下:

Activity:

package com.alexchen.listviewbaseadapter;import java.util.List;import android.app.Activity;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.BaseAdapter;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.alexchen.listviewbaseadapter.dao.PersonDao;import com.alexchen.listviewbaseadapter.entity.Person;public class MainActivity extends Activity {private ListView lv;private List<Person> personlist;private PersonDao dao;private Person person;private EditText et_name;private EditText et_age;private final String NULLSRTING = "";private TextView tv;private View inflateView;private View view;private LayoutInflater inflater;private MyAdapter adapter;private int[] colors = { R.color.light_blue, R.color.light_orange };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 去掉标题requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);dao = new PersonDao(this, "personinfo.db", null, 1);personlist = dao.queryAll();if (personlist == null || personlist.size() == 0) {return;}et_name = (EditText) findViewById(R.id.et_name);et_age = (EditText) findViewById(R.id.et_age);lv = (ListView) findViewById(R.id.listview);adapter = new MyAdapter();lv.setAdapter(adapter);}public void click(View view) {String name = et_name.getText().toString().trim();String age = et_age.getText().toString().trim();if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(age)&& Integer.valueOf(age) > 0) {dao.insert(new Person(0, name, Integer.valueOf(age)));// ---------------------------动态刷新机制--------------------------------dao.personlist.clear();dao.personlist.addAll(dao.queryAll());adapter.notifyDataSetChanged();// ---------------------------动态刷新机制--------------------------------} else {Toast.makeText(MainActivity.this, "请输入正确的姓名和年龄", 0).show();et_name.setText(NULLSRTING);et_age.setText(NULLSRTING);return;}}public class MyAdapter extends BaseAdapter {private Drawable drawable;@Overridepublic View getView(int position, View convertView, ViewGroup parent) {view = null;if (convertView != null) {view = convertView;} else {inflater = getLayoutInflater();view = inflater.inflate(R.layout.listview_item, null);}tv = (TextView) view.findViewById(R.id.tv);//----------------------------listview隔行效果-------------------------------if (position % 2 == 0) {tv.setBackgroundColor(getResources().getColor(R.color.light_blue));drawable = getResources().getDrawable(R.drawable.qq);drawable.setBounds(0, 0, drawable.getMinimumWidth() / 2,drawable.getMinimumHeight() / 2);tv.setCompoundDrawables(drawable, null, null, null);} else {tv.setBackgroundColor(getResources().getColor(R.color.light_orange));drawable = getResources().getDrawable(R.drawable.xunlei);drawable.setBounds(0, 0, drawable.getMinimumWidth() / 2,drawable.getMinimumHeight() / 2);tv.setCompoundDrawables(drawable, null, null, null);}//----------------------------listview隔行效果-------------------------------if (personlist != null && personlist.size() > 0) {person = personlist.get(position);tv.setTextSize(23);tv.setText("姓名:" + person.getName() + "\n\n年龄:"+ person.getAge());}return view;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic int getCount() {return personlist.size();}}}



0 0
原创粉丝点击