Hae stackoverflow大师,
我正在寻求帮助。我已经在互联网上进行了研究但是徒劳无功。我想将所有提取的值添加到listview。我已设法将值插入数据库并设法获取所有这些值,但我无法将它们添加到listview。 以下功能完美无缺,并将数据作为列表返回。
}
private void showRecords() {
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
//lv.setAdapter(new dataAdapter(this, cn.getID(), cn.getName(), cn.getPhoneNumber()));
String log = "Id: " + cn.getID() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to log
//Log.d("Name: ", log);
Toast.makeText(getApplicationContext(),log, Toast.LENGTH_SHORT).show();
}
}
此外,下面的功能非常完美,并使用日志打印出所有检索到的值
private void showRecords() {
List<Contact> contacts = db.getAllContacts();
for(int i=0; i < contacts.size();i++){
lv.setAdapter(new dataAdapter(this, cn.getID(),cn.getName(), cn.getPhoneNumber()));
}
}
现在,我创建了一个列表视图,但是我无法将数据添加到列表视图中,如图所示。
List<Contact> contacts = db.getAllContacts();
我的问题是,如何将public class dataAdapter extends BaseAdapter {
Context context;
String [] name;
String [] phone;
int [] dataId;
private static LayoutInflater inflater=null;
public dataAdapter(Context context, int[] dataId, String[] name, String[] phone){
this.context=context;
this.dataId=dataId;
this.name=name;
this.phone=phone;
inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return dataId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public class Holder{
TextView nameV;
TextView phoneV;
}
public View getView(final int position, View convertView, ViewGroup parent){
Holder holder=new Holder();
View rowView;
rowView=inflater.inflate(R.layout.thelist, null);
holder.nameV=(TextView) rowView.findViewById(R.id.txt1);
holder.phoneV=(TextView) rowView.findViewById(R.id.txt2);
holder.nameV.setText(name[position]);
holder.phoneV.setText(phone[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,name[position],Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
}
填充到listview lv?
这是整个dataAdapter类
{{1}}
答案 0 :(得分:1)
不要为每个新项目设置适配器,而是仅将联系人列表传递给列表视图适配器一次。如果要添加新项目,请添加到现有适配器。
你可以这样做
private void showRecords() {
List<Contact> contacts = db.getAllContacts();
lv.setAdapter(new dataAdapter(this, contacts));
}
或者您按照上面评论中提供的教程更好地理解。
修改1
您可以从List<Contact> contacts = db.getAllContacts()
.....
.....
List<Contact> mContacts;
DataAdapter(Context context,List<Contact> contact) {
this.context=context;
this.mContacts=contacts;
.....
.....
}
你的getCount方法应该是
public int getCount() {
return mContact.size();
}
你的getView()方法应该是
public View getView(final int position, View convertView, ViewGroup parent) {
Contact contact = mContact.get(position);
....
....
holder.nameV.setText(contact.getName());
holder.phoneV.setText(contact.getPhone());
....
....
return view;
}
如果你添加到这个现有的适配器,添加像这样的自定义方法
public void add(Contact contact) {
if(mContact != null) {
mContact.add(contact);
// This will refresh the list.
notifyDataSetChanged();
}
}
我仍然建议你仔细阅读上面链接中提到的教程,你的适配器类需要进行大量更改以获得更好的实现。