我是新手到android和工作在ListView与自定义listitem.I我有一个listView与自定义controlls包括一些图像和实验室,我的概率是当我点击一个项目它给我是错的listItem detail.most或许它给人的正下方列表项目的数据,可以anybuddy帮我解决这个问题,我的代码是:listItem上显示的listitem错误在android中单击
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;
}
}
Click事件
@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();
}
在此我点击事件是imageViewPostImage
哪里是你的ItemClickListener代码 – Dhina
@Dhina - 请等待我张贴的弟弟。 –
@Dhina - 请参阅我的更新..我编辑我的点击事件。 –