之前一直困扰listview刷新后位置的问题,现在才明白,原来不能重新设置listview的adapter,而应该用notifyDataSetChanged()来刷新,这样位置就不会置顶。
下面做了一个测试的例子,点击最后一条记录,会增加一条新的记录,
代码如下,使用继承SimpleAdapter
1.TestAdapter
package com.TestAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class TestAdapter extends Activity {
/** Called when the activity is first created. */
private int[] images ;
private ListView listview;
private MyAdapter adapter;
List
下面我们再来看看使用继承BaseAdapter来刷新listview数据:
public class PersonAdapter extends BaseAdapter {
private ArrayList mList;
private Context mContext;
public PersonAdapter(ArrayList list, Context context) {
mList = list;
mContext = context;
}
public void refresh(ArrayList list) {
mList = list;//传入list,然后调用notifyDataSetChanged方法
notifyDataSetChanged();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.list_item, null);
holder = new Holder();
holder.mNameText = (TextView)convertView.findViewById(R.id.name_text);
holder.mIDText = (TextView)convertView.findViewById(R.id.id_text);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.mNameText.setText(mList.get(getCount() - position - 1).getName());
holder.mIDText.setText(mList.get(getCount() - position - 1).getID());
return convertView;
}
class Holder {
private TextView mNameText, mIDText;
}
}
在使用这种类型时,在Activity使用mAdapter.notifyDataSetChanged()时候,有时候会发现数据不能够及时的更新。下面看一下主Activity:
public class ListViewRefreshActivity extends Activity {
private ListView mListView;
private ArrayList mList;
private PersonAdapter mAdapter;
private Handler mHandler;
private String mName, mID;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView)findViewById(R.id.listView);
mList = new ArrayList();
mAdapter = new PersonAdapter(mList, ListViewRefreshActivity.this);
mListView.setAdapter(mAdapter);
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mList.add((PersonBean) msg.obj);
Log.v("@@@@@@", "this is get message");
mAdapter.refresh(mList);
// mAdapter.notifyDataSetChanged();
}
};
// final Message message = new Message();
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
mName = "hao :" + i;
mID = "" + i;
PersonBean bean = new PersonBean();
bean.setID(mID);
bean.setName(mName);
Message message = new Message();
message.obj = bean;
Thread.sleep(3000);
mHandler.sendMessage(message);
// mHandler.sendMessageDelayed(message, 10000);
}}catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}