在列表项更新的观点,通过列表视图滚动时不留更新[IMG + SOURCE] [英] Updated views in list item don't stay updated when scrolling through listview[IMG + SOURCE]

查看:177
本文介绍了在列表项更新的观点,通过列表视图滚动时不留更新[IMG + SOURCE]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题的短版::如果我有一个列表视图,其中每个项目都有一个按钮,该按钮就可以使一个ListItem一个TextView更新,如何让我改变我做的从getView一个onClickListener列表行永久的吗?

问题的详细说明:我有一个列表视图和列表视图中有一个凸点按钮(相当于一个喜欢在Facebook按钮),当用户presses此类似按钮3件事情:


  1. 一个异步请求我的API,它记录了凸点,加入一个记录到我的数据库表中的一种制成。如果申请成功的API问题有2场的响应(is_bumped(布尔)及bump_count(INT))

  2. 如果API请求成功,那么我们就从响应抢bumpCount,并用它来更新bumpTv反映倍列表项已被撞的新总数....例如:3凸点

  3. 如果API请求成功,isBumped = true,那么我们更新图像资源的一个版本我的凹凸图标看起来选定/ pressed。

所有这些作品乍一看不错,但如果你凸点列表项,然后一路滚动到列表的底部,然后回到顶部,您刚才撞到将不再列表项似乎撞到除非你刷新整个活动。我知道这必须有事情做与我绑定到适配器没有更新的数据,但我怎么去,而无需刷新整个更新更新?

 包com.quotho​​r.helpers;进口的java.util.ArrayList;
进口的java.util.HashMap;进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.support.v4.app.FragmentManager;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ImageButton;
进口android.widget.TextView;
进口android.widget.Toast;公共类NewLazyAdapter延伸BaseAdapter { 私人活动活动;
 私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
 私有静态LayoutInflater吹气= NULL;
 公共ImageLoader的ImageLoader的;
 公共FragmentManager frag_manager; 静态类ViewHolder {
  TextView的名称;
  TextView的DIV3;
  TextView的DIV2;
  TextView的bumpTv;
  TextView的消息;
  TextView的commentsTv;
  SmartImageView thumb_image;
  的ImageButton bumpBtn;
  的ImageButton requestBtn;
  的ImageButton settingsBtn;
  TextView中创建的;    INT位置;
  } 公共NewLazyAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
  活性=一个;
  数据= D;
  吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  ImageLoader的=新ImageLoader的(activity.getApplicationContext());
 } 公共NewLazyAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串>> D,FragmentManager经理){
  活性=一个;
  frag_manager =经理;
  数据= D;
  吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  ImageLoader的=新ImageLoader的(activity.getApplicationContext());
 } 公众诠释的getCount(){
  返回data.size();
 } 公共对象的getItem(INT位置){
  返回的位置;
 } 众长getItemId(INT位置){
  返回的位置;
 } 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
  ViewHolder持有人;
  HashMap的<字符串,字符串>更新=新的HashMap<字符串,字符串>();
  更新= data.get(位置);  如果(convertView == NULL){
   convertView = inflater.inflate(R.layout.list_row,NULL);
   持有人=新ViewHolder();  holder.name =(TextView中)convertView.findViewById(R.id.name); //称号
  holder.div3 =(TextView中)convertView.findViewById(R.id.divider3); //称号
  holder.div2 =(TextView中)convertView.findViewById(R.id.divider2); //称号  holder.bumpTv =(TextView中)convertView.findViewById(R.id.bump); //称号
  holder.message =(TextView中)convertView.findViewById(R.id.message); //歌手名
  holder.commentsTv =(TextView中)convertView.findViewById(R.id.comments); //歌手名  holder.thumb_image =(SmartImageView)convertView.findViewById(R.id.list_image);
  holder.bumpBtn =(的ImageButton)convertView.findViewById(R.id.bump_btn);
  holder.requestBtn =(的ImageButton)convertView.findViewById(R.id.hidden_​​btn);  holder.settingsBtn =(的ImageButton)convertView.findViewById(R.id.settings_btn);
  holder.created =(TextView中)convertView.findViewById(R.id.created); //持续时间
  holder.bumpBtn.setTag(保持器);
  holder.bumpBtn.setOnClickListener(新凹凸(位置更新));
  convertView.setTag(保持器);  }其他{
         支架=(ViewHolder)convertView.getTag();
     }  Log.i(LazyAdapter数据,将String.valueOf(位置)++ update.toString());  如果(update.get(bump_count)!= NULL){
   holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
   //holder.bumpBtn.setOnClickListener(new凹凸(位置更新));
   串bump_count = update.get(bump_count);
   串is_bumped = update.get(is_bumped);
   //设置凹凸的TextView
   如果(bump_count.equals(0)){   }否则如果(bump_count.equals(1)){
    holder.div3.setVisibility(holder.div3.VISIBLE);
    holder.bumpTv.setVisibility(holder.bumpTv.VISIBLE);
    holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
    holder.bumpTv.setText(bump_count +凸点);
   }其他{
    holder.div3.setVisibility(holder.div3.VISIBLE);
    holder.bumpTv.setVisibility(holder.bumpTv.VISIBLE);
    holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
    holder.bumpTv.setText(bump_count +隆起);   }
   如果(is_bumped.equals(真)){
    holder.bumpBtn.setImageResource(R.drawable.quotho​​r_thumb_blue);
    //bumpBtn.setBackgroundResource(R.drawable.quotho​​r_bump_btn_bg_black);
   }其他{
    holder.bumpBtn.setImageResource(R.drawable.quotho​​r_bump_icon_black);
    //bumpBtn.setBackgroundResource(android.R.drawable.btn_default);
   }  }  如果(update.get(relationship_view)!= NULL){
   如果(update.get(UID)!= TabHostFragmentActivity.loggedin_uid){
    如果(update.get(relation_to_user)!= NULL){
     holder.requestBtn.setVisibility(holder.requestBtn.VISIBLE);     串关系= update.get(relation_to_user);     如果(relation.equals(朋友)){
      holder.settingsBtn.setVisibility(holder.settingsBtn.VISIBLE);
      holder.requestBtn.setImageResource(R.drawable.friend_btn);     }否则如果(relation.equals(熟悉)){
      holder.settingsBtn.setVisibility(holder.settingsBtn.VISIBLE);
      holder.requestBtn.setImageResource(R.drawable.familiar_btn);
     }
     holder.requestBtn.setOnClickListener(新myOnClickListener(位置));
     holder.settingsBtn.setOnClickListener(新myOnClickListener(位置));
    }   }  }  如果(update.get(创造)!= NULL){
   TextView中创建=(TextView中)convertView.findViewById(R.id.created); //持续时间
   字符串str_cr​​eated = update.get(创建);
   长created_l =的Long.parseLong(str_cr​​eated);
   字符串TIME_AGO = TimeAgo.fromPhpTime(created_l);
   created.setVisibility(convertView.VISIBLE);
   created.setText(TIME_AGO);  }
  如果(update.get(COMMENT_COUNT)!= NULL){
   holder.div2.setVisibility(holder.div2.VISIBLE);
   holder.commentsTv.setVisibility(holder.commentsTv.VISIBLE);   串注释= update.get(COMMENT_COUNT);
   如果(comments.equals(0)){
    holder.commentsTv.setText(无意见);
   }否则如果(comments.equals(1)){
    holder.commentsTv.setText(1个评论);
   }其他{
    holder.commentsTv.setText(评论+意见);
   }
  }其他{
   holder.commentsTv.setVisibility(holder.commentsTv.INVISIBLE);  }
  //设置列表视图中的所有值
  holder.name.setText(update.get(名字));
  如果(update.get(信息)!= NULL){
   holder.message.setText(update.get(信息));
  }其他{   holder.message.setVisibility(holder.message.INVISIBLE);
  }
  holder.thumb_image.setImageUrl(update.get(thumb_img));
  / *
        name.setOnClickListener(新myOnClickListener(位置));
        thumb_image.setOnClickListener(新myOnClickListener(位置));
   * /
  返回convertView;
 } 公共类myOnClickListener实现OnClickListener {
  私人诠释的位置;
  私人字符串clicked_uid;
  公共myOnClickListener(INT位置){
   this.position =位置;
  }
  @覆盖
  公共无效的onClick(视图v){   HashMap的<字符串,字符串>更新=新的HashMap<字符串,字符串>();
   更新= data.get(位置);
   Log.i(更新您的位置:update.toString());
   clicked_uid = update.get(UID);
   Log.d(点击了UID:clicked_uid +);
   串关系= update.get(relation_to_user);
   字符串的uid = update.get(UID);
   字符串名称= update.get(名称);
   串thumb_img = update.get(thumb_img);
   FragmentManager FM = frag_manager;
   EditRelationshipDialog editRelationshipDialog =新EditRelationshipDialog()的newInstance(UID,关系,姓名,thumb_img)。
   editRelationshipDialog.show(FM,relationshipsdialog);  } }
    公共类凹凸实现OnClickListener {
     私人诠释的位置;
  私人字符串clicked_uid;
  公共凹凸(INT位置,HashMap的<字符串,字符串>更新){
      this.position =位置;     }
  @覆盖
  公共无效的onClick(视图v){
   HashMap的<字符串,字符串>更新=新的HashMap<字符串,字符串>();
   更新= data.get(位置);
   最后查看theview = V;
   Log.i(更新您的位置:update.toString());
   串MSG_ID = update.get(MSG_ID);
   // ViewHolder mH的=(ViewHolder)theview.getTag();        // mH.message.setText(点击);   RequestParams PARAMS =新RequestParams();
   params.put(MSG_ID,MSG_ID);
   params.put(loggedin_uid,TabHostFragmentActivity.loggedin_uid);
   RestClient.post(TabHostFragmentActivity.token,HTTP:// API /凸点,则params,新JsonHtt presponseHandler(){
    @覆盖
    公共无效onFailure处(Throwable的为arg0,ARG1的JSONObject){
     // TODO自动生成方法存根
     super.onFailure(为arg0,ARG1);     Log.i(凸点请求懒适配器失败,arg1.toString());
     Toast.makeText(activity.getApplicationContext(),arg1.toString(),Toast.LENGTH_LONG).show();
    }    @覆盖
    公共无效的onSuccess(JSON的JSONObject){
     ViewHolder mH的=(ViewHolder)theview.getTag();     尝试{      串is_bumped = json.getString(is_bumped);
      串bump_count = json.getString(bump_count);
      如果(bump_count!= NULL){
       如果(bump_count.equals(0)){       }否则如果(bump_count.equals(1)){
        mH.div3.setVisibility(mH.div3.VISIBLE);
        mH.bumpTv.setVisibility(mH.bumpTv.VISIBLE);
        mH.bumpBtn.setVisibility(mH.bumpBtn.VISIBLE);
        mH.bumpTv.setText(bump_count +凸点);
       }其他{
        mH.div3.setVisibility(mH.div3.VISIBLE);
        mH.bumpTv.setVisibility(mH.bumpTv.VISIBLE);
        mH.bumpBtn.setVisibility(mH.bumpBtn.VISIBLE);
        mH.bumpTv.setText(bump_count +隆起);       }
      }
     }赶上(JSONException E){
      // TODO自动生成catch块
      e.printStackTrace();
     }
     尝试{
      串is_bumped = json.getString(is_bumped);      如果(is_bumped.equals(真)){
       mH.bumpBtn.setImageResource(R.drawable.quotho​​r_thumb_blue);
       //bumpBtn.setBackgroundResource(R.drawable.quotho​​r_bump_btn_bg_black);
      }其他{
       mH.bumpBtn.setImageResource(R.drawable.quotho​​r_bump_icon_black);
       //bumpBtn.setBackgroundResource(android.R.drawable.btn_default);
      }
     }赶上(JSONException E){
      // TODO自动生成catch块
      e.printStackTrace();
     }
    }
   });
  }    }
}

编辑:的意见,我肯定我的名单将开始运作我曾打算的方式服用意见后,但它只是打开了地狱的一个全新的CAN ....现在出于某种原因,如果用户单击第一个6个项目只是正常的凸点按钮的任何地方,但除此之外,奇怪的事情发生开始。当用户点击下面的第6个项目的列表项之一凹凸键发送到错误的位置和它的位置的某处0和6之间?!?!?

 包com.quotho​​r.helpers;进口的java.util.ArrayList;
进口的java.util.HashMap;进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.support.v4.app.FragmentManager;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ImageButton;
进口android.widget.TextView;
进口android.widget.Toast;
公共类NewLazyAdapter延伸BaseAdapter { 私人活动活动;
 私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
 私有静态LayoutInflater吹气= NULL;
 公共ImageLoader的ImageLoader的;
 公共FragmentManager frag_manager; 静态类ViewHolder {
  TextView的名称;
  TextView的DIV3;
  TextView的DIV2;
  TextView的bumpTv;
  TextView的消息;
  TextView的commentsTv;
  SmartImageView thumb_image;
  的ImageButton bumpBtn;
  的ImageButton requestBtn;
  的ImageButton settingsBtn;
  TextView中创建的;    INT位置;
  } 公共NewLazyAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
  活性=一个;
  数据= D;
  吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  ImageLoader的=新ImageLoader的(activity.getApplicationContext());
 } 公共NewLazyAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串>> D,FragmentManager经理){
  活性=一个;
  frag_manager =经理;
  数据= D;
  吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  ImageLoader的=新ImageLoader的(activity.getApplicationContext());
 } 公众诠释的getCount(){
  返回data.size();
 } 公共对象的getItem(INT位置){
  返回的位置;
 } 众长getItemId(INT位置){
  返回的位置;
 } 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
  ViewHolder持有人;
  HashMap的<字符串,字符串>更新=新的HashMap<字符串,字符串>();
  更新= data.get(位置);
  Log.i(位置被滚动过,将String.valueOf(位置));  如果(convertView == NULL){
   convertView = inflater.inflate(R.layout.list_row,NULL);
   持有人=新ViewHolder();  holder.name =(TextView中)convertView.findViewById(R.id.name); //称号
  holder.div3 =(TextView中)convertView.findViewById(R.id.divider3); //称号
  holder.div2 =(TextView中)convertView.findViewById(R.id.divider2); //称号  holder.bumpTv =(TextView中)convertView.findViewById(R.id.bump); //称号
  holder.message =(TextView中)convertView.findViewById(R.id.message); //歌手名
  holder.commentsTv =(TextView中)convertView.findViewById(R.id.comments); //歌手名  holder.thumb_image =(SmartImageView)convertView.findViewById(R.id.list_image);
  holder.bumpBtn =(的ImageButton)convertView.findViewById(R.id.bump_btn);
  holder.requestBtn =(的ImageButton)convertView.findViewById(R.id.hidden_​​btn);  holder.settingsBtn =(的ImageButton)convertView.findViewById(R.id.settings_btn);
  holder.created =(TextView中)convertView.findViewById(R.id.created); //持续时间
  holder.bumpBtn.setTag(保持器);
  holder.bumpBtn.setOnClickListener(新凹凸(位置更新));
  convertView.setTag(保持器);  }其他{
         支架=(ViewHolder)convertView.getTag();
     }  Log.i(LazyAdapter数据,将String.valueOf(位置)++ update.toString());  如果(update.get(bump_count)!= NULL){
   holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
   //holder.bumpBtn.setOnClickListener(new凹凸(位置更新));
   串bump_count = update.get(bump_count);
   串is_bumped = update.get(is_bumped);
   //设置凹凸的TextView
   如果(bump_count.equals(0)){   }否则如果(bump_count.equals(1)){
    holder.div3.setVisibility(holder.div3.VISIBLE);
    holder.bumpTv.setVisibility(holder.bumpTv.VISIBLE);
    holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
    holder.bumpTv.setText(bump_count +凸点);
   }其他{
    holder.div3.setVisibility(holder.div3.VISIBLE);
    holder.bumpTv.setVisibility(holder.bumpTv.VISIBLE);
    holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
    holder.bumpTv.setText(bump_count +隆起);   }
   如果(is_bumped.equals(真)){
    holder.bumpBtn.setImageResource(R.drawable.quotho​​r_thumb_blue);
    //bumpBtn.setBackgroundResource(R.drawable.quotho​​r_bump_btn_bg_black);
   }其他{
    holder.bumpBtn.setImageResource(R.drawable.quotho​​r_bump_icon_black);
    //bumpBtn.setBackgroundResource(android.R.drawable.btn_default);
   }  }  如果(update.get(relationship_view)!= NULL){
   如果(update.get(UID)!= TabHostFragmentActivity.loggedin_uid){
    如果(update.get(relation_to_user)!= NULL){
     holder.requestBtn.setVisibility(holder.requestBtn.VISIBLE);     串关系= update.get(relation_to_user);     如果(relation.equals(朋友)){
      holder.settingsBtn.setVisibility(holder.settingsBtn.VISIBLE);
      holder.requestBtn.setImageResource(R.drawable.friend_btn);     }否则如果(relation.equals(熟悉)){
      holder.settingsBtn.setVisibility(holder.settingsBtn.VISIBLE);
      holder.requestBtn.setImageResource(R.drawable.familiar_btn);
     }
     holder.requestBtn.setOnClickListener(新myOnClickListener(位置));
     holder.settingsBtn.setOnClickListener(新myOnClickListener(位置));
    }   }  }  如果(update.get(创造)!= NULL){
   TextView中创建=(TextView中)convertView.findViewById(R.id.created); //持续时间
   字符串str_cr​​eated = update.get(创建);
   长created_l =的Long.parseLong(str_cr​​eated);
   字符串TIME_AGO = TimeAgo.fromPhpTime(created_l);
   created.setVisibility(convertView.VISIBLE);
   created.setText(TIME_AGO);  }
  如果(update.get(COMMENT_COUNT)!= NULL){
   holder.div2.setVisibility(holder.div2.VISIBLE);
   holder.commentsTv.setVisibility(holder.commentsTv.VISIBLE);   串注释= update.get(COMMENT_COUNT);
   如果(comments.equals(0)){
    holder.commentsTv.setText(无意见);
   }否则如果(comments.equals(1)){
    holder.commentsTv.setText(1个评论);
   }其他{
    holder.commentsTv.setText(评论+意见);
   }
  }其他{
   holder.commentsTv.setVisibility(holder.commentsTv.INVISIBLE);  }
  //设置列表视图中的所有值
  holder.name.setText(update.get(MSG_ID));
  如果(update.get(信息)!= NULL){
   holder.message.setText(update.get(信息));
  }其他{   holder.message.setVisibility(holder.message.INVISIBLE);
  }
  holder.thumb_image.setImageUrl(update.get(thumb_img));
  / *
        name.setOnClickListener(新myOnClickListener(位置));
        thumb_image.setOnClickListener(新myOnClickListener(位置));
   * /  返回convertView;
 } 公共类myOnClickListener实现OnClickListener {
  私人诠释的位置;
  私人字符串clicked_uid;
  公共myOnClickListener(INT位置){
   this.position =位置;
  }
  @覆盖
  公共无效的onClick(视图v){   HashMap的<字符串,字符串>更新=新的HashMap<字符串,字符串>();
   更新= data.get(位置);
   Log.i(更新您的位置:update.toString());
   clicked_uid = update.get(UID);
   Log.d(点击了UID:clicked_uid +);
   串关系= update.get(relation_to_user);
   字符串的uid = update.get(UID);
   字符串名称= update.get(名称);
   串thumb_img = update.get(thumb_img);
   FragmentManager FM = frag_manager;
   EditRelationshipDialog editRelationshipDialog =新EditRelationshipDialog()的newInstance(UID,关系,姓名,thumb_img)。
   editRelationshipDialog.show(FM,relationshipsdialog);  } }
    公共类凹凸实现OnClickListener {
     私人诠释的位置;
  私人字符串clicked_uid;
  公共凹凸(INT位置,HashMap的<字符串,字符串>更新){
      this.position =位置;
     }
  @覆盖
  公共无效的onClick(视图v){
   HashMap的<字符串,字符串>更新=新的HashMap<字符串,字符串>();
   更新= data.get(位置);
   最后查看theview = V;
      Log.i(位置被点击,将String.valueOf(位置));
   Log.i(更新您的位置:update.toString());   串MSG_ID = update.get(MSG_ID);
   Log.i(MSG_ID,MSG_ID);
   // ViewHolder mH的=(ViewHolder)theview.getTag();        // mH.message.setText(点击);   RequestParams PARAMS =新RequestParams();
   params.put(MSG_ID,MSG_ID);
   params.put(loggedin_uid,TabHostFragmentActivity.loggedin_uid);
   RestClient.post(TabHostFragmentActivity.token,HTTP:// API /内容/凸点,则params,新JsonHtt presponseHandler(){
    @覆盖
    公共无效onFailure处(Throwable的为arg0,ARG1的JSONObject){
     // TODO自动生成方法存根
     super.onFailure(为arg0,ARG1);     Log.i(凸点请求懒适配器失败,arg1.toString());
     Toast.makeText(activity.getApplicationContext(),arg1.toString(),Toast.LENGTH_LONG).show();
    }    @覆盖
    公共无效的onSuccess(JSON的JSONObject){
     ViewHolder mH的=(ViewHolder)theview.getTag();     HashMap的<字符串,字符串> latestUpdate =新的HashMap<字符串,字符串>();
     latestUpdate = data.get(位置);
     Log.i(列表项正在编辑,latestUpdate.toString());     尝试{
      串bump_count = json.getString(bump_count);
      如果(bump_count!= NULL){
       latestUpdate.put(bump_count,bump_count);
      }
     }赶上(JSONException E){
      // TODO自动生成catch块
      e.printStackTrace();
     }     尝试{
      串is_bumped = json.getString(is_bumped);
      latestUpdate.put(is_bumped,is_bumped);
     }赶上(JSONException E){
      // TODO自动生成catch块
      e.printStackTrace();
     }     data.remove(位置);
     data.add(位置,latestUpdate);
     notifyDataSetChanged();
    }
   });
  }    }
}


解决方案

在这个问题的评论,它的成立,即使UI已被更新(基于用户交互),通过适配器<$ C $所使用的基础数据C> getView()不是。

解决方法:更新数据源,并调用 notifyDataSetChanged()适配器上。让 getView()处理UI更新。

通过阅读评论获取更多信息。

The short version of my question: If I have a listview where each item has a button and that button causes a textview on that listitem to be updated, how do I make changes I make to the list row from a onClickListener in getView permanent?

Detailed description of problem: I have a listview and that listview has a "bump" button (equivalent to a "like" button on Facebook) and when a user presses this like button 3 things happen:

  1. A async request is made to my api which records the "bump" by adding a record to one of my db tables. If the request is successful the api issues a response with 2 fields ( is_bumped(boolean) & bump_count(int) )
  2. If the api request was successful then we grab bumpCount from the response and use it to update the bumpTv to reflect the new total number of times that list item has been "bumped".... example: "3 Bumps"
  3. If the API request was successful and isBumped=true then we update the image resource to a version of my bump icon that looks selected/pressed.

All of this works just fine at first glance, but if you "bump" a list item and then scroll all the way to the bottom of the list and then back to the top, the list item you just bumped will no longer appear to be bumped unless you refresh the entire activity. I know this has to have something to do with the data that I bind to the adapter not being updated, but how do I go about updating without refreshing the entire update?

package com.quothor.helpers;

import java.util.ArrayList; 
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;





public class NewLazyAdapter extends BaseAdapter {

 private Activity activity;
 private ArrayList<HashMap<String, String>> data;
 private static LayoutInflater inflater=null;
 public ImageLoader imageLoader; 
 public FragmentManager frag_manager;



 static class ViewHolder {
  TextView name;
  TextView div3;
  TextView div2;
  TextView bumpTv;
  TextView message;
  TextView commentsTv;
  SmartImageView thumb_image;
  ImageButton bumpBtn;
  ImageButton requestBtn;
  ImageButton settingsBtn;
  TextView created ;

    int position;
  }

 public NewLazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
  activity = a;
  data=d;
  inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  imageLoader=new ImageLoader(activity.getApplicationContext());
 }

 public NewLazyAdapter(Activity a, ArrayList<HashMap<String, String>> d, FragmentManager manager) {
  activity = a;
  frag_manager=manager;
  data=d;
  inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  imageLoader=new ImageLoader(activity.getApplicationContext());
 }

 public int getCount() {
  return data.size();
 }

 public Object getItem(int position) {
  return position;
 }

 public long getItemId(int position) {
  return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  HashMap<String, String> update = new HashMap<String, String>();
  update = data.get(position); 

  if(convertView==null){
   convertView = inflater.inflate(R.layout.list_row, null);
   holder = new ViewHolder();

  holder.name = (TextView)convertView.findViewById(R.id.name); // title
  holder.div3 = (TextView)convertView.findViewById(R.id.divider3); // title
  holder.div2 = (TextView)convertView.findViewById(R.id.divider2); // title

  holder.bumpTv = (TextView)convertView.findViewById(R.id.bump); // title
  holder.message = (TextView)convertView.findViewById(R.id.message); // artist name
  holder.commentsTv = (TextView)convertView.findViewById(R.id.comments); // artist name

  holder.thumb_image = (SmartImageView) convertView.findViewById(R.id.list_image);
  holder.bumpBtn= (ImageButton)convertView.findViewById(R.id.bump_btn);
  holder.requestBtn = (ImageButton)convertView.findViewById(R.id.hidden_btn);

  holder.settingsBtn = (ImageButton)convertView.findViewById(R.id.settings_btn);
  holder.created = (TextView)convertView.findViewById(R.id.created); // duration
  holder.bumpBtn.setTag(holder);
  holder.bumpBtn.setOnClickListener(new Bump(position, update));
  convertView.setTag(holder);

  } else {
         holder = (ViewHolder) convertView.getTag();
     }



  Log.i("LazyAdapter data", String.valueOf(position)+" "+update.toString());

  if(update.get("bump_count") != null){
   holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
   //holder.bumpBtn.setOnClickListener(new Bump(position, update));
   String bump_count=update.get("bump_count");
   String is_bumped=update.get("is_bumped");


   //sets bump textview
   if(bump_count.equals("0")){ 

   }else if(bump_count.equals("1")){
    holder.div3.setVisibility(holder.div3.VISIBLE);
    holder.bumpTv.setVisibility(holder.bumpTv.VISIBLE);
    holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
    holder.bumpTv.setText(bump_count+" bump"); 
   }else{
    holder.div3.setVisibility(holder.div3.VISIBLE);
    holder.bumpTv.setVisibility(holder.bumpTv.VISIBLE);
    holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
    holder.bumpTv.setText(bump_count+" bumps"); 

   }
   if(is_bumped.equals("true")){
    holder.bumpBtn.setImageResource(R.drawable.quothor_thumb_blue);
    //bumpBtn.setBackgroundResource(R.drawable.quothor_bump_btn_bg_black);
   }else{
    holder.bumpBtn.setImageResource(R.drawable.quothor_bump_icon_black);
    //bumpBtn.setBackgroundResource(android.R.drawable.btn_default); 
   }

  }

  if(update.get("relationship_view")!=null){
   if(update.get("uid")!=TabHostFragmentActivity.loggedin_uid){
    if(update.get("relation_to_user")!=null){


     holder.requestBtn.setVisibility(holder.requestBtn.VISIBLE);

     String relation= update.get("relation_to_user");

     if(relation.equals("Friend")){
      holder.settingsBtn.setVisibility(holder.settingsBtn.VISIBLE); 
      holder.requestBtn.setImageResource(R.drawable.friend_btn);

     }else if(relation.equals("Familiar")){
      holder.settingsBtn.setVisibility(holder.settingsBtn.VISIBLE); 
      holder.requestBtn.setImageResource(R.drawable.familiar_btn);
     }


     holder.requestBtn.setOnClickListener(new myOnClickListener(position));
     holder.settingsBtn.setOnClickListener(new myOnClickListener(position));


    }

   }

  }



  if(update.get("created") != null){
   TextView created = (TextView)convertView.findViewById(R.id.created); // duration
   String str_created=update.get("created");
   long created_l = Long.parseLong(str_created);
   String time_ago=TimeAgo.fromPhpTime(created_l);
   created.setVisibility(convertView.VISIBLE);
   created.setText(time_ago);

  }
  if(update.get("comment_count")!=null){
   holder.div2.setVisibility(holder.div2.VISIBLE);
   holder.commentsTv.setVisibility(holder.commentsTv.VISIBLE);

   String comments = update.get("comment_count");
   if(comments.equals("0")){
    holder.commentsTv.setText("no comments");
   }else if(comments.equals("1")){
    holder.commentsTv.setText("1 comment");
   }else{
    holder.commentsTv.setText(comments+ " comments");
   }


  }else{
   holder.commentsTv.setVisibility(holder.commentsTv.INVISIBLE);

  }
  // Setting all values in listview
  holder.name.setText(update.get("name"));
  if(update.get("message") != null){
   holder.message.setText(update.get("message"));
  }else{

   holder.message.setVisibility(holder.message.INVISIBLE);
  }
  holder.thumb_image.setImageUrl(update.get("thumb_img"));
  /*
        name.setOnClickListener(new myOnClickListener(position));
        thumb_image.setOnClickListener(new myOnClickListener(position));
   */
  return convertView;
 }

 public class myOnClickListener implements OnClickListener{
  private int position;
  private String clicked_uid;
  public myOnClickListener(int position){
   this.position=position;
  }
  @Override
  public void onClick(View v) {

   HashMap<String, String> update = new HashMap<String, String>();
   update = data.get(position); 
   Log.i("Update Position:", update.toString());
   clicked_uid=update.get("uid");
   Log.d("Clicked UID:", clicked_uid+"");
   String relation= update.get("relation_to_user");
   String uid = update.get("uid");
   String name = update.get("name");
   String thumb_img = update.get("thumb_img");


   FragmentManager fm = frag_manager;
   EditRelationshipDialog editRelationshipDialog = new EditRelationshipDialog().newInstance(uid,relation,name,thumb_img);
   editRelationshipDialog.show(fm, "relationshipsdialog");



  }

 }


    public class Bump implements OnClickListener{
     private int position;
  private String clicked_uid;
  public Bump(int position,  HashMap<String, String> update){
      this.position=position;

     }
  @Override
  public void onClick(View v) {
   HashMap<String, String> update = new HashMap<String, String>();
   update = data.get(position); 
   final View theview=v;
   Log.i("Update Position:", update.toString());
   String msg_id=update.get("msg_id");
   //ViewHolder mH = (ViewHolder) theview.getTag();

        // mH.message.setText("clicked");

   RequestParams params = new RequestParams();
   params.put("msg_id", msg_id);
   params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);




   RestClient.post(TabHostFragmentActivity.token,"http://api/bump", params, new JsonHttpResponseHandler() {
    @Override
    public void onFailure(Throwable arg0, JSONObject arg1) {
     // TODO Auto-generated method stub
     super.onFailure(arg0, arg1);

     Log.i("bump request failed in lazy adapter", arg1.toString());
     Toast.makeText(activity.getApplicationContext(), arg1.toString() , Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSuccess(JSONObject json) {
     ViewHolder mH = (ViewHolder) theview.getTag();

     try {

      String is_bumped=json.getString("is_bumped");
      String bump_count=json.getString("bump_count");
      if(bump_count != null){ 
       if(bump_count.equals("0")){ 

       }else if(bump_count.equals("1")){
        mH.div3.setVisibility(mH.div3.VISIBLE);
        mH.bumpTv.setVisibility(mH.bumpTv.VISIBLE);
        mH.bumpBtn.setVisibility(mH.bumpBtn.VISIBLE);
        mH.bumpTv.setText(bump_count+" bump"); 
       }else{
        mH.div3.setVisibility(mH.div3.VISIBLE);
        mH.bumpTv.setVisibility(mH.bumpTv.VISIBLE);
        mH.bumpBtn.setVisibility(mH.bumpBtn.VISIBLE);
        mH.bumpTv.setText(bump_count+" bumps"); 

       }


      } 
     } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     try {
      String is_bumped=json.getString("is_bumped");

      if(is_bumped.equals("true")){
       mH.bumpBtn.setImageResource(R.drawable.quothor_thumb_blue);
       //bumpBtn.setBackgroundResource(R.drawable.quothor_bump_btn_bg_black);
      }else{
       mH.bumpBtn.setImageResource(R.drawable.quothor_bump_icon_black);
       //bumpBtn.setBackgroundResource(android.R.drawable.btn_default); 
      }
     } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }


    }
   });


  }

    }
}

EDIT: After taking the advice in the comments i was sure my list would begin functioning the way I had intended it to , but it only opened up a whole new can of hell....now for some reason if a user clicks the "bump" button anywhere in the first 6 items it works just fine, but beyond that something weird starts happening. when a user hits the bump button on one of the list items below the first 6 items it sends the wrong position and its a position somewhere between 0 and 6 ?!?!?

package com.quothor.helpers;

import java.util.ArrayList; 
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;




public class NewLazyAdapter extends BaseAdapter {

 private Activity activity;
 private ArrayList<HashMap<String, String>> data;
 private static LayoutInflater inflater=null;
 public ImageLoader imageLoader; 
 public FragmentManager frag_manager;



 static class ViewHolder {
  TextView name;
  TextView div3;
  TextView div2;
  TextView bumpTv;
  TextView message;
  TextView commentsTv;
  SmartImageView thumb_image;
  ImageButton bumpBtn;
  ImageButton requestBtn;
  ImageButton settingsBtn;
  TextView created ;

    int position;
  }

 public NewLazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
  activity = a;
  data=d;
  inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  imageLoader=new ImageLoader(activity.getApplicationContext());
 }

 public NewLazyAdapter(Activity a, ArrayList<HashMap<String, String>> d, FragmentManager manager) {
  activity = a;
  frag_manager=manager;
  data=d;
  inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  imageLoader=new ImageLoader(activity.getApplicationContext());
 }

 public int getCount() {
  return data.size();
 }

 public Object getItem(int position) {
  return position;
 }

 public long getItemId(int position) {
  return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  HashMap<String, String> update = new HashMap<String, String>();
  update = data.get(position); 
  Log.i("position being scrolled over", String.valueOf(position));

  if(convertView==null){
   convertView = inflater.inflate(R.layout.list_row, null);
   holder = new ViewHolder();

  holder.name = (TextView)convertView.findViewById(R.id.name); // title
  holder.div3 = (TextView)convertView.findViewById(R.id.divider3); // title
  holder.div2 = (TextView)convertView.findViewById(R.id.divider2); // title

  holder.bumpTv = (TextView)convertView.findViewById(R.id.bump); // title
  holder.message = (TextView)convertView.findViewById(R.id.message); // artist name
  holder.commentsTv = (TextView)convertView.findViewById(R.id.comments); // artist name

  holder.thumb_image = (SmartImageView) convertView.findViewById(R.id.list_image);
  holder.bumpBtn= (ImageButton)convertView.findViewById(R.id.bump_btn);
  holder.requestBtn = (ImageButton)convertView.findViewById(R.id.hidden_btn);

  holder.settingsBtn = (ImageButton)convertView.findViewById(R.id.settings_btn);
  holder.created = (TextView)convertView.findViewById(R.id.created); // duration
  holder.bumpBtn.setTag(holder);
  holder.bumpBtn.setOnClickListener(new Bump(position, update));
  convertView.setTag(holder);

  } else {
         holder = (ViewHolder) convertView.getTag();
     }



  Log.i("LazyAdapter data", String.valueOf(position)+" "+update.toString());

  if(update.get("bump_count") != null){
   holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
   //holder.bumpBtn.setOnClickListener(new Bump(position, update));
   String bump_count=update.get("bump_count");
   String is_bumped=update.get("is_bumped");


   //sets bump textview
   if(bump_count.equals("0")){ 

   }else if(bump_count.equals("1")){
    holder.div3.setVisibility(holder.div3.VISIBLE);
    holder.bumpTv.setVisibility(holder.bumpTv.VISIBLE);
    holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
    holder.bumpTv.setText(bump_count+" bump"); 
   }else{
    holder.div3.setVisibility(holder.div3.VISIBLE);
    holder.bumpTv.setVisibility(holder.bumpTv.VISIBLE);
    holder.bumpBtn.setVisibility(holder.bumpBtn.VISIBLE);
    holder.bumpTv.setText(bump_count+" bumps"); 

   }
   if(is_bumped.equals("true")){
    holder.bumpBtn.setImageResource(R.drawable.quothor_thumb_blue);
    //bumpBtn.setBackgroundResource(R.drawable.quothor_bump_btn_bg_black);
   }else{
    holder.bumpBtn.setImageResource(R.drawable.quothor_bump_icon_black);
    //bumpBtn.setBackgroundResource(android.R.drawable.btn_default); 
   }

  }

  if(update.get("relationship_view")!=null){
   if(update.get("uid")!=TabHostFragmentActivity.loggedin_uid){
    if(update.get("relation_to_user")!=null){


     holder.requestBtn.setVisibility(holder.requestBtn.VISIBLE);

     String relation= update.get("relation_to_user");

     if(relation.equals("Friend")){
      holder.settingsBtn.setVisibility(holder.settingsBtn.VISIBLE); 
      holder.requestBtn.setImageResource(R.drawable.friend_btn);

     }else if(relation.equals("Familiar")){
      holder.settingsBtn.setVisibility(holder.settingsBtn.VISIBLE); 
      holder.requestBtn.setImageResource(R.drawable.familiar_btn);
     }


     holder.requestBtn.setOnClickListener(new myOnClickListener(position));
     holder.settingsBtn.setOnClickListener(new myOnClickListener(position));


    }

   }

  }



  if(update.get("created") != null){
   TextView created = (TextView)convertView.findViewById(R.id.created); // duration
   String str_created=update.get("created");
   long created_l = Long.parseLong(str_created);
   String time_ago=TimeAgo.fromPhpTime(created_l);
   created.setVisibility(convertView.VISIBLE);
   created.setText(time_ago);

  }
  if(update.get("comment_count")!=null){
   holder.div2.setVisibility(holder.div2.VISIBLE);
   holder.commentsTv.setVisibility(holder.commentsTv.VISIBLE);

   String comments = update.get("comment_count");
   if(comments.equals("0")){
    holder.commentsTv.setText("no comments");
   }else if(comments.equals("1")){
    holder.commentsTv.setText("1 comment");
   }else{
    holder.commentsTv.setText(comments+ " comments");
   }


  }else{
   holder.commentsTv.setVisibility(holder.commentsTv.INVISIBLE);

  }
  // Setting all values in listview
  holder.name.setText(update.get("msg_id"));
  if(update.get("message") != null){
   holder.message.setText(update.get("message"));
  }else{

   holder.message.setVisibility(holder.message.INVISIBLE);
  }
  holder.thumb_image.setImageUrl(update.get("thumb_img"));
  /*
        name.setOnClickListener(new myOnClickListener(position));
        thumb_image.setOnClickListener(new myOnClickListener(position));
   */

  return convertView;
 }

 public class myOnClickListener implements OnClickListener{
  private int position;
  private String clicked_uid;
  public myOnClickListener(int position){
   this.position=position;
  }
  @Override
  public void onClick(View v) {

   HashMap<String, String> update = new HashMap<String, String>();
   update = data.get(position); 
   Log.i("Update Position:", update.toString());
   clicked_uid=update.get("uid");
   Log.d("Clicked UID:", clicked_uid+"");
   String relation= update.get("relation_to_user");
   String uid = update.get("uid");
   String name = update.get("name");
   String thumb_img = update.get("thumb_img");


   FragmentManager fm = frag_manager;
   EditRelationshipDialog editRelationshipDialog = new EditRelationshipDialog().newInstance(uid,relation,name,thumb_img);
   editRelationshipDialog.show(fm, "relationshipsdialog");



  }

 }


    public class Bump implements OnClickListener{
     private int position;
  private String clicked_uid;
  public Bump(int position,  HashMap<String, String> update){
      this.position=position;


     }
  @Override
  public void onClick(View v) {
   HashMap<String, String> update = new HashMap<String, String>();
   update = data.get(position); 
   final View theview=v;
      Log.i("POSITION BEING CLICKED",String.valueOf(position));
   Log.i("Update Position:", update.toString());

   String msg_id=update.get("msg_id");
   Log.i("msg_id",msg_id);
   //ViewHolder mH = (ViewHolder) theview.getTag();

        // mH.message.setText("clicked");

   RequestParams params = new RequestParams();
   params.put("msg_id", msg_id);
   params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);




   RestClient.post(TabHostFragmentActivity.token,"http://api/content/bump", params, new JsonHttpResponseHandler() {
    @Override
    public void onFailure(Throwable arg0, JSONObject arg1) {
     // TODO Auto-generated method stub
     super.onFailure(arg0, arg1);

     Log.i("bump request failed in lazy adapter", arg1.toString());
     Toast.makeText(activity.getApplicationContext(), arg1.toString() , Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSuccess(JSONObject json) {
     ViewHolder mH = (ViewHolder) theview.getTag();

     HashMap<String, String> latestUpdate = new HashMap<String, String>();
     latestUpdate = data.get(position);
     Log.i("list item being edited", latestUpdate.toString());

     try {
      String bump_count=json.getString("bump_count");
      if(bump_count != null){
       latestUpdate.put("bump_count", bump_count);
      } 
     } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }



     try {
      String is_bumped=json.getString("is_bumped");
      latestUpdate.put("is_bumped", is_bumped);
     } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }

     data.remove(position);
     data.add(position, latestUpdate);
     notifyDataSetChanged();
    }
   });


  }

    }
}

解决方案

In the question's comments, it was established that even though the UI was being updated (based on user interaction), the underlying data used by adapter's getView() weren't.

Solution: Update the data source and call notifyDataSetChanged() on the adapter. Let getView() handle UI updates.

Read through the comments for more information.

这篇关于在列表项更新的观点,通过列表视图滚动时不留更新[IMG + SOURCE]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆