有谁能告诉我如何在自定义列表视图中显示电话号码和联系人姓名? 代码粘贴在
下面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>
任何人都可以指导我吗? 感谢。
答案 0 :(得分:15)
由于op要求使用自定义适配器的解决方案,我已经发布了以下内容。(来自评论)
Display.java
public class Display extends Activity implements OnItemClickListener{
List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
MyAdapter ma ;
Button select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
getAllContacts(this.getContentResolver());
ListView lv= (ListView) findViewById(R.id.lv);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
// adding
select = (Button) findViewById(R.id.button1);
select.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
StringBuilder checkedcontacts= new StringBuilder();
System.out.println(".............."+ma.mCheckStates.size());
for(int i = 0; i < name1.size(); i++)
{
if(ma.mCheckStates.get(i)==true)
{
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
}
else
{
System.out.println("Not Checked......"+name1.get(i).toString());
}
}
Toast.makeText(Display.this, checkedcontacts,1000).show();
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
ma.toggle(arg2);
}
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(".................."+phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
}
phones.close();
}
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
{ private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1,tv;
CheckBox cb;
MyAdapter()
{
mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.row, null);
TextView tv= (TextView) vi.findViewById(R.id.textView1);
tv1= (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :"+ name1.get(position));
tv1.setText("Phone No :"+ phno1.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
System.out.println("hello...........");
notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}
display.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:id="@+id/lv"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Select" />
</RelativeLayout>
说明:
以上使用自定义适配器。自定义布局row.xml
包含2个textviews和一个复选框,每行都会显示一个。 getAllContacts()
将从联系人列表中获取所有联系人,并将其存储在列表中。
自定义适配器显示为每行充气的自定义布局中的项目。
勾选复选框并单击显示屏会显示带有所选联系人姓名的祝酒词。
快照
答案 1 :(得分:0)
我认为您应该再次调用cursor.moveToFirst()
,然后再将其传递给SimpleCursorAdapter
,因为现在您正在发送“已完成”的游标。
除此之外,你的代码似乎很好。您可以将它与代码here进行比较,代码执行类似的操作(但使用用户定义的数据库)。