Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
yilzer
4,601 PointsHow to Delete last user in ArrayAdapter<string>?
Hi guys,
I'm using a custom ArrayAdapter and everything works fine i can delete every single user and update the list, but when i want to delete my last user he won't dissapear. I tried everything, but i think you've to do something with an arraylist to delete that last user.
I hope you've an answer for me!
thanks in advance.
yilzer
4,601 Points try {
result = serverRequest.execute().get();
JSONObject jsonResponse = new JSONObject(result.toString());
JSONArray jsonUsername = jsonResponse.getJSONArray("friendRequests");
String [] usernames = new String[jsonUsername.length()];
for (int i = 0; i < jsonUsername.length(); i++) {
JSONObject user = jsonUsername.getJSONObject(i);
sUsername = user.getString("username");
friendUsername = sUsername.toString();
usernames[i] = sUsername;
ReceivedFriendsAdapter = new ReceivedFriendsAdapter(
getListView().getContext(), usernames);
setListAdapter(ReceivedFriendsAdapter);
}
public class ReceivedFriendsAdapter extends ArrayAdapter<String> {
public ReceivedFriendsAdapter(Context context, String[] usernames) {
super(context, R.layout.received_request_icon, usernames);
mContext = context;
mUsernames = usernames;
}
@Override
public final View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.received_request_icon, null);
holder = new ViewHolder();
holder.mOrange = (ImageView)convertView.findViewById(R.id.receivedIconOrange);
holder.mRemove = (ImageView) convertView.findViewById(R.id.RemoveFriendRequest);
else {
holder = (ViewHolder)convertView.getTag();
}
holder.mTextView.setText(mUsernames[position]);
holder.mGray.setImageResource(R.drawable.ic_gray_add_person);
return convertView;
}
private static class ViewHolder {
ImageView mGray;
TextView mTextView;
ImageView mOrange;
ImageView mRemove;
}
I delete my items by removing them from a php server, so that code is correct and its just that last item that doesn't removed. I think we have to do something in the adapter to add a view with arrayList and later delete that view.
I hope you see a solution with this code
1 Answer
Gunjeet Hattar
14,483 PointsHi Carl,
I'm just going through your code. It looks fine to me, just that I don't see any code where you might be deleting the user. However, I believe this discussion might be of help Remove all items in array adapter
yilzer
4,601 PointsHi Gunjeet,
Thanks for your fast reply! This was the thing i was looking for, thanks man! I have just one more question for you:) At the moment I set my icons for my row in the Listview in the custom ArrayAdapter<String> with ConvertView like this set up: -USER - SettingsIcon - TrashcanIcon - So when i click on the Settingsicon i want to remove or block my friend. I have the code for that but i really don't know where to place this code. At the moment I retrieve(store) this code in the custom ArrayAdapter<String> with (ConvertView)Onclicklistener, but this works not optimal to me. Should I do this here or is there another way to store this information elsewhere and retrieve this in die customAdapter? In my fragment where i host my customAdapter i use OnlistItemClick but this only works if you tap on a row in the Listview not when you click on an icon. Is there a way to set code about the settings -and trashcan icon in the onListItemClick or elsewhere in my hostFragment for the CustomAdapter?
I really would like to know this, because i've been struggling with this for a week now..
So i hope you've an answer for me!
Thanks in advance man!
Refael Ozeri
18,274 PointsRefael Ozeri
18,274 PointsMind sharing the code you got right now? kind of hard helping out if I can't see your code. =]