我已经创建了自定义适配器和自定义列表项。我最初使用的是一个简单的列表项,只是填充了名称或数字。但是,现在我想在每个列表项中显示名称和电话号码。当我在自动填充文本中输入内容时,它不会填充任何内容。我做了一些错误测试,似乎它甚至没有调用重写的getView()方法。我似乎无法弄清楚为什么这不起作用。
另外,是的,我在清单中添加了READ_CONTACTS权限。
MainActivity.java
package com.sncrmck.autocompletetest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.widget.AutoCompleteTextView;
import com.sncrmck.adapter.ContactAdapter;
import com.sncrmck.obj.SimpleContact.SimpleContact;
public class MainActivity extends Activity {
AutoCompleteTextView acTextField;
private ContactAdapter adapter;
public ArrayList<SimpleContact> contactList = new ArrayList<SimpleContact>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver resolver = getContentResolver();
acTextField = (AutoCompleteTextView) findViewById(R.id.autoComplete);
Uri contacts = Uri.parse("content://icc/adn");
Cursor cursor = resolver.query(contacts, null, null, null, null);
if (cursor.moveToFirst()) {
String contactName;
String phoneNumber;
int nameColumn = cursor.getColumnIndex("name");
int phoneColumn = cursor.getColumnIndex("number");
do {
contactName = cursor.getString(nameColumn);
phoneNumber = cursor.getString(phoneColumn);
if((contactName != " " || contactName != null) && (phoneNumber!= " " ||phoneNumber!= null))
{
SimpleContact sc = new SimpleContact(contactName, phoneNumber);
contactList.add(sc);
}
} while (cursor.moveToNext());
}
adapter = new ContactAdapter(this, R.layout.grid_item, contactList);
acTextField.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
CustomAdapter.java
package com.sncrmck.adapter;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.sncrmck.autocompletetest.R;
import com.sncrmck.obj.SimpleContact.SimpleContact;
public class ContactAdapter extends ArrayAdapter<SimpleContact> {
private ArrayList<SimpleContact> entries;
private Activity activity;
public ContactAdapter(Activity a, int textViewResourceId, ArrayList<SimpleContact> entries){
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
Toast.makeText(getContext(), "Constructor", Toast.LENGTH_SHORT).show();
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Toast.makeText(getContext(), "Get View", Toast.LENGTH_SHORT).show();
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.big);
holder.item2 = (TextView) v.findViewById(R.id.small);;
}
else
holder=(ViewHolder)v.getTag();
final SimpleContact contactInfo = entries.get(position);
if (contactInfo != null) {
holder.item1.setText(contactInfo.getContactName());
holder.item2.setText(contactInfo.getContactNumber());
}
return v;
}
}
这是我传递给ContactAdapter的SimpleContact对象
package com.sncrmck.obj.SimpleContact;
public class SimpleContact {
private String contactName;
private String contactNumber;
public SimpleContact(String string, String string2) {
this.contactName = string;
this.contactNumber = string2;
}
public String getContactName() { return contactName; }
public void set(String contactName) { this.contactName= contactName; }
public String getContactNumber() { return contactNumber; }
public void setcustomSmall(String contactNumber) { this.contactNumber = contactNumber; }
}
最后,这是我的自定义列表项 - grid_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:textSize="24dp"
android:id="@+id/big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textSize="24dip"
android:id="@+id/small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:0)