谁能告诉我应该如何在自定义列表视图中显示电话号码和联系人姓名?代码粘贴在下面
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class AddContacts extends Activity implements OnClickListener {
ListView list;
Cursor cursor;
SimpleCursorAdapter adapter;
String phoneNumber;
Button AddNumber;
EditText number1;
EditText number2;
EditText number3;
String contact1;
String contact2;
CheckBox checkBox;
String contact_name;
static final String TAG = "In AddContacts";
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
AddNumber = (Button) findViewById(R.id.AddNumbers);
number1 = (EditText) findViewById(R.id.NumberField1);
number2 = (EditText) findViewById(R.id.NumberField2);
number3 = (EditText) findViewById(R.id.NumberField3);
AddNumber.setOnClickListener(this);
list = (ListView) findViewById(R.id.lists);
Log.d(TAG, "onCreate");
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
String[] nameNumberArray = new String[cursor.getCount()];
Log.d("rows", Integer.toString(cursor.getCount()));
Log.d("phone", "here");
while (cursor.moveToNext()) {
String phoneNumber =
cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i("Number + Name", phoneNumber + name);
}
if (cursor.getCount() > 0) {
int i = 0;
if (cursor.moveToFirst()) {
do {
phoneNumber = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact_name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
nameNumberArray[i] = contact_name + " , " + phoneNumber;
i++;
} while (cursor.moveToNext());
}
}
final String[] FROM = { nameNumberArray[0], nameNumberArray[1] };
final int[] TO = { R.id.contact_name, R.id.phone_number, };
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
list.setAdapter(adapter);
cursor.close();
}
@Override
public void onClick(View v) {
Log.d(TAG, "onClicked method");
}
}
ROW.xml 文件是一个自定义的 listView,带有一个复选框和两个 textView。该代码从联系人中提取数据并将其显示在 LogCat 中。我不知道如何使用 simpleCursorAdapter 在我的自定义视图中显示名称和数字。
row.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="John Doe"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/contact_name"
android:text="(999)999-9999"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="@+id/checkBox_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
谁能指导我?谢谢。