android listview不能刷新 adapter.notifyDataSetChanged()和setListAdapter(myAdapter)都不好用
publicclassPortraitContectPicActivityextendsListActivity{ContextmContext=null;private...
public class PortraitContectPicActivity extends ListActivity {
Context mContext = null;
private static final String[] PHONES_PROJECTION = new String[] {
...
private ArrayList<String> mContactsName = new ArrayList<String>();
...
ListView mListView = null;
MyListAdapter myAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
mContext = this;
mListView = this.getListView();
getPhoneContacts();
myAdapter = new MyListAdapter(this);
setListAdapter(myAdapter);
Intent intent=getIntent();
String value=intent.getStringExtra(Util.RESULT_EDIT_PROTRAIT_ID);
final String pic = value;
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
ContentResolver c = getContentResolver();
Bitmap sourceBitmap = BitmapFactory.decodeFile(pic);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
byte[] avatar =os.toByteArray();
ContentValues values = new ContentValues();
Cursor cursor = c.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + mContactsName.get(position) + "'", null,
null);
if (cursor.moveToFirst()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
values.put(Photo.PHOTO, avatar);
getContentResolver().update(android.provider.ContactsContract.Data.CONTENT_URI, values, " raw_contact_id=? and mimetype=?", new String[]{contactId,Photo.CONTENT_ITEM_TYPE});
}
cursor.close();
setListAdapter(myAdapter);
}
});
super.onCreate(savedInstanceState);
}
/**得到手机通讯录联系人信息**/
private void getPhoneContacts() {
...
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
}
phoneCursor.close();
}
}
} 展开
Context mContext = null;
private static final String[] PHONES_PROJECTION = new String[] {
...
private ArrayList<String> mContactsName = new ArrayList<String>();
...
ListView mListView = null;
MyListAdapter myAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
mContext = this;
mListView = this.getListView();
getPhoneContacts();
myAdapter = new MyListAdapter(this);
setListAdapter(myAdapter);
Intent intent=getIntent();
String value=intent.getStringExtra(Util.RESULT_EDIT_PROTRAIT_ID);
final String pic = value;
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
ContentResolver c = getContentResolver();
Bitmap sourceBitmap = BitmapFactory.decodeFile(pic);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
byte[] avatar =os.toByteArray();
ContentValues values = new ContentValues();
Cursor cursor = c.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + mContactsName.get(position) + "'", null,
null);
if (cursor.moveToFirst()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
values.put(Photo.PHOTO, avatar);
getContentResolver().update(android.provider.ContactsContract.Data.CONTENT_URI, values, " raw_contact_id=? and mimetype=?", new String[]{contactId,Photo.CONTENT_ITEM_TYPE});
}
cursor.close();
setListAdapter(myAdapter);
}
});
super.onCreate(savedInstanceState);
}
/**得到手机通讯录联系人信息**/
private void getPhoneContacts() {
...
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
}
phoneCursor.close();
}
}
} 展开
2个回答
展开全部
你没有往你的Adapter里添加数据。如果你要调用Adapter.notifyDatasetChanged()这个方法,你只能王Adapter里面的List添加数据,而不能每次都给一个新的List对象,否则也无法刷新数据。
追问
我做的是程序调用通讯录先读取联系人姓名电话头像,再点击按钮修改联系人头像,点击按钮把图片给联系人设上了,然后想让listview叶更新一下,但listview显示的还是原来的头像(通讯录里的新头像已经设置上了),
不是添加,是更新,Adapter.notifyDatasetChanged()就不起作用吗?那要怎么做呢?
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询