我想在listView上获得conctacts,我想要reutilitzate一个SocialAuth代码。 在这段代码中,我可以在LogCat上获取ContactList,但不能在listView中获取,我不知道如何做到这一点。此listView位于其他XML上。
我很瞪眼,但我不知道如何调整代码
public void Events(String provider) {
setContentView(R.layout.contact_list);
List < Contact > contactsList = adapter.getContactList();
if (contactsList != null && contactsList.size() > 0) {
for (Contact p: contactsList) {
if (TextUtils.isEmpty(p.getFirstName()) && TextUtils.isEmpty(p.getLastName())) {
p.setFirstName(p.getDisplayName());
}
Log.d("Custom-UI", "Display Name = " + p.getDisplayName());
String ContactNAme = p.getDisplayName();
//ContactName = new String[] {p.getDisplayName()};
//mapTo = new int[] {android.R.id.text1};
Log.d("Custom-UI", "First Name = " + p.getFirstName());
String ContactFisrtName = p.getFirstName();
Log.d("Custom-UI", "Last Name = " + p.getLastName());
String ContactLastName = p.getLastName();
Log.d("Custom-UI", "Contact ID = " + p.getId());
String ContactId = p.getId();
Log.d("Custom-UI", "Profile URL = " + p.getProfileUrl());
String ContactProfileUrl = p.getProfileUrl();
}
// Log.d("ContactList",mAdapter.toString());
}
Toast.makeText(CustomUI.this, "View Logcat for Contacts Information", Toast.LENGTH_SHORT).show();
}
我认为问题是光标,因为我没有任何人,我有这个功能,我觉得它有点像光标
public List<Contact> getContactList()
{
try
{
contactsList = new contactTask().execute().get();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
return contactsList;
}
所以,如果有人可以帮助我,非常感谢。
答案 0 :(得分:0)
您需要为列表视图编写自定义适配器。
这是一个很好的例子
答案 1 :(得分:0)
答案 2 :(得分:0)
// Call contact thread
contact_thread = new Contact_thread();
contact_thread.start();
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, null, selectionArgs, sortOrder);
}
class Contact_thread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
// Build adapter with contact entries
Cursor cursor = getContacts();
cursor.moveToFirst();
contactName = new String[cursor.getCount()];
contactNo = new String[cursor.getCount()];
checkedPosition = new boolean[cursor.getCount()];
ContentResolver contect_resolver = getContentResolver();
int i = 0;
if (cursor.getCount() > 0) {
do {
String id = cursor
.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
Cursor phoneCur = contect_resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
contactName[i] = phoneCur
.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
contactNo[i] = phoneCur
.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (contactName[i] == null) {
contactName[i] = "Unknown";
}
} else {
contactName[i] = "Unknown";
contactNo[i] = "";
}
db.AddContact(contactName[i], contactNo[i]);
i++;
phoneCur.close();
} while (cursor.moveToNext());
}
cursor.close();
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// mContactList.setAdapter(cursorAdapter);
mContactList.setAdapter(new ContactAdapter(
ContactManager.this, R.layout.contact_entry));
}
});
}
}
private class ContactAdapter extends ArrayAdapter<String> implements
Filterable {
public ContactAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
public int getCount() {
return contactName.length;
}
public String getItem(int position) {
return contactName[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.contact_entry, null);
} else {
v = convertView;
}
CheckedTextView text = (CheckedTextView) v.findViewById(R.id.text1);
text.setText(contactName[position] + " (" + contactNo[position]
+ ") ");
text.setChecked(checkedPosition[position]);
return v;
}
}
contact_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#AA0114"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />