Android 为什么在recyclerview中显示相同的数据
为什么在recyclerview中显示相同的数据 我正在尝试获取所有联系人并将其列在RecycleService中,但它显示相同的联系人(意味着:如果联系人数为50,则它将显示一个联系人50次)Android 为什么在recyclerview中显示相同的数据,android,android-recyclerview,android-contacts,Android,Android Recyclerview,Android Contacts,为什么在recyclerview中显示相同的数据 我正在尝试获取所有联系人并将其列在RecycleService中,但它显示相同的联系人(意味着:如果联系人数为50,则它将显示一个联系人50次) private void displayContacts(){ List arrayList=新建arrayList(); ContactsModel数据=新ContactsModel(); ContentResolver cr=getContentResolver(); Cursor cur=cr.q
private void displayContacts(){
List arrayList=新建arrayList();
ContactsModel数据=新ContactsModel();
ContentResolver cr=getContentResolver();
Cursor cur=cr.query(Contacts contract.Contacts.CONTENT\u URI,
空,空,空,空);
如果(cur.getCount()>0){
while(cur.moveToNext()){
字符串id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._id));
字符串名称=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_name));
if(Integer.parseInt(cur.getString(
cur.getColumnIndex(Contacts.Contacts.HAS_PHONE_NUMBER))>0){
游标pCur=cr.query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
ContactsContract.CommonDataTypes.Phone.CONTACT_ID+“=?”,
新字符串[]{id},null);
while(pCur.moveToNext()){
String phoneNo=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
数据。设置_id(id);
data.setName(name);
数据。设置号码(电话号码);
Log.e(“测试”,“名称:+Name+”,电话号码:+phoneNo);
}
pCur.close();
}arrayList.add(数据);
}
}
adapter=新的ContactAdapter(arrayList,getApplicationContext());
mRecyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
cur.close();
}试试这个,我做了一些更改:
private void displayContacts() {
String name_check="start";
List<ContactsModel> arrayList = new ArrayList<ContactsModel>();
ContactsModel data = new ContactsModel();
ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToFirst()) {
do{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(name check.equals(name)){
name_check = name;
data.set_id(id);
data.setName(name);
data.setNumber(phoneNo);
Log.e("test", "Name: " + name + ", Phone No: " + phoneNo);
arrayList.add(data);
}
}
pCur.close();
}
} while(cur.moveToNext());
}
}
adapter = new ContactAdapter(arrayList, getApplicationContext());
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
cur.close();
}
private void displayContacts(){
字符串名称\u check=“start”;
List arrayList=新建arrayList();
ContactsModel数据=新ContactsModel();
ContentResolver cr=getContentResolver();
最终光标cur=cr.query(ContactsContract.Contacts.CONTENT\u URI,
空,空,空,空);
如果(cur.getCount()>0){
while(cur.moveToFirst()){
做{
字符串id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._id));
if(Integer.parseInt(cur.getString(
cur.getColumnIndex(Contacts.Contacts.HAS_PHONE_NUMBER))>0){
游标pCur=cr.query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
ContactsContract.CommonDataTypes.Phone.CONTACT_ID+“=?”,
新字符串[]{id},null);
while(pCur.moveToNext()){
String phoneNo=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
字符串名称=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_name));
if(名称检查。等于(名称)){
名称\检查=名称;
数据。设置_id(id);
data.setName(name);
数据。设置号码(电话号码);
Log.e(“测试”,“名称:+Name+”,电话号码:+phoneNo);
arrayList.add(数据);
}
}
pCur.close();
}
}while(cur.moveToNext());
}
}
adapter=新的ContactAdapter(arrayList,getApplicationContext());
mRecyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
cur.close();
}
试试这个,我做了一些更改:
private void displayContacts() {
String name_check="start";
List<ContactsModel> arrayList = new ArrayList<ContactsModel>();
ContactsModel data = new ContactsModel();
ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToFirst()) {
do{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(name check.equals(name)){
name_check = name;
data.set_id(id);
data.setName(name);
data.setNumber(phoneNo);
Log.e("test", "Name: " + name + ", Phone No: " + phoneNo);
arrayList.add(data);
}
}
pCur.close();
}
} while(cur.moveToNext());
}
}
adapter = new ContactAdapter(arrayList, getApplicationContext());
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
cur.close();
}
private void displayContacts(){
字符串名称\u check=“start”;
List arrayList=新建arrayList();
ContactsModel数据=新ContactsModel();
ContentResolver cr=getContentResolver();
最终光标cur=cr.query(ContactsContract.Contacts.CONTENT\u URI,
空,空,空,空);
如果(cur.getCount()>0){
while(cur.moveToFirst()){
做{
字符串id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._id));
if(Integer.parseInt(cur.getString(
cur.getColumnIndex(Contacts.Contacts.HAS_PHONE_NUMBER))>0){
游标pCur=cr.query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
ContactsContract.CommonDataTypes.Phone.CONTACT_ID+“=?”,
新字符串[]{id},null);
while(pCur.moveToNext()){
String phoneNo=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
字符串名称=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_name));
if(名称检查。等于(名称)){
名称\检查=名称;
数据。设置_id(id);
data.setName(name);
数据。设置号码(电话号码);
Log.e(“测试”,“名称:+Name+”,电话号码:+phoneNo);
arrayList.add(数据);
}
}
pCur.close();
}
}while(cur.moveToNext());
}
}
adapter=新的ContactAdapter(arrayList,getApplicationContext());
mRecyclerView.setAdapter(适配器);
adapter.notifyDataSetChanged();
cur.close();
}
感谢Ans,但我以前已经解决了,但现在我希望所有联系人都不重复。您可以设置一个简单的if条件。为此,在每个循环中为store number fetch声明一个变量,并在满足条件后使用新的fetch number对其进行初始化,并在条件为true后在数组列表中添加数据。@初学者我已根据您的要求更新了我的答案,请再次查看此内容。感谢Ans,但我以前已经解决了它,但现在我需要所有联系人没有重复,你可以设置一个简单的if条件。为此,在每个循环中为storenumber fetch声明一个变量,并用n初始化它