我创建了一个由两个文本视图和一个复选框组成的列表视图,并且正在打印联系人。但是,我无法使用复选框将它们返回到我的程序并将它们添加到我的Person类。
public void populateListView(View view) {
try {
Cursor phones = getContentResolver().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));
list.add(new Person(name, phoneNumber));
}
phones.close();
}
catch (Exception e){
e.printStackTrace();
}
ArrayAdapter<Person> adapter = new myListAdapter();
ListView listview2 = (ListView) findViewById(R.id.contactlistview);
listview2.setAdapter(adapter);
}
public class myListAdapter extends ArrayAdapter<Person> {
public myListAdapter() {
super(MainActivity.this, R.layout.da_item, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.da_item, parent, false);
}
// Find person wot work with
Person currentperson = list.get(position);
// Fill the view
TextView nameboxview = (TextView)itemView.findViewById(R.id.NameView);
nameboxview.setText(currentperson.getName());
TextView numberboxview = (TextView)itemView.findViewById(R.id.NumberView);
numberboxview.setText(currentperson.getPhone());
return itemView;
}
}
public void registerDoneClick(View view) {
View itemView = view;
ListView lister = (ListView) findViewById(R.id.contactlistview);
long[] id = lister.getCheckedItemIds();
TextView nameboxview = (TextView)itemView.findViewById(R.id.NameView);
TextView numberboxview = (TextView)itemView.findViewById(R.id.NumberView);
for(int i = 0; i < id.length; i++) {
Person human = new Person(nameboxview.toString(), numberboxview.toString());
persons.add(human);
BetterDisplay(human);
}
setContentView(R.layout.activity_main);
}
public void grabthecontacts(View view) {
setContentView(R.layout.selectcontacts);
populateListView(view);
}
我找到了一个关于与我正在做的事情非常类似的旧线程,但是他似乎使用了一种不同的方法来发布结果,所以我完全感到困惑,因为它看起来与我写的完全不同。 我是Java和Android开发的新手。我想我可能完全没有关于返回值和格式,任何帮助将不胜感激。