gpt4 book ai didi

android - ListView Adapter 为单个点击事件同时在多行上执行 onclick 方法

转载 作者:太空狗 更新时间:2023-10-29 14:08:34 26 4
gpt4 key购买 nike

在我的应用程序中,我在标签 fragment 中有一个 listviewlistview 的每一行都是一个 cardview ,其中有多个 View 。(Buttonstextviews 等)我为此 listview 创建了一个自定义 adapter 类。

适配器 类从本地数据库获取数据并将其插入cardview 中的不同 View ,创建卡片列表。我在 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>

最佳答案

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

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

}
});

关于android - ListView Adapter 为单个点击事件同时在多行上执行 onclick 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30889582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com