ListView Adapter executing onclick methods on multiple rows at the same time for one click event

In my application I have listview

a tab fragment

. Each line listview

is cardview

one that has multiple views in it. ( Buttons

, textviews

etc.) I created listview

my own adapter

class for this .

This class adapter

takes the data from the local database and inserts it into a different views

in cardview

, creating a list of cards. I have a button in cardview

.

Now when the user clicks a button like this, I change the background of the button using the onClickListener

on button in the adapter class.

The problem is that if I click a button on one card, it changes the background of a similar button on that card, as well as the button on the fourth card down in the list. I think it needs to be done with the template ViewHolder

I'm using, but don't know how to fix it.

Here is the custom adapter class

package com.example.nxtstepz.nxtstepzone.Adpters;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.example.nxtstepz.nxtstepzone.Communicater.Communicater;
import com.example.nxtstepz.nxtstepzone.PostDetailActivity;
import com.example.nxtstepz.nxtstepzone.PostInfo;
import com.example.nxtstepz.nxtstepzone.R;
import java.util.List;
import de.greenrobot.dao.query.QueryBuilder;
import nxtstepz.DaoMaster;
import nxtstepz.DaoSession;
import nxtstepz.Post;
import nxtstepz.PostDao;
import nxtstepz.SavedPost;
import nxtstepz.SavedPostDao;

/**
 * Created by Prathya on 6/8/2015.
 */
public class HomeScreenNewsFeedAdapter extends ArrayAdapter<PostInfo>{
List<PostInfo> data;
Context context;
int layoutid;
public HomeScreenNewsFeedAdapter(Context context, int layoutid,     List<PostInfo> data) {
    super(context, layoutid);
    this.context=context;
    this.data=data;
    this.layoutid=layoutid;
}


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

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

private class PostHolder{
    TextView postusername,autherinstitute,postheading,postdescription,likecount,comments,date,category;
    View save,like;
    LinearLayout postdetail;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context,"nxtstepz",null);
    SQLiteDatabase db =helper.getWritableDatabase();
    DaoMaster daoMaster = new DaoMaster(db);
    DaoSession session = daoMaster.newSession();
    final PostDao postDao = session.getPostDao();
   final PostHolder holder;
    View v= convertView;
    if(v==null){
        LayoutInflater li = LayoutInflater.from(context);
        v= li.inflate(layoutid,parent,false);
        holder = new PostHolder();
        holder.postusername = (TextView)v.findViewById(R.id.postusername);
        holder.autherinstitute= (TextView)v.findViewById(R.id.autherinstitute);
        holder.postheading = (TextView)v.findViewById(R.id.post_title);
        holder.postdescription = (TextView)v.findViewById(R.id.post_description);
        holder.likecount = (TextView)v.findViewById(R.id.like_count);
        holder.comments = (TextView)v.findViewById(R.id.comment_count);
        holder.date = (TextView)v.findViewById(R.id.post_datetime);
        holder.category = (TextView)v.findViewById(R.id.post_category);
        holder.save=v.findViewById(R.id.save_button);
        holder.like= v.findViewById(R.id.like_button);
        holder.postdetail=(LinearLayout)v.findViewById(R.id.ll3);
        v.setTag(holder);
    }
    else {
        holder= (PostHolder)v.getTag();
    }
  final PostInfo post;
     post = data.get(position);
    Log.d("post id", String.valueOf(post.posttypeid));
    holder.postusername.setText(post.postusername);
    holder.autherinstitute.setText(post.autherinstitutename);
    holder.postheading.setText(post.postheading);
    holder.postdescription.setText(post.postdescription);
    holder.likecount.setText(""+post.likecount);
    holder.comments.setText("" + post.comments);
    holder.category.setText(post.categoryname);
    holder.date.setText(post.date);

    holder.like.setOnClickListener(new View.OnClickListener() { //this method has the problem
        @Override
        public void onClick(View v) {
            if (post.postlikeflag == 0) {
                holder.like.setBackgroundResource(R.drawable.liked);
                post.postlikeflag =1;
            } else {
                holder.like.setBackgroundResource(R.drawable.like);
                post.postlikeflag = 0;
            }


        }
    });
    holder.postdetail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent eventDetail = new Intent(context, PostDetailActivity.class);
            eventDetail.putExtra("EventDetail", post);
            context.startActivity(eventDetail);

        }
    });


return  v;
 }
}

      

Here is the single_card_view.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BDBDBD">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/homecardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp">

    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imgIcon"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentTop="true"
                android:layout_margin="10dp"
                android:src="@drawable/img8" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"
                android:gravity="left"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/postusername"
                    android:gravity="left"
                    android:text="Jessica Alba"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:id="@+id/autherinstitute"
                    android:text="Mahatma audkjfsdk dkbldvb"
                    android:singleLine="true"

                    android:ellipsize="end"
                    android:textSize="13sp"
                    android:textStyle="italic"
                    android:scrollHorizontally="true"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top|right"
                    android:layout_marginRight="10dp"
                    android:text="38 min"
                    android:id="@+id/post_datetime"/>

                <TextView
                    android:id="@+id/post_category"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|right"
                    android:layout_marginTop="10dp"
                    android:background="#9C27B0"
                    android:padding="5dp"
                    android:text=""
                    android:textColor="#ffffff"
                    android:textStyle="bold|italic" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/post_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00b5ad"
                android:gravity="center|left"
                android:padding="5dp"
                android:text="Sri-Ram breakup party"
                android:textColor="#ffffff"
                android:textSize="15dp"
                />

            <TextView
                android:id="@+id/post_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="1dp"
                android:background="#ffffff"
                android:gravity="top|left"
                android:padding="5dp"
                android:text="Monica is an Italian actress and model who recently broke up with Sriram started her modelling career at the age of 13 by posing for a local photo enthusiast. Android:The Best OS.Android powers hundreds of millions of mobile devices in more than 190 countries around the world.Android openness has made it a favorite for consumers"
                android:textSize="10dp" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_margin="2dp"
            android:background="#000000"></View>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <View
                android:id="@+id/like_button"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/like"
                android:focusable="false"
                android:focusableInTouchMode="false">

            </View>

            <View
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/comment"
                android:id="@+id/comment_button"></View>

            <View
                android:layout_width="45dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/view"
                android:id="@+id/save_button"
                android:background="@drawable/save"></View>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:padding="6dp"
                android:text="0"
                android:gravity="center"
                android:id="@+id/like_count"
                android:background="#FFECB3"
                android:layout_alignTop="@+id/like_button"
                android:layout_toRightOf="@+id/like_button"
                android:layout_toEndOf="@+id/like_button"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:padding="6dp"
                android:text="0"
                android:gravity="center"
                android:id="@+id/comment_count"
                android:background="#FFECB3"
                android:layout_gravity="center_horizontal|bottom"
                android:layout_alignTop="@+id/comment_button"
                android:layout_toRightOf="@+id/comment_button"
                android:layout_toEndOf="@+id/comment_button" />
        </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

      

+3


source to share


3 answers


I got it using HashMap as SAM suggested. Here's what I did.

in adapter class

HashMap<Long, String> map = new HashMap<Long, String>();

      

in getView()

methodadapter



    if(post.postlikeflag==0){
      map.put(post.postid, "like");
      }
     else{
           map.put(post.postid,"liked");
      }

   if(map.get(post.postid).equals("like")) {
            holder.like.setBackgroundResource(R.drawable.like);
        }
      else {
          holder.like.setBackgroundResource(R.drawable.liked);
        }

      

And in a onclickListener

similar button

  if (post.postlikeflag == 0 && map.get(post.postid).equals("like")) {
                holder.like.setBackgroundResource(R.drawable.liked);
                map.put(post.postid, "liked");
                post.postlikeflag = 1;
  } else {
                holder.like.setBackgroundResource(R.drawable.like);
                post.postlikeflag = 0;
                map.put(post.postid,"like");
      }

      

I know there might be redundancy in the code. Indicate this if you find it.

0


source


I changed your code, paste it in and check if it works or not.



   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context,"nxtstepz",null);
        HashMap<Integer, String> map = new  HashMap<Integer, String>();
        SQLiteDatabase db =helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession session = daoMaster.newSession();
        final PostDao postDao = session.getPostDao();
       final PostHolder holder;
        View v= convertView;
        if(v==null){
            LayoutInflater li = LayoutInflater.from(context);
            v= li.inflate(layoutid,parent,false);
            holder = new PostHolder();
            holder.postusername = (TextView)v.findViewById(R.id.postusername);
            holder.autherinstitute= (TextView)v.findViewById(R.id.autherinstitute);
            holder.postheading = (TextView)v.findViewById(R.id.post_title);
            holder.postdescription = (TextView)v.findViewById(R.id.post_description);
            holder.likecount = (TextView)v.findViewById(R.id.like_count);
            holder.comments = (TextView)v.findViewById(R.id.comment_count);
            holder.date = (TextView)v.findViewById(R.id.post_datetime);
            holder.category = (TextView)v.findViewById(R.id.post_category);
            holder.save=v.findViewById(R.id.save_button);
            holder.like= v.findViewById(R.id.like_button);
            holder.postdetail=(LinearLayout)v.findViewById(R.id.ll3);
            v.setTag(holder);
        }
        else {
            holder= (PostHolder)v.getTag();
        }
      final PostInfo post;
         post = data.get(position);
        Log.d("post id", String.valueOf(post.posttypeid));
        holder.postusername.setText(post.postusername);
        holder.autherinstitute.setText(post.autherinstitutename);
        holder.postheading.setText(post.postheading);
        holder.postdescription.setText(post.postdescription);
        holder.likecount.setText(""+post.likecount);
        holder.comments.setText("" + post.comments);
        holder.category.setText(post.categoryname);
        holder.date.setText(post.date);
        map.put(position,"like");
        holder.like.setOnClickListener(new View.OnClickListener() { //this method has the problem
            @Override
            public void onClick(View v) {
                if (post.postlikeflag == 0 && map.get(position).equals("like")) {
                    holder.like.setBackgroundResource(R.drawable.liked);
                    map.put(position,"liked");
                    post.postlikeflag =1;
                } else {
                    holder.like.setBackgroundResource(R.drawable.like);
                    post.postlikeflag = 0;
                }


            }
        });
        holder.postdetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent eventDetail = new Intent(context, PostDetailActivity.class);
                eventDetail.putExtra("EventDetail", post);
                context.startActivity(eventDetail);

            }
        });

      

+1


source


In your button and in another view where the onClick listener adds these two lines to the xml

android:focusable="false"
android:focusableInTouchMode="false"

      

declare a final variable index as shown below and initialize it with position at the very start of getView method below

final int index = position;

      

Also press instead

 post.postlikeflag =1;

      

try it

data.get(index).postlikeflag = 1;

      

also you will have to call notifyDataSetChanged ()

0


source







All Articles