我浏览了一些Base适配器tuts并做了这个,我不知道如何将联系人设置到列表中,任何人都可以解释我错过的内容
以下是代码:
class SingleRow{
String name;
String number;
int image;
SingleRow(String name,String number){
this.name = name;
this.number = number;
}
}
我使用SingleRow作为内部类,这是我的自定义addapter
public class PranzAdapter extends BaseAdapter {
ArrayList<SingleRow> list;
Context c;
PranzAdapter(Context context){
c = context;
list = new ArrayList<SingleRow>();
String[] names = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
String[] number= new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
for (int position=0; position<names.length; position++)
{
list.add(new SingleRow(names[position],number[position])) ;
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row,null,false);
TextView name= (TextView) row.findViewById(R.id.textView1);
TextView number = (TextView) row.findViewById(R.id.textView2);
SingleRow temp = list.get(position);
name.setText(temp.name);
number.setText(temp.number);
return row;
}
}
主要活动
public class MainActivity extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
list.setAdapter(new PranzAdapter(this));
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
}
}