我是新手到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 - 請參閱我的更新..我編輯我的點擊事件。 –