I have the following problem, and searched for a while but haven't got any solution from the net:
I have a custom list view, each item has the following layout (I just post the essential):
<LinearLayout>
<ImageView android:id="@+id/friendlist_iv_avatar" />
<TextView andorid:id="@+id/friendlist_tv_nick_name" />
<ImageView android:id="@+id/friendlist_iv_status_icon" />
</LinearLayout>
And I have a class FriendRowItem, which is inflated from the above layout:
public class FriendRowItem extends LinearLayout{
private ImageView ivAvatar;
private ImageView ivStatusIcon;
private TextView tvNickName;
public FriendRowItem(Context context) {
super(context);
RelativeLayout friendRow = (RelativeLayout) Helpers.inflate(context, R.layout.friendlist_row);
this.addView(friendRow);
ivAvatar = (ImageView)findViewById(R.id.friendlist_iv_avatar);
ivStatusIcon = (ImageView)findViewById(R.id.friendlist_iv_status_icon);
tvNickName = (TextView)findViewById(R.id.friendlist_tv_nick_name);
}
public void setPropeties(Friend friend) {
//Avatar
ivAvatar.setImageResource(friend.getAvatar().getDrawableResourceId());
//Status
Status.Type status = friend.getStatusType();
if ( status == Type.ONLINE) {
ivStatusIcon.setImageResource(R.drawable.online_icon);
} else {
ivStatusIcon.setImageResource(R.drawable.offline_icon);
}
//Nickname
String name = friend.g开发者_JAVA百科etChatID();
if ( friend.hasName()) {
name = friend.getName();
}
tvNickName.setText(name);
}
}
In the main activity, I have a custom listview: lvMainListView, with an custom adapter (whose class extends ArrayAdapter - and off course: override the method getView ), the data set of the adapter is: ArrayList<Friend> friends
:
private class FriendRowAdapter extends ArrayAdapter<Friend> {
public FriendRowAdapter(Context applicationContext, int friendlistRow,
ArrayList<Friend> friends) {
super(applicationContext, friendlistRow, friends);
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
Friend friend = getItem(position);
FriendRowItem row = (FriendRowItem) convertView;
if ( row == null ) {
row = new FriendRowItem(ShowFriendsList.this.getApplicationContext());
}
row.setPropeties( friend );
return row;
}
}
- the problem is when I change the status of a friend from OFFLINE to ONLINE, then call notifyDataSetChanged(), nothing happens : the status icon of that friend doesn't change. I tried debugging, and saw the code: notifyDataSetChanged() get called, but the custom getView() is not fired !
Can you please tell me, that is normal in Android, or did I do something wrong ? (I am using Android 1.5).
Thank you in advance,
SonI am sorry,
I have just found out: because I called the the method notifyDatasetChanged() in another thread (In fact, I am using Smack API to develop a chat client - and the method notifyDatasetChanged() is get called in a listener of smack, which in turn is run in another thread ).
The correct solution: notifyDatasetChanged should be called like this:
lvFriendList.post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
I hope this will spare somebody time, if they meet the same problem :).
I had the same problem. You need to add
@Override
public int getCount() {
return YOURLIST.length;
}
in the your adapter file
精彩评论