android中ListView数据混乱问题
在ListView没有拖动前数据是一种,上下拖动后数据又是一种,就是说数据在不断变化中。如何解决。。。
想了很久也不知道在哪里出了问题,.问题有三点:
第一.
我的ViewHolder写的有问题.如下:
private class ViewHodler
{
public static TextView t_Subject;
public static TextView t_Author;
public static TextView t_Dateline;
public static ImageView t_photo;
public static TextView t_Replies;
}
我将ViewHodler里的每个属性都设置成了static.
static,说明只用一个内存空间,而getView时,每个Item的数据不一样,所以不能用static
第二.
没有重置ViewHodler.
if (convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.m_digesttopiclist_item,
null);
hodler = new ViewHodler();
hodler.t_Subject = (TextView) convertView
.findViewById(R.m_digesttopicid.subject);
hodler.t_Author = (TextView) convertView
.findViewById(R.m_digesttopicid.author);
hodler.t_Dateline = (TextView) convertView
.findViewById(R.m_digesttopicid.dateline);
hodler.t_photo = (ImageView) convertView
.findViewById(R.m_digesttopicid.userimage);
hodler.t_Replies = (TextView) convertView
.findViewById(R.m_digesttopicid.replies);
hodler.t_photo.setTag(oAuthorphoto);
convertView.setTag(hodler);
}else {
hodler = (ViewHodler) convertView.getTag();
resetViewHolder(hodler);
}
protected void resetViewHolder(ViewHodler p_ViewHolder)
{
p_ViewHolder.t_Subject.setText(null);
p_ViewHolder.t_Author.setText(null);
p_ViewHolder.t_Dateline.setText(null);
p_ViewHolder.t_photo.setImageDrawable(null);
p_ViewHolder.t_Replies.setText(null);
}
第三.
头像没有绑定tag
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHodler hodler = null;
String oTid =null;
String oSubject = null;
String oAuthor = null;
String oDateline = null;
String oReplies = null;
String odateStr = null;
String oAuthorphoto=null;
try
{
ExtJSONObject oDigestItem = mForumJsonArray.getExtJSONObject(position);
oTid = oDigestItem.getString(“tid”);
oSubject = oDigestItem.getString(“subject”);
oAuthor = oDigestItem.getString(“author”);
oDateline =oDigestItem.getString(“dateline”);
odateStr = AppUtility.formatRelativeTime(Integer
.parseInt(oDateline));
Log.i(“TAG”, “date= ” + oDateline);
oReplies = oDigestItem.getString(“replies”);
oAuthorphoto=oDigestItem.getString(“authorid”);
}
catch (JSONException e)
{
e.printStackTrace();
}
if (convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.m_digesttopiclist_item,
null);
hodler = new ViewHodler();
hodler.t_Subject = (TextView) convertView
.findViewById(R.m_digesttopicid.subject);
hodler.t_Author = (TextView) convertView
.findViewById(R.m_digesttopicid.author);
hodler.t_Dateline = (TextView) convertView
.findViewById(R.m_digesttopicid.dateline);
hodler.t_photo = (ImageView) convertView
.findViewById(R.m_digesttopicid.userimage);
hodler.t_Replies = (TextView) convertView
.findViewById(R.m_digesttopicid.replies);
hodler.t_photo.setTag(oAuthorphoto);
convertView.setTag(hodler);
}else {
hodler = (ViewHodler) convertView.getTag();
resetViewHolder(hodler);
}
hodler.t_Subject.setText(oSubject);
hodler.t_Dateline.setText(odateStr);
hodler.t_Author.setText(oAuthor);
hodler.t_Replies.setText(oReplies);
hodler.t_Replies.setGravity(Gravity.CENTER);
hodler.t_photo.setTag(oTid);
String userFaceUrl=AppUtility.getResourceString(R.string.apiuserphotourl)+”uid=”+oAuthorphoto+”&size=middle”;
this.setImageValue(hodler.t_photo,oTid,userFaceUrl,100, 100, R.drawable.m_newestlistimage,-1);
return convertView;
}