从数据库中填充ListView
在主Activity中,我定义了按钮并将它们放在onclicklistener上。问题是在viewAll按钮时,我想从数据库填充列表视图,以便我调用该方法db.getAllContacts(),但该代码不起作用从数据库中填充ListView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.editText1);
phone = (EditText)findViewById(R.id.editText2);
btnViewAll=(Button)findViewById(R.id.button1);
btnAdd=(Button)findViewById(R.id.button2);
btnAdd.setOnClickListener(this);
btnViewAll.setOnClickListener(this);
}
@Override
public void onClick(View view) {
MySQLiteHelper db = new MySQLiteHelper(this);
if(view==btnViewAll) {
if(name.getText().toString().trim().length()==0 || phone.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
else{
List<Contact> contact = new ArrayList<Contact>();
contact= db.getAllContacts();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contact);
ListView listContent = (ListView)findViewById(R.id.List);
listContent.setAdapter(adapter);
}
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
name.setText("");
phone.setText("");
}
在SQLiteHelper文件:
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM contact";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhone(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}// return contact list
return contactList;}
请帮我
数据库访问对象的逻辑很好。主要问题在于阵列适配器。你应该尝试在你的活动来实现,而不是ListAdapter的使用ArrayAdapter
您能帮助我如何做到这一点?我是一个初学者 – user3552658 2014-12-07 10:54:37
更改listContent.setAdapter(适配器);到listContent.setAdapter(this); IDE应提示您在活动中实现ListAdapter。 然后根据文档 – 2014-12-07 11:01:30
删除此行: -
ArrayAdapter适配器=新ArrayAdapter(这一点,android.R.layout.simple_list_item_1,接触);
并添加此: -
ContactListAdapter contactListAdapter =新ContactListAdapter(此,R.layout.contact_list_item,接触);
ContactListAdapter类: -
public class ContactListAdapter extends ArrayAdapter<Contact> {
// Declare Variables
Context context;
LayoutInflater inflater;
List<Contact> contactlist;
public ContactListAdapter(Context context, int resourceId,
List<Contact> contactlist) {
super(context, resourceId, contactlist);
this.context = context;
this.contactlist = contactlist;
inflater = LayoutInflater.from(context);
}
private class ViewHolder {
TextView contactId;
TextView contactName;
TextView contactPhone;
}
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.contact_list_item, null);
// Locate the TextViews in listview_item.xml
holder.contactName = (TextView) view.findViewById(R.id.contact_id);
holder.contactName = (TextView) view.findViewById(R.id.contact_name);
holder.contactPhone = (TextView) view.findViewById(R.id.contact_phone);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews
holder.contactId.setText(contactlist.get(position).getContactId());
holder.contactName.setText(contactlist.get(position).getContactName());
holder.contactPhone.setText(contactlist.get(position).getContactPhone());
return view;
}
}
和contact_list_item.xml: -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/contact_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="test"/>
<TextView
android:id="@+id/contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="test"/>
<TextView
android:id="@+id/contact_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
执行该方法谢谢..但是,此代码给我的错误: ContactListAdapter contactListAdapter = new ContactListAdapter(this,R.layout.contact_list_item,contact); 它说构造函数没有定义 – user3552658 2014-12-08 18:37:41
检查你传递给构造函数的3个参数。这些参数都是返回所需的值。我在ContactListAdapter类中定义了它 – 2014-12-09 11:16:14
使用一个CursorAdapter来代替。 – Nazgul 2014-12-07 09:55:36
您应该为您的联系人列表使用customAdapter – 2014-12-07 10:13:27
并检查getAllContacts()方法的日志,以便它从数据库中返回联系人列表 – 2014-12-07 10:23:08