ListView适配器针对一个单击事件同时在多行上执行onclick方法

Prathmesh Deshmukh

在我的应用程序中,我listview在选项卡中有一个fragment每行listview是一个cardview,它在它的多个意见。( Buttonstextviews等等),我创建了一个自定义的adapter类此listview

此类adapter从本地数据库获取数据,并将其插入views中的cardview,以创建卡列表。我有一个喜欢的按钮cardview

现在,当用户按下“赞”按钮时,我正在使用onClickListener适配器类中的“打开”按钮来更改按钮的背景

问题是,如果我按一张卡上的“喜欢”按钮,它将改变该卡上“喜欢”按钮的背景以及列表中第四张卡片上的“喜欢”按钮的背景。我认为这与ViewHolder我使用的模式有关,但不确定如何解决。

这是自定义适配器类

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;
 }
}

这是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's 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>

Prathmesh Deshmukh

我通过使用SAM建议的HashMap使其工作。这是我所做的。

在适配器类中

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

在...的getView()方法中adapter

    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);
        }

并在onclickListener喜欢按钮

  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");
      }

我知道代码中可能会有一些冗余。如果发现,请指出。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ListView适配器针对一个单击事件同时在多行上执行onclick方法

来自分类Dev

在ListView中禁用对一个数据适配器的单击

来自分类Dev

在ListView中禁用对一个数据适配器的单击

来自分类Dev

无法设置 ListView 的适配器,因为 ListView 有一个空对象引用

来自分类Dev

如何完成一个ListView适配器类的活动?

来自分类Dev

如何完成一个ListView适配器类的活动?

来自分类Dev

获取一个ListView刷新?YouTube API-自定义适配器

来自分类Dev

从适配器更改ListView的另一个位置的EditText的值

来自分类Dev

从另一个适配器中更新适配器数据集

来自分类Dev

从另一个回收器适配器类的 onClick() 调用一个方法来运行回收器视图

来自分类Dev

Hyper-V更改其中一个适配器上的静态MAC地址

来自分类Dev

使用多个RecyclerView一个适配器类

来自分类Dev

带有一个适配器的多个gridview

来自分类Dev

如何从活动类中在 Recycler View Adapters 上设置 OnClick 侦听器?或者从第一个适配器访问其他 RVAdapter?

来自分类Dev

一个Adaptee类继承另一个适配器的适配器设计模式?

来自分类Dev

在适配器内写下太多方法是一个坏主意吗?

来自分类Dev

Android-将返回值从适配器中的重写接口方法传递给适配器中的另一个方法

来自分类Dev

Android如何从外部适配器识别Listview Button单击事件?

来自分类Dev

如何为随机自定义ListView适配器设置按钮单击事件?

来自分类Dev

ListFragment适配器onClick仅在首次单击时执行部分代码,在连续单击时正确执行

来自分类Dev

如何使用适配器在按钮单击上添加ListView项目

来自分类Dev

如何在另一个recycleview适配器下使用recycleview适配器?

来自分类Dev

一个网络适配器的流程图失败,显示“ LLLL ...”,其他适配器成功

来自分类Dev

Worklight Java适配器调用另一个适配器会得到I / O问题

来自分类Dev

一个网络适配器正在响应所有其他网络适配器

来自分类Dev

如何在Android的多个屏幕上使用一个自定义适配器类?

来自分类Dev

如何在Android的多个屏幕上使用一个自定义适配器类?

来自分类Dev

在游标适配器中使用单击事件

来自分类Dev

在自定义ListView适配器的getView()方法中使用getItem(position)时出现错误的项目,其中每个列表项都有一个CheckBox和一个搜索栏

Related 相关文章

  1. 1

    ListView适配器针对一个单击事件同时在多行上执行onclick方法

  2. 2

    在ListView中禁用对一个数据适配器的单击

  3. 3

    在ListView中禁用对一个数据适配器的单击

  4. 4

    无法设置 ListView 的适配器,因为 ListView 有一个空对象引用

  5. 5

    如何完成一个ListView适配器类的活动?

  6. 6

    如何完成一个ListView适配器类的活动?

  7. 7

    获取一个ListView刷新?YouTube API-自定义适配器

  8. 8

    从适配器更改ListView的另一个位置的EditText的值

  9. 9

    从另一个适配器中更新适配器数据集

  10. 10

    从另一个回收器适配器类的 onClick() 调用一个方法来运行回收器视图

  11. 11

    Hyper-V更改其中一个适配器上的静态MAC地址

  12. 12

    使用多个RecyclerView一个适配器类

  13. 13

    带有一个适配器的多个gridview

  14. 14

    如何从活动类中在 Recycler View Adapters 上设置 OnClick 侦听器?或者从第一个适配器访问其他 RVAdapter?

  15. 15

    一个Adaptee类继承另一个适配器的适配器设计模式?

  16. 16

    在适配器内写下太多方法是一个坏主意吗?

  17. 17

    Android-将返回值从适配器中的重写接口方法传递给适配器中的另一个方法

  18. 18

    Android如何从外部适配器识别Listview Button单击事件?

  19. 19

    如何为随机自定义ListView适配器设置按钮单击事件?

  20. 20

    ListFragment适配器onClick仅在首次单击时执行部分代码,在连续单击时正确执行

  21. 21

    如何使用适配器在按钮单击上添加ListView项目

  22. 22

    如何在另一个recycleview适配器下使用recycleview适配器?

  23. 23

    一个网络适配器的流程图失败,显示“ LLLL ...”,其他适配器成功

  24. 24

    Worklight Java适配器调用另一个适配器会得到I / O问题

  25. 25

    一个网络适配器正在响应所有其他网络适配器

  26. 26

    如何在Android的多个屏幕上使用一个自定义适配器类?

  27. 27

    如何在Android的多个屏幕上使用一个自定义适配器类?

  28. 28

    在游标适配器中使用单击事件

  29. 29

    在自定义ListView适配器的getView()方法中使用getItem(position)时出现错误的项目,其中每个列表项都有一个CheckBox和一个搜索栏

热门标签

归档