我尝试在自定义列表视图中使用OnClickListener
,但是当单击textview时(Onclicklistener运行)它在listview中的几个textview上运行代码!
请参阅我的holder.tvComment.setOnClickListener
更新:
ListAdapter.class
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.skyline.jimmy.widget.LikeToast;
import com.skyline.jimmy.widget.MyToast;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
//import android.text.ClipboardManager;
import android.content.ClipboardManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
public class VoteListAdapter extends BaseAdapter {
protected static final String CLIPBOARD_SERVICE = null;
private final String TAG = "*** VoteListAdapter ***";
private Context context;
private ArrayList<Vote> votes;
int showHideBtn = 0;
public VoteListAdapter(Context context,List<Vote> voteList) {
this.context =context;
this.votes =(ArrayList<Vote>) voteList;
}
@Override
public int getCount() {
return votes.size();
}
@Override
public Object getItem(int position) {
return votes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row, null);
holder.tvName = (TextView) convertView.findViewById(R.id.user);
holder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
holder.tvDate = (TextView) convertView.findViewById(R.id.tDate);
holder.tvLikes = (TextView) convertView.findViewById(R.id.tvLikes);
holder.ratingBar = (RatingBar)convertView.findViewById(R.id.ratingBar);
holder.share = (ImageButton) convertView.findViewById(R.id.sharebtn);
holder.copyJoke = (ImageButton) convertView.findViewById(R.id.copyJoke);
holder.btnLike = (Button) convertView.findViewById(R.id.likebtn);
holder.btnLiked = (Button) convertView.findViewById(R.id.likedbtn);
holder.btns = (ImageButton) convertView.findViewById(R.id.btns);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Typeface yaghut=Typeface.createFromAsset(context.getAssets(), "font/Far_Casablanca.ttf");
String likes = Integer.parseInt(votes.get(position).getRate()) + " likes";
holder.tvName.setText(votes.get(position).getName());
holder.tvComment.setText(votes.get(position).getComment());
holder.tvDate.setText(getFormatedDate(votes.get(position).getPublishDate()));
holder.tvLikes.setText(likes);
holder.ratingBar.setRating(Integer.parseInt(votes.get(position).getRate()));
holder.tvComment.setTypeface(yaghut);
if (convertView == null) {
holder.tvComment.setOnClickListener( new OnClickListener() {
int clicks = 0;
@Override
public void onClick(View v) {
clicks = clicks + 1;
if(clicks == 1){
}else if(clicks == 2) {
LikeToast.makeText(context, "Liked" , Toast.LENGTH_SHORT).show();
holder.btnLike.setText("Liked");
Drawable image = context.getResources().getDrawable( R.drawable.ic_action_liked );
int hlike = image.getIntrinsicHeight();
int wlike = image.getIntrinsicWidth();
image.setBounds( 0, 0, wlike, hlike );
holder.btnLike.setCompoundDrawables( image, null, null, null );
//holder.btnLikes.setBackgroundDrawable(R.drawable.ic_action_liked);
} else{
clicks = 2;
}
}
});
convertView.setTag(holder);
}
holder.btns.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
if(showHideBtn == 1){
holder.share.setVisibility(View.INVISIBLE);
holder.btns.setVisibility(View.VISIBLE);
holder.copyJoke.setVisibility(View.INVISIBLE);
showHideBtn = 0;
} else {
holder.share.setVisibility(View.VISIBLE);
holder.btns.setVisibility(View.VISIBLE);
holder.copyJoke.setVisibility(View.VISIBLE);
showHideBtn = 1;
}
}
});
holder.copyJoke.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
String Joke = votes.get(position).getComment();
clipboard.setText(Joke);
Toast toast3 = MyToast.makeText(context, "جک مورد نظر کپی شد", Toast.LENGTH_SHORT);
toast3.show();
}
});
holder.share.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
/* Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT,votes.get(position).getComment());
try {
context.startActivity(Intent.createChooser(i, "Share"));
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
}
*/
Toast toast = MyToast.makeText(context, "این قسمت در نسخه بتا در حال طراحی است", Toast.LENGTH_LONG);
toast.show();
Toast toast2 = MyToast.makeText(context, "منتظر آپدیت باشید", Toast.LENGTH_SHORT);
toast2.show();
}
});
return convertView;
}
private String getFormatedDate(String date) {
String myDate = null;
try {
Date oldDate = new SimpleDateFormat("yy-mm-dd hh:mm:ss").parse(date);
myDate = new SimpleDateFormat("dd MMM yyyy").format(oldDate);
} catch (ParseException e) {
myDate = "";
e.printStackTrace();
}
return myDate;
}
static class ViewHolder {
ImageButton copyJoke;
ImageButton share;
ImageButton btns;
Button btnLike;
Button btnLiked;
TextView tvName;
TextView tvLikes;
TextView tvComment;
TextView tvDate;
RatingBar ratingBar;
}
}
答案 0 :(得分:0)
将setOnClickListener代码写入if条件
if (convertView == null) {
.....
.....
// write here your holder.tvComment.setOnClickListener
convertView.setTag(holder);
}
在此之前:
convertView.setTag(holder);
您的完整适配器将是这样的:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.skyline.jimmy.widget.LikeToast;
import com.skyline.jimmy.widget.MyToast;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
//import android.text.ClipboardManager;
import android.content.ClipboardManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
public class VoteListAdapter extends BaseAdapter {
ViewHolder holder;
protected static final String CLIPBOARD_SERVICE = null;
private final String TAG = "*** VoteListAdapter ***";
private Context context;
private ArrayList<Vote> votes;
int showHideBtn = 0;
public VoteListAdapter(Context context,List<Vote> voteList) {
this.context =context;
this.votes =(ArrayList<Vote>) voteList;
}
@Override
public int getCount() {
return votes.size();
}
@Override
public Object getItem(int position) {
return votes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row, null);
holder.tvName = (TextView) convertView.findViewById(R.id.user);
holder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
holder.tvDate = (TextView) convertView.findViewById(R.id.tDate);
holder.tvLikes = (TextView) convertView.findViewById(R.id.tvLikes);
holder.ratingBar = (RatingBar)convertView.findViewById(R.id.ratingBar);
holder.share = (ImageButton) convertView.findViewById(R.id.sharebtn);
holder.copyJoke = (ImageButton) convertView.findViewById(R.id.copyJoke);
holder.btnLike = (Button) convertView.findViewById(R.id.likebtn);
holder.btnLiked = (Button) convertView.findViewById(R.id.likedbtn);
holder.btns = (ImageButton) convertView.findViewById(R.id.btns);
holder.btns.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
if(showHideBtn == 1){
holder.share.setVisibility(View.INVISIBLE);
holder.btns.setVisibility(View.VISIBLE);
holder.copyJoke.setVisibility(View.INVISIBLE);
showHideBtn = 0;
} else {
holder.share.setVisibility(View.VISIBLE);
holder.btns.setVisibility(View.VISIBLE);
holder.copyJoke.setVisibility(View.VISIBLE);
showHideBtn = 1;
}
}
});
holder.copyJoke.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
String Joke = votes.get(position).getComment();
clipboard.setText(Joke);
Toast toast3 = MyToast.makeText(context, "جک مورد نظر کپی شد", Toast.LENGTH_SHORT);
toast3.show();
}
});
holder.share.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
/* Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT,votes.get(position).getComment());
try {
context.startActivity(Intent.createChooser(i, "Share"));
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
}
*/
Toast toast = MyToast.makeText(context, "این قسمت در نسخه بتا در حال طراحی است", Toast.LENGTH_LONG);
toast.show();
Toast toast2 = MyToast.makeText(context, "منتظر آپدیت باشید", Toast.LENGTH_SHORT);
toast2.show();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Typeface yaghut=Typeface.createFromAsset(context.getAssets(), "font/Far_Casablanca.ttf");
String likes = Integer.parseInt(votes.get(position).getRate()) + " likes";
holder.tvName.setText(votes.get(position).getName());
holder.tvComment.setText(votes.get(position).getComment());
holder.tvDate.setText(getFormatedDate(votes.get(position).getPublishDate()));
holder.tvLikes.setText(likes);
holder.ratingBar.setRating(Integer.parseInt(votes.get(position).getRate()));
holder.tvComment.setTypeface(yaghut);
return convertView;
}
private String getFormatedDate(String date) {
String myDate = null;
try {
Date oldDate = new SimpleDateFormat("yy-mm-dd hh:mm:ss").parse(date);
myDate = new SimpleDateFormat("dd MMM yyyy").format(oldDate);
} catch (ParseException e) {
myDate = "";
e.printStackTrace();
}
return myDate;
}
static class ViewHolder {
ImageButton copyJoke;
ImageButton share;
ImageButton btns;
Button btnLike;
Button btnLiked;
TextView tvName;
TextView tvLikes;
TextView tvComment;
TextView tvDate;
RatingBar ratingBar;
}
}