android中的listitem单击时在listitem上显示错误的listitem
我是android新手,正在使用自定义listitem处理listView。我有一个带有自定义控件的listView,包括一些图像和标签,我的问题是当我单击某个项目时,它给了我错误的listitem详细信息。最有可能的是,它给了下面的列表项数据,Anybody可以帮助我修复它吗,我的代码是:android中的listitem单击时在listitem上显示错误的listitem,android,listview,android-adapter,Android,Listview,Android Adapter,我是android新手,正在使用自定义listitem处理listView。我有一个带有自定义控件的listView,包括一些图像和标签,我的问题是当我单击某个项目时,它给了我错误的listitem详细信息。最有可能的是,它给了下面的列表项数据,Anybody可以帮助我修复它吗,我的代码是: package one.tusk.stush.adapters; import android.content.Context; import android.view.LayoutInflater; i
package one.tusk.stush.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.company.stush.R;
import java.util.List;
import one.tusk.stush.connect.Post;
import one.tusk.stush.views.PostListItem;
public class TimelineAdapter extends ArrayAdapter<Post> {
private final LayoutInflater mInflater;
public TimelineAdapter(Context context) {
super(context, R.layout.list_item_post);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setData(List<Post> data) {
clear();
if (data != null) {
addAll(data);
notifyDataSetChanged();
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Post post = getItem(position);
PostListItem view;
if (convertView == null) {
view = (PostListItem) mInflater.inflate(R.layout.list_item_post, parent, false);
} else {
view = (PostListItem) convertView;
}
view.setPost(post);
return view;
}
}
package one.tusk.stush.adapters;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
进口公司stush.R;
导入java.util.List;
导入one.tusk.stush.connect.Post;
导入one.tusk.stush.views.PostListItem;
公共类TimelineAdapter扩展了ArrayAdapter{
私人最终布局平面图;
公共TimelineAdapter(上下文){
超级(上下文、右布局、列表、项目、帖子);
mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
}
公共无效设置数据(列表数据){
清除();
如果(数据!=null){
addAll(数据);
notifyDataSetChanged();
}
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
Post Post=获取项目(位置);
PostListItem视图;
if(convertView==null){
视图=(PostListItem)mInflater.inflate(R.layout.list\u item\u post,父项,false);
}否则{
视图=(PostListItem)转换视图;
}
视图.setPost(post);
返回视图;
}
}
单击事件
@Override
public void onClick(final View button)
{
if (button == buttonLike) {
if (!mPost.likedThisPost) {
buttonLike.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.btn_likes, 0, 0, 0);
} else {
buttonLike.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.btn_liked, 0, 0, 0);
}
} else if (button == buttonLikes) {
Intent likesIntent = new Intent(PostListItem.this.getContext(),
LikesActivity.class);
likesIntent.putExtra("postID", mPost.postID);
PostListItem.this.getContext().startActivity(likesIntent);
} else if (button == buttonComment || button == mNumberOfComments) {
Intent commentIntent = new Intent(PostListItem.this.getContext(),
CommentsActivity.class);
commentIntent.putExtra("postID", mPost.postID);
PostListItem.this.getContext().startActivity(commentIntent);
} else if (button == buttonShare) {
// Log.e("Click button Share", "Success");
album = mPost.inAlbum;
showPopupMenu(button);
}
else if(button == imageViewPostImage) {
//Toast.makeText(PostListItem.this.getContext(),"item Clicked...!!!",Toast.LENGTH_LONG).show();
if(Const.post_det.equals("0")) {
Intent intentPostDetail = new Intent(PostListItem.this.getContext(), PostDetailActivity.class);
Post post = mPost;
System.out.print("========MY POST IS======>" + mPost.toString());
intentPostDetail.putExtra("Post", post);
intentPostDetail.putExtra("flag", "post");
intentPostDetail.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
getContext().startActivity(intentPostDetail);
flag_det = "1";
//Const.post_det = "1";
}
}else if(button == imguser) {
flag_pro = "1";
showUser();
}
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
Connect sharedConnect = Connect.getInstance(getContext());
if(button == buttonShare)
{
//return sharedConnect.deletePost(mPost.postID);
}
if (button == buttonLike) {
if (!mPost.likedThisPost) {
///buttonLike.setCompoundDrawablesWithIntrinsicBounds(
// R.drawable.btn_likes, 0, 0, 0);
return sharedConnect.likePost(mPost.postID);
} else {
//buttonLike.setCompoundDrawablesWithIntrinsicBounds(
// R.drawable.btn_liked, 0, 0, 0);
return sharedConnect.unlikePost(mPost.postID);
}
} else if (button == buttonLikes) {
return true;
} else if (button == buttonComment
|| button == mNumberOfComments) {
return true;
} else {
return null;
}
}
protected void onPostExecute(Boolean result) {
if (button == buttonLike) {
if (result && !mPost.likedThisPost) {
buttonLike.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.btn_likes, 0, 0, 0);
mPost.likedThisPost = true;
mPost.postLikesCount++;
} else if (result) {
buttonLike.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.btn_liked, 0, 0, 0);
mPost.likedThisPost = false;
mPost.postLikesCount--;
}
buttonLikes.setText(likeOrLikes(mPost.postLikesCount));
} else if (button == buttonLikes) {
} else if (button == buttonComment
|| button == mNumberOfComments) {
} else if (button == buttonShare) {
}else if(button == imageViewPostImage){
// Intent intentPostDetail = new Intent(PostListItem.this.getContext(), PostDetailActivity.class);
// Post post = mPost;
// intentPostDetail.putExtra("Post", post);
// getContext().startActivity(intentPostDetail);
if(flag_det.equals("0")) {
Intent intentPostDetail = new Intent(PostListItem.this.getContext(), PostDetailActivity.class);
Post post = mPost;
System.out.print("========MY POST IS======>" + mPost.toString());
Toast.makeText(getContext(),post.postID,Toast.LENGTH_LONG).show();
intentPostDetail.putExtra("flag", "post");
intentPostDetail.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intentPostDetail.putExtra("Post", post);
getContext().startActivity(intentPostDetail);
flag_det = "1";
}else {
flag_det = "0";
}
}
else if(button == imguser) {
if(flag_pro.equals("0")) {
showUser();
flag_pro = "1";
}else {
flag_pro = "0";
}
}
}
}.execute();
}
@覆盖
公共void onClick(最终视图按钮)
{
如果(按钮==按钮状){
如果(!mPost.likedThisPost){
按钮式。SetCompoundDrawableswithInInstructBounds(
R.drawable.btn_likes,0,0,0);
}否则{
按钮式。SetCompoundDrawableswithInInstructBounds(
R.drawable.btn_,0,0,0);
}
}else if(按钮==按钮类){
Intent-likeContent=新的意图(PostListItem.this.getContext(),
喜欢活动(课堂);
类似于content.putExtra(“postID”,mPost.postID);
PostListItem.this.getContext().startActivity(likeContent);
}else if(按钮==按钮注释| |按钮==注释数){
Intent commentIntent=新意图(PostListItem.this.getContext(),
评论活动类);
commentIntent.putExtra(“postID”,mPost.postID);
PostListItem.this.getContext().startActivity(commentIntent);
}else if(按钮==按钮共享){
//Log.e(“单击按钮共享”、“成功”);
相册=mPost.inAlbum;
显示弹出菜单(按钮);
}
else if(按钮==imageViewPostImage){
//Toast.makeText(PostListItem.this.getContext(),“项目已点击…”,Toast.LENGTH\u LONG.show();
if(常数后测等于(“0”)){
Intent intentPostDetail=新意图(PostListItem.this.getContext(),PostDetailActivity.class);
Post=mPost;
System.out.print(“=============我的帖子是=====>”+mPost.toString());
intentPostDetail.putExtra(“Post”,Post);
intentPostDetail.putExtra(“旗帜”、“帖子”);
intentPostDetail.addFlags(Intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
getContext().startActivity(intentPostDetail);
flag_det=“1”;
//Const.post_det=“1”;
}
}else if(按钮==imguser){
flag_pro=“1”;
showUser();
}
新建异步任务(){
@凌驾
受保护的布尔doInBackground(Void…params){
Connect sharedConnect=Connect.getInstance(getContext());
如果(按钮==按钮共享)
{
//返回sharedConnect.deletePost(mPost.posted);
}
如果(按钮==按钮状){
如果(!mPost.likedThisPost){
///按钮式。SetCompoundDrawableswithInInstructBounds(
//R.drawable.btn_likes,0,0,0);
返回SharedConnection.likePost(mPost.posted);
}否则{
//按钮式。SetCompoundDrawableswithInInstructBounds(
//R.drawable.btn_,0,0,0);
返回SharedConnection.unlikePost(mPost.posted);
}
}else if(按钮==按钮类){
返回true;
}否则如果(按钮==按钮注释
||按钮==mnumberof注释){
返回true;
}否则{
返回null;
}
}
受保护的void onPostExecute(布尔结果){
如果(按钮==按钮状){
if(结果与此帖子类似){
按钮式。SetCompoundDrawableswithInInstructBounds(
R.drawable.btn_likes,0,0,0);
mPost.likedThisPost=真;
mPost.postLikesCount++;
}否则,如果(结果){
按钮式。SetCompoundDrawableswithInInstructBounds(
R.drawable.btn_,0,0,0);
mPost.likedThisPost=false;
mPost.postLikesCount--;
}
按钮类。设置文本(类似或
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final PostModel item = getItem(position);
int itemType = getItemViewType(position);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListener.onItemClick(null, null, position, getItemId(position));
}
};
final ViewHolderVideo holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.row_list_videos, parent, false);
holder = new ViewHolderVideo();
holder.videoImage = (ImageView) convertView.findViewById(R.id.image_video_preload);
holder.title = (TextView) convertView.findViewById(R.id.text_video_title);
holder.date = (TextView) convertView.findViewById(R.id.text_video_date);
holder.share = (TextView) convertView.findViewById(R.id.btn_share);
holder.download = (TextView) convertView.findViewById(R.id.btn_download);
convertView.setTag(holder);
} else {
holder = (ViewHolderVideo) convertView.getTag();
}
if (item != null) {
String thumbLink = item.getThumbnailLink();
if (!TextUtils.isEmpty(thumbLink)) {
ImageLoader.displayImage(mContext, holder.videoImage, thumbLink);
}
if (!TextUtils.isEmpty(item.getTitlePlain())) {
holder.title.setText(item.getTitlePlain());
} else {
holder.title.setText("");
}
if (!TextUtils.isEmpty(item.getDate())) {
holder.date.setText(item.getDate());
} else {
holder.date.setVisibility(View.GONE);
}
holder.share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationHelper.changeIconAnim(holder.share, R.string.icon_mail_reply);
IntentHelper.shareOnSocials(mContext, item.getTitlePlain() + "\n" + item.getMediaLink());
}
});
holder.download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationHelper.changeIconAnim(holder.download, R.string.icon_download);
downloadVideoFile(item);
}
});
holder.videoImage.setOnClickListener(onClickListener);
holder.title.setOnClickListener(onClickListener);
holder.date.setOnClickListener(onClickListener);
convertView.setOnClickListener(onClickListener);
}
return convertView;
}