我正在进行一项任务,要求我在一行中显示姓名和号码。我尝试创建一个自定义类,联系人,以容纳字符串,名称和数字。代码粘贴在下面:
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1. The class ContactsContract allows access to various types of data stored in the phone
//Examples: Contacts, Phone settings, Contact groups etc.
//Creates cursor to search contacts
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//Creates array to store string (names from contact)
ArrayAdapter<Contact> list = new ArrayAdapter<Contact>(this,
R.layout.activity_main);
//2. Using the ContactsContract class above, we got a cursor to the data. Now we iterate through it to get all the data
while (cursor.moveToNext()) {
//3. In our example, we get the DISPLAY_NAME but other data elements are also available
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact newContact = new Contact();
newContact.setName(name);
newContact.setNumber(number);
list.add(newContact);
}
setListAdapter(list);
}
}
这是我的联系班:
public class Contact {
private String name;
private String number;
void setName(String Name){
name = Name;
}
void setNumber (String Number){
number = Number;
}
}
答案 0 :(得分:1)
实现它的关键是管理联系人列表的客户适配器。
<强> CustomeAdapter.java 强>
public class CustomeAdapter extends ArrayAdapter {
private final Activity activity;
private final List stocks;
public CustomeAdapter(Activity activity, List objects) {
super(activity, R.layout.row_list_item , objects);
this.activity = activity;
this.stocks = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ManageView mview = null;
if(rowView == null)
{
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.row_list_item, null);
mview= new ManageView();
mview.namev = (TextView) rowView.findViewById(R.id.nameid);
mview.numberv = (TextView) rowView.findViewById(R.id.numberid);
// Cache the view objects
rowView.setTag(mview);
} else {
mview= (ManageView) rowView.getTag();
}
Contact currentContact = stocks.get(position);
mview.namev.setText(currentContact.getName());
mview.numberv.setText(currentContact.getNumber());
return rowView;
}
protected static class ManageView
protected TextView namev;
protected TextView numberv;
}
}
<强> row_list_item.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/nameid" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/numberid" />
</LinearLayout>
在您的活动中
/* Creates array to store string (names from contact)
ArrayAdapter<Contact> list = new ArrayAdapter<Contact>(this,
R.layout.activity_main);*/
ArrayAdapter<Contact> list = new ArrayAdapter<Contact>();
while (cursor.moveToNext()) {
//3. In our example, we get the DISPLAY_NAME but other data elements are also available
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact newContact = new Contact();
newContact.setName(name);
newContact.setNumber(number);
list.add(newContact);
}
setListAdapter(new CustomeAdapter(this, list));
答案 1 :(得分:0)
Listview有一个setAdapter方法。所以只需调用listview.setAdapter(your_list_adapter_here);
您的列表适配器是从列表中创建的
您创建了一个适配器类,如Custom Adapter for List View
所示