我正在为Android创建一个社交网络应用程序,我有一个列表视图,其中显示了帖子,我想要做的是给用户删除自己的帖子的可能性,我不知道知道如何在某些帖子中删除按钮,而不是全部。我希望你能解决我的问题,任何帮助都会非常感激。 谢谢。
这是Feed的适配器:
public class FeedListAdapter extends BaseAdapter {
private static final String TAG = FeedListAdapter.class.getSimpleName();
PopupWindow popWindow = null;
private Activity activity;
private LayoutInflater inflater;
private List<FeedItem> feedItems;
private String URL = "http://192.168.1.10/social/like.php";
private String user_id;
ViewHolder holder;
private int[] mHighlightedPositions = new int[6];
private String test;
private CommentsListAdapter listAdapter;
private List<CommentsListItem> commentsitems;
private LikesListAdapter listAdapter1;
private List<FriendsListItem> likesitems;
private String URL1 = "http://192.168.1.10/social/comments.php";
private String URL2 = "http://192.168.1.10/social/comment.php";
private String URL3 = "http://192.168.1.10/social/likes.php";
private static final String TAG_SUCCESS = "success";
FeedItem item;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public FeedListAdapter(Activity activity, List<FeedItem> feedItems, String user_id) {
this.activity = activity;
this.feedItems = feedItems;
this.user_id = user_id;
}
@Override
public int getCount() {
return feedItems.size();
}
@Override
public Object getItem(int location) {
return feedItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
public void refresh(List<FeedItem> newlist) {
feedItems.addAll(newlist);
this.notifyDataSetChanged();
}
public List<FeedItem> getData() {
return feedItems;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View vi = convertView;
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (inflater == null)
inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.posts_row, null);
holder = new ViewHolder();
holder.name = (Button)convertView .findViewById(R.id.name);
holder.timestamp = (TextView) convertView
.findViewById(R.id.time);
holder.statusMsg = (TextView) convertView
.findViewById(R.id.status);
holder. url = (TextView) convertView.findViewById(R.id.url);
holder.profilePic = (CircleImageView) convertView
.findViewById(R.id.profile_img);
holder.feedImageView = (FeedImageView) convertView
.findViewById(R.id.image);
holder.nbr_likes = (Button) convertView.findViewById(R.id.nbr_likes);
holder.nbr_comments = (Button)convertView.findViewById(R.id.nbr_comments);
holder.like =(Button)convertView.findViewById(R.id.like);
holder.comment =(Button)convertView.findViewById(R.id.comment);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
Log.d(TAG, "Button row pos click: " + getCount());
holder.like.setTag(position);
SharedPreferences prefs = activity.getSharedPreferences("mHighlightedPositions2", activity.MODE_PRIVATE);
String savedString = prefs.getString("string", "");
StringTokenizer st = new StringTokenizer(savedString, ",");
int[] savedList = new int[6];
for (int i = 0; i < 6; i++) {
while(st.hasMoreTokens())
mHighlightedPositions[i] = Integer.parseInt(st.nextToken());
}
if(mHighlightedPositions[position] == 1) {
holder.like.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like2, 0, 0, 0);
holder.like.setTextColor(Color.parseColor("#FF5722"));
notifyDataSetChanged();
}else {
holder.like.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like1, 0, 0, 0);
holder.like.setTextColor(Color.parseColor("#8B8B8F"));
notifyDataSetChanged();
}
holder.nbr_likes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item = feedItems.get(position);
LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = null;
PopupWindow popWindow = null;
// inflate the custom popup layout
inflatedView = layoutInflater.inflate(R.layout.likes, null, false);
// find the ListView in the popup layout
ListView listView = (ListView) inflatedView.findViewById(R.id.likesListView);
TextView emptyList = (TextView)inflatedView.findViewById(R.id.empty);
listView.setEmptyView(emptyList);
// get device size
Display display = activity.getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
int mDeviceHeight = size.y;
// fill the data to the list items
setLikesList(listView);
// set height depends on the device size
if(inflatedView!=null)
popWindow = new PopupWindow(inflatedView, size.x - 50, size.y - 400, true);
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.fb_popup_bg));
// make it focusable to show the keyboard to enter in `EditText`
popWindow.setFocusable(true);
// make it outside touchable to dismiss the popup window
popWindow.setOutsideTouchable(true);
// show the popup at bottom of the screen and set some margin at bottom ie,
if(popWindow!=null)
popWindow.showAtLocation(inflatedView, Gravity.BOTTOM, 0, 100);
}
});
holder.like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item = feedItems.get(position);
int position = (Integer) v.getTag();
Log.d(TAG, "Button row pos click: " + getCount());
LinearLayout layout = (LinearLayout) v.getParent();
Button button = (Button) layout.getChildAt(0);
if (mHighlightedPositions[position] == 1) {
test ="delete";
item.setNbrLikes(item.getNbrLikes()-1);
button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like1, 0, 0, 0);
button.setTextColor(Color.parseColor("#8B8B8F"));
mHighlightedPositions[position] = 0;
notifyDataSetChanged();
} else {
test ="add";
item.setNbrLikes(item.getNbrLikes()+1);
button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.like2, 0, 0, 0);
button.setTextColor(Color.parseColor("#FF5722"));
mHighlightedPositions[position] = 1;
notifyDataSetChanged();
}
Map<String, String> params = new HashMap<String, String>();
params.put("post_id", item.getId());
params.put("user_id", user_id);
params.put("test", test);
Toast.makeText(activity, item.getId(), Toast.LENGTH_SHORT).show();
Toast.makeText(activity, user_id, Toast.LENGTH_SHORT).show();
// making fresh volley request and getting json
CustomRequest jsonReq = new CustomRequest(Request.Method.POST,
URL, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
});
holder.comment.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
item = feedItems.get(position);
LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = null;
// inflate the custom popup layout
inflatedView = layoutInflater.inflate(R.layout.comment, null, false);
// find the ListView in the popup layout
ListView listView = (ListView) inflatedView.findViewById(R.id.commentsListView);
TextView nbr_likes_pop = (TextView) inflatedView.findViewById(R.id.nbr_likes_pop);
TextView emptyList = (TextView)inflatedView.findViewById(R.id.empty);
listView.setEmptyView(emptyList);
nbr_likes_pop.setText(item.getNbrLikes()+" Persons Like this");
if (item.getNbrLikes() == 0) nbr_likes_pop.setText("No Like For This Post Yet");
ImageButton send = (ImageButton) inflatedView.findViewById(R.id.send);
final EditText writeComment =(EditText)inflatedView.findViewById(R.id.writeComment);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item = feedItems.get(position);
if (writeComment.getText().toString() == "") {
} else {
item.setNbrComments(item.getNbrComments() + 1);
String comment = writeComment.getText().toString();
Long ts = System.currentTimeMillis();
String time = ts.toString();
String post_id = item.getId();
Map<String, String> params = new HashMap<String, String>();
params.put("post_id", post_id);
params.put("comment", comment);
params.put("time", time);
params.put("user_id", user_id);
CustomRequest jsonReq = new CustomRequest(Request.Method.POST,
URL2, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
parseJsonComment(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
popWindow.dismiss();
}
}
});
// get device size
Display display = activity.getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
int mDeviceHeight = size.y;
// fill the data to the list items
setCommentList(listView);
// set height depends on the device size
if(inflatedView!=null)
popWindow = new PopupWindow(inflatedView, size.x - 50, size.y - 400, true);
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.fb_popup_bg));
// make it focusable to show the keyboard to enter in `EditText`
popWindow.setFocusable(true);
// make it outside touchable to dismiss the popup window
popWindow.setOutsideTouchable(true);
// show the popup at bottom of the screen and set some margin at bottom ie,
if(popWindow!=null)
popWindow.showAtLocation(inflatedView, Gravity.BOTTOM, 0, 100);
}
});
holder.nbr_comments.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
item = feedItems.get(position);
LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = null;
PopupWindow popWindow = null;
// inflate the custom popup layout
inflatedView = layoutInflater.inflate(R.layout.comments, null, false);
// find the ListView in the popup layout
ListView listView = (ListView) inflatedView.findViewById(R.id.commentsListView);
TextView nbr_likes_pop = (TextView) inflatedView.findViewById(R.id.nbr_likes_pop);
TextView emptyList = (TextView)inflatedView.findViewById(R.id.empty);
listView.setEmptyView(emptyList);
nbr_likes_pop.setText(item.getNbrLikes()+" Persons Like this");
if (item.getNbrLikes() == 0) nbr_likes_pop.setText("No Like For This Post Yet");
// get device size
Display display = activity.getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
int mDeviceHeight = size.y;
// fill the data to the list items
setCommentList(listView);
// set height depends on the device size
if(inflatedView!=null)
popWindow = new PopupWindow(inflatedView, size.x - 50, size.y - 400, true);
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.fb_popup_bg));
// make it focusable to show the keyboard to enter in `EditText`
popWindow.setFocusable(true);
// make it outside touchable to dismiss the popup window
popWindow.setOutsideTouchable(true);
// show the popup at bottom of the screen and set some margin at bottom ie,
if(popWindow!=null)
popWindow.showAtLocation(inflatedView, Gravity.BOTTOM, 0, 100);
}
});
item = feedItems.get(position);
holder.name.setText(item.getName());
holder.nbr_likes.setText(item.getNbrLikes() + " Likes - ");
holder.nbr_comments.setText(item.getNbrComments() + " Comments");
// Converting timestamp into x ago format
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(item.getTimeStamp()),
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
holder.timestamp.setText(timeAgo);
// Chcek for empty status message
if (!TextUtils.isEmpty(item.getStatus())) {
holder.statusMsg.setText(item.getStatus());
holder.statusMsg.setVisibility(View.VISIBLE);
} else {
// status is empty, remove from view
holder.statusMsg.setVisibility(View.GONE);
}
// Checking for null feed url
if (item.getUrl() != null) {
holder.url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
+ item.getUrl() + "</a> "));
// Making url clickable
holder.url.setMovementMethod(LinkMovementMethod.getInstance());
holder.url.setVisibility(View.VISIBLE);
} else {
// url is null, remove from the view
holder.url.setVisibility(View.GONE);
}
// user profile pic
holder.profilePic.setImageUrl(item.getProfilePic(), imageLoader);
// Feed image
if (item.getImge() != null) {
holder.feedImageView.setImageUrl(item.getImge(), imageLoader);
holder.feedImageView.setVisibility(View.VISIBLE);
holder.feedImageView
.setResponseObserver(new FeedImageView.ResponseObserver() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
}
});
} else {
holder.feedImageView.setVisibility(View.GONE);
}
SharedPreferences.Editor editor = activity.getSharedPreferences("mHighlightedPositions2", activity.MODE_PRIVATE).edit();
StringBuilder str = new StringBuilder();
for (int i = 0; i < mHighlightedPositions.length; i++) {
str.append(mHighlightedPositions[i]).append(",");
}
editor.putString("string", str.toString());
return convertView;
}
void setCommentList(ListView listView){
commentsitems = new ArrayList<CommentsListItem>();
listAdapter = new CommentsListAdapter(activity, commentsitems);
listView.setAdapter(listAdapter);
Map<String, String> params = new HashMap<String, String>();
params.put("post_id", item.getId());
// making fresh volley request and getting json
CustomRequest jsonReq = new CustomRequest(Request.Method.POST,
URL1,params , new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
parseJsonComments(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
void setLikesList(ListView listView){
likesitems = new ArrayList<FriendsListItem>();
listAdapter1 = new LikesListAdapter(activity, likesitems);
listView.setAdapter(listAdapter1);
Map<String, String> params = new HashMap<String, String>();
params.put("post_id", item.getId());
// making fresh volley request and getting json
CustomRequest jsonReq = new CustomRequest(Request.Method.POST,
URL3,params , new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
parseJsonLikes(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
static class ViewHolder {
Button name;
TextView timestamp;
TextView statusMsg;
TextView url;
CircleImageView profilePic;
FeedImageView feedImageView;
Button nbr_likes;
Button nbr_comments;
Button like;
Button comment;
}
private void parseJsonFeed(JSONObject response) {
int success;
try {
success = response.getInt(TAG_SUCCESS);
if (success == 1) {
notifyDataSetChanged();
} else {
Toast.makeText(activity, "erreur", Toast.LENGTH_SHORT).show();
}
//listAdapter.addAll(feedItems);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void parseJsonComments(JSONObject response) {
int success;
try {
success = response.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray feedArray = response.getJSONArray("comments");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
CommentsListItem item = new CommentsListItem();
item.setId(feedObj.getString("comment_id"));
item.setName(feedObj.getString("name"));
item.setComment(feedObj.getString("comment"));
item.setProfile_img(feedObj.getString("profile_img"));
item.setTime(feedObj.getString("time"));
commentsitems.add(item);
// Toast.makeText(getContext(),feedObj.getString("text"), Toast.LENGTH_LONG).show();
}
// update data in our adapter
listAdapter.notifyDataSetChanged();
// notify data changes to list adapater
} else {
}
//listAdapter.addAll(feedItems);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void parseJsonLikes(JSONObject response) {
int success;
try {
success = response.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray feedArray = response.getJSONArray("likes");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
FriendsListItem item = new FriendsListItem();
item.setName(feedObj.getString("name"));
item.setProfImage(feedObj.getString("profile_img"));
likesitems.add(item);
// Toast.makeText(getContext(),feedObj.getString("text"), Toast.LENGTH_LONG).show();
}
// update data in our adapter
listAdapter1.notifyDataSetChanged();
// notify data changes to list adapater
} else {
}
//listAdapter.addAll(feedItems);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void parseJsonComment(JSONObject response) {
int success;
try {
success = response.getInt(TAG_SUCCESS);
if (success == 1) {
notifyDataSetChanged();
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}