ListView适配器同时在多个行上执行onclick方法,以进行单击事件

时间:2015-06-17 10:58:10

标签: android listview android-listview android-adapter

在我的应用中,我在标签listview中有一个fragmentlistview的每一行都是cardview,其中包含多个视图。(Buttonstextviews等。)我创建了一个自定义adapter类这个listview

adapter类从本地数据库获取数据并将其插入views中的不同cardview,从而创建卡片列表。我在cardview中有一个类似的按钮。

现在,当用户按下like按钮时,我正在使用适配器类中的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>

3 个答案:

答案 0 :(得分:1)

我修改了你的代码粘贴它并检查它是否正常工作

   @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 :(得分:0)

在你的按钮和其他视图中,onClick监听器在那里添加这两行xml

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

如下所示声明最终变量索引,并在非常位置初始化它 getView方法的开始如下

final int index = position;

同时点击而非此

 post.postlikeflag =1;

试试这个

data.get(index).postlikeflag = 1;

您还必须调用notifyDataSetChanged()

答案 2 :(得分:0)

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

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