在主Activity中我定义了按钮并将它们放在onclicklistener上。当我想从数据库填充列表视图时,问题出现在viewAll按钮中我调用方法db.getAllContacts()但代码不起作用
@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;}
请帮帮我
答案 0 :(得分:1)
删除此行: -
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,contact);
并添加: -
ContactListAdapter contactListAdapter = new ContactListAdapter(this,R.layout.contact_list_item,contact);
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" />
答案 1 :(得分:0)
数据库访问对象逻辑很好。主要问题在于数组适配器。您应该尝试在Activity中实现ListAdapter,而不是使用ArrayAdapter