我查看了这个教程: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
现在我想在Listview中加载这些数据。 这是我的尝试:
private ArrayAdapter<Contact> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("Ravi", "9100000000"));
db.addContact(new Contact("Srinivas", "9199999999"));
db.addContact(new Contact("Tommy", "9522222222"));
db.addContact(new Contact("Karthik", "9533333333"));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: " + cn.getID() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to log
Log.d("Name: ", log);
}
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
adapter = new ArrayAdapter<Contact>(getApplicationContext(), R.layout.list_view, contacts);
ListView listView = (ListView) findViewById(R.id.list);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
所以来自联系人的Class你可以在androidhive的url中看到。
答案 0 :(得分:1)
那就是你对ArrayAdapter的提供联系,但它需要String来填充值
或使用CustomAdapter弹出有关列表中联系人的所有Vau
1.为单个项目创建布局,在您的情况下,在布局文件夹中创建list_item.xml
<强> list_item.xml 强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:padding="6dip" >
<TextView
android:id="@+id/contactname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
<TextView
android:id="@+id/contactphone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
</RelativeLayout>
2.创建扩展ArrayAdapter的CustomAdapter
<强> CustomAdapter.java 强>
public class CustomAdapter extends ArrayAdapter<Contacts> {
private final Context context;
private ArrayList<Contacts> contacts;
public CustomAdapter(Context context,ArrayList<Contacts> contacts) {
super(context, R.layout.list_item, values);
this.context = context;
this.contacts= contacts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView name= (TextView) rowView.findViewById(R.id.contactname);
TextView mobile= (TextView) rowView.findViewById(R.id.contactphone);
name.setText(contacts.get(position).getName());
mobile.setText(contacts.get(position).getPhoneNumber());
return rowView;
}
}
3.从活动中呼叫Adpater
CustomAdapter adapter = new CustomAdapter (getApplicationContext(),contacts);
ListView listView = (ListView) findViewById(R.id.list);
// Assign adapter to ListView
listView.setAdapter(adapter);
答案 1 :(得分:0)
你可以试试这个:
第一步 通过循环逐个读取数据库中的所有联系人:
protected void onDestroy() {
super.onDestroy();
}
//reading all the contacts from database
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + <Table-Name>;
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.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
第二步
现在你可以在onCreate中调用这个getAllContacts()并解压缩所有联系人并保存在Adapter中,然后在你的ListView中显示。
希望它有所帮助。 :)