2016-04-06 172 views
2

我在我的列表視圖中有問題。其實我已經從Facebook Like Custom Feed 實現了Facebook Like Feed,這是從MySQL數據庫獲取數據。在每個列表項目的底部,我有一個來自此庫的Like Button Material Favourite Button。我的問題是,當我點擊第一列表項目時,第五列表項目會自動被喜歡,如果我點擊第二列表項目第四列表項目被喜歡等等。我嘗試了所有我能做的事情,但沒有解決這個問題。我嘗試將視圖持有者也添加到我的列表適配器類中,如在這裏回答的各種問題中所建議的,但它並未解決我的問題。請幫助!以下是我的適配器類:Android ListView重複onclicking項目

public class FeedListAdapter extends BaseAdapter { 
    private Activity activity; 

    private int lastPosition = -1; 
    private DatabaseHandler db; 
    ViewHolder holder; 
     int id = 0; 
    String email; 

    private List<FeedItem> feedItems; 

    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 

    public FeedListAdapter(Activity activity, List<FeedItem> feedItems) { 
     this.activity = activity; 
     this.feedItems = feedItems; 
    } 

    @Override 
    public int getCount() { 
     return feedItems.size(); 
    } 

    @Override 
    public Object getItem(int location) { 
     return feedItems.get(location); 
    } 

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

    @Override 
    public View getView(final int position, View convertView, final ViewGroup parent) { 



     if (convertView == null){ 
      LayoutInflater inflater = (LayoutInflater) activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.feed_item, parent,false); 
      holder = new ViewHolder(); 
      holder.materialFavoriteButtonNice = 
        (MaterialFavoriteButton) convertView.findViewById(R.id.like_anim); 
      convertView.setTag(holder); 
     }else{ 

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

     if (imageLoader == null) 
      imageLoader = AppController.getInstance().getImageLoader(); 


      TextView name = (TextView) convertView.findViewById(R.id.name); 
      TextView timestamp = (TextView) convertView 
        .findViewById(R.id.timestamp); 
      //get User Email 
      db = new DatabaseHandler(activity.getApplication()); 
      db = new DatabaseHandler(activity.getApplication()); 
      HashMap<String, String> user = db.getUserDetails(); 
      email = user.get("email").toString(); 
      // End get User Email ID for sending it to db 

     //Getting Views from Layout 


     TextView statusMsg = (TextView) convertView 
       .findViewById(R.id.txtStatusMsg); 
     TextView url = (TextView) convertView.findViewById(R.id.txtUrl); 
     final TextView like = (TextView) convertView.findViewById(R.id.like_box_no); 
     TextView share = (TextView) convertView.findViewById(R.id.share_no); 
     TextView comment = (TextView) convertView.findViewById(R.id.comment_no); 
     NetworkImageView profilePic = (NetworkImageView) convertView 
       .findViewById(R.id.profilePic); 

     FeedImageView feedImageView = (FeedImageView) convertView 
       .findViewById(R.id.feedImage1); 
     //End Getting Views from Layout 

     final FeedItem item = feedItems.get(position); 
     name.setText(item.getName()); 

     // Converting timestamp into x ago format 
     CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
       Long.parseLong(item.getTimeStamp()), 
       System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS); 
     timestamp.setText(timeAgo); 

     // Check for empty status message 
     if (!TextUtils.isEmpty(item.getStatus())) { 
      statusMsg.setText(item.getStatus()); 
      statusMsg.setVisibility(View.VISIBLE); 
     } else { 
      // status is empty, remove from view 
      statusMsg.setVisibility(View.GONE); 
     } 
     // Chcek for empty Like 
     if (!TextUtils.isEmpty(item.getLike())) { 
      like.setText(item.getLike()); 
      like.setVisibility(View.VISIBLE); 
     } else { 
      // status is empty, remove from view 
      like.setText("0"); 
     } 
     // Chcek for empty Comment 
     if (!TextUtils.isEmpty(item.getComment())) { 
      comment.setText(item.getComment()); 
      comment.setVisibility(View.VISIBLE); 
     } else { 
      // status is empty, remove from view 
      comment.setText("0"); 
     } 
     // Chcek for empty Share 
     if (!TextUtils.isEmpty(item.getShare())) { 
      share.setText(item.getShare()); 
      share.setVisibility(View.VISIBLE); 
     } else { 
      // status is empty, remove from view 
      share.setText("0"); 
     } 
     if (item.getFav().equals("1")) { 
      holder.materialFavoriteButtonNice.setFavorite(true, false); 
      holder.materialFavoriteButtonNice.setVisibility(View.VISIBLE); 

     } else { 
      // status is empty, remove from view 
      holder.materialFavoriteButtonNice.setFavorite(false, false); 
     } 

     // Checking for null feed url 
     if (item.getUrl() != null) { 
      url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">" 
        + item.getUrl() + "</a> ")); 

      // Making url clickable 
      url.setMovementMethod(LinkMovementMethod.getInstance()); 
      url.setVisibility(View.VISIBLE); 
     } else { 
      // url is null, remove from the view 
      url.setVisibility(View.GONE); 
     } 

     // user profile pic 
     profilePic.setImageUrl(item.getProfilePic(), imageLoader); 
     imageLoader.get(item.getProfilePic(), ImageLoader.getImageListener(profilePic, R.drawable._businessman, R.drawable._businessman)); 

     // Feed image 
     if (item.getImge() != null) { 
      feedImageView.setImageUrl(item.getImge(), imageLoader); 
      feedImageView.setVisibility(View.VISIBLE); 
      feedImageView 
        .setResponseObserver(new FeedImageView.ResponseObserver() { 
         @Override 
         public void onError() { 
         } 

         @Override 
         public void onSuccess() { 
         } 
        }); 
     } else { 
      feedImageView.setVisibility(View.GONE); 
     } 

     //Animating the List View 
     Animation animation = AnimationUtils.loadAnimation(activity.getApplication(), (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top); 
     convertView.startAnimation(animation); 
     lastPosition = position; 
     //End Animating the List View 

     //onClick Like 


holder.materialFavoriteButtonNice.setOnFavoriteChangeListener(new MaterialFavoriteButton.OnFavoriteChangeListener() { 
    @Override 
    public void onFavoriteChanged(MaterialFavoriteButton buttonView, boolean favorite) { 
     id = item.getId(); 
     Log.d("inFavChngeListner", "Clickd" + item.getId()); 
     Toast.makeText(activity.getApplication(), "Fav Changed : " + item.getId(), Toast.LENGTH_SHORT).show(); 
     if (favorite) { 


      new send_json().execute(); 

     } else { 

      holder.materialFavoriteButtonNice.setFavorite(false, true); 
      new send_json_unlike().execute(); 
     } 
    } 
}); 
     return convertView; 


    } 
    static class ViewHolder { 
     MaterialFavoriteButton materialFavoriteButtonNice; 
    } 

    //Sending Likes with email id and feed id to Remote Mysql Db 
    public class send_json extends AsyncTask<String, String, JSONObject> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      if(!holder.materialFavoriteButtonNice.isFavorite()) 
      holder.materialFavoriteButtonNice.setFavorite(true, true); 
     } 

     @Override 
     protected JSONObject doInBackground(String... params) { 
      UserFunctions userFunction = new UserFunctions(); 

      JSONObject json = userFunction.like_func(email, String.valueOf(id)); 
      Log.d("BG Like, Email:" + email + "Id: " + String.valueOf(id), json.toString()); 
      return json; 
     } 

     @Override 
     protected void onPostExecute(JSONObject jsonObject) { 
      super.onPostExecute(jsonObject); 
if(!holder.materialFavoriteButtonNice.isFavorite()) 
      holder.materialFavoriteButtonNice.setFavorite(true, true); 
     } 
    } 
    //Sending Likes with email id and feed id to Remote Mysql Db 
    public class send_json_unlike extends AsyncTask<String, String, JSONObject> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      if(!holder.materialFavoriteButtonNice.isFavorite()) 
       holder.materialFavoriteButtonNice.setFavorite(false, true); 
     } 

     @Override 
     protected JSONObject doInBackground(String... params) { 
      UserFunctions userFunction = new UserFunctions(); 

      JSONObject json = userFunction.unlike_func(email, String.valueOf(id)); 
      Log.d("BG UnLike, Email:" + email + "Id: " + String.valueOf(id), json.toString()); 
      return json; 
     } 

     @Override 
     protected void onPostExecute(JSONObject jsonObject) { 
      super.onPostExecute(jsonObject); 
      if(!holder.materialFavoriteButtonNice.isFavorite()) 
       holder.materialFavoriteButtonNice.setFavorite(false, true); 
     } 
    } 
} 

回答

0

不使用ViewHolder實例全局。使其在功能範圍內。並嘗試改變邏輯。只更新數組內的模型,並調用notifyDataSetChanged()方法。我認爲它會解決你的問題。

+0

把ViewHolder放在函數範圍內幫助!!!非常感謝。現在我面臨另一個問題,當我滾動以前喜歡的職位得到重置。我如何保存以前喜歡的帖子,以便用戶知道他喜歡哪個帖子? – Aamir

+0

狀態更改後是否更新模型? – Krish