- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在遵循了几个没有成功的例子之后,我发现自己在这个问题上陷入了死胡同。我遇到的问题是如何用数据库中的数据填充 ListView
。
The purpose is to create a screen with the listView which displays only the name column from the database, then when the item is selected the application goes to a new screen where it displays the other data related to the item.
我想帮助理解的事情是用我的数据库表“Contacts”中的“Name”列中的数据填充 ListView
,然后从基于所选项目的数据库。
这是 ContactsMenu 类的代码,其中将显示姓名。
package com.emailandcontactmanager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class ContactsMenu extends Activity {
ListView listView;
public static final String fields[] = { DatabaseSetup.colName};
Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.managecontacts);
listView = (ListView) findViewById(R.id.lstContacts);
DatabaseSetup.init(this);
Button btnAddItem = (Button) findViewById(R.id.btnAddContact);
btnAddItem.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent addItem = new Intent(v.getContext(), AddContact.class);
startActivity(addItem);
}
});
}
@Override
protected void onPause() {
listView.setAdapter(null);
cursor.close();
DatabaseSetup.deactivate();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
DatabaseSetup.init(this);
cursor = DatabaseSetup.getContactNames();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, fields, new int[] {R.id.item_text});
listView.setAdapter(adapter);
}
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
//Nothing happens on No button click, and the menu closes
break;
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu mainmenu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, mainmenu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final CharSequence[] items = {"Contacts list", "Add Contact"};
switch (item.getItemId()) {
case R.id.help: AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a function to revice information about it.");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int selected) {
switch(selected){
case 0:
Toast.makeText(getApplicationContext(),
"Allows you to view the selected item and make editations to it or delete it.",
Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getApplicationContext(),
"Allows you to add a new contact by bringing up a screen where the nececary information can be entered.",
Toast.LENGTH_LONG).show();
}
}
});
builder.show();
break;
case R.id.back: AlertDialog.Builder builderBack = new AlertDialog.Builder(this);
builderBack.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
break;
}
return true;
}
}
数据库设置类
package com.emailandcontactmanager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/*
* usage:
* DatabaseSetup.init(egActivityOrContext);
* DatabaseSetup.createEntry() or DatabaseSetup.getContactNames() or DatabaseSetup.getDb()
* DatabaseSetup.deactivate() then job done
*/
class DatabaseSetup extends SQLiteOpenHelper {
static DatabaseSetup instance = null;
static SQLiteDatabase db = null;
public static void init(Context context) {
if (null == instance) {
instance = new DatabaseSetup(context);
}
}
public static SQLiteDatabase getDb() {
if (null == db) {
db = instance.getWritableDatabase();
}
return db;
}
public static void deactivate() {
if (null != db && db.isOpen()) {
db.close();
}
db = null;
instance = null;
}
public static long createEntry(String name, String mail, String phone1,
String phone2, String address, String notes) {
ContentValues cv = new ContentValues();
cv.put(colName, name);
cv.put(colMail, mail);
cv.put(colPhone1, phone1);
cv.put(colPhone2, phone2);
cv.put(colAddress, address);
cv.put(colNotes, notes);
return getDb().insert(contactsTable, null, cv);
}
public static Cursor getContactNames() {
// TODO Auto-generated method stub
String[] columns = new String[] {"_id", colName };
return getDb().query(contactsTable, columns, null, null, null, null,
null);
}
DatabaseSetup(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS " + contactsTable
+ " (_id integer primary key autoincrement, " + colName
+ " TEXT NOT NULL, " + colMail + " TEXT NOT NULL, " + colPhone1
+ " TEXT NOT NULL, " + colPhone2 + " TEXT NOT NULL, "
+ colAddress + " TEXT NOT NULL, " + colNotes
+ " TEXT NOT NULL)");
db.execSQL("CREATE TABLE IF NOT EXISTS " + templatesTable
+ " (_id integer primary key autoincrement, " + colSubject
+ " TEXT NOT NULL, " + colBody + " TEXT NOT NULL)");
db.execSQL("CREATE TABLE IF NOT EXISTS " + tagsTable
+ " (_id integer primary key autoincrement, " + colTagName
+ " TEXT NOT NULL, " + colContact + " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + contactsTable);
db.execSQL("DROP TABLE IF EXISTS " + templatesTable);
db.execSQL("DROP TABLE IF EXISTS " + tagsTable);
onCreate(db);
}
static final String dbName = "DB";
static final int dbVersion = 1;
static final String contactsTable = "Contacts";
static final String colName = "Name";
static final String colMail = "Email";
static final String colPhone1 = "Phone1";
static final String colPhone2 = "Phone2";
static final String colAddress = "Address";
static final String colNotes = "Notes";
static final String templatesTable = "Templates";
static final String colSubject = "Subject";
static final String colBody = "Body";
static final String tagsTable = "Tags";
static final String colTagName = "Name";
static final String colContact = "Contact";
}
添加到联系人菜单的类的代码。当我有一个临时显示器时它曾经工作,但是现在它不工作了。
package com.emailandcontactmanager;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddContact extends Activity
{
Button saveContact;
EditText contactName, contactMail, contactPhone1, contactPhone2, contactAddress, contactNotes;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newcontact);
contactName = (EditText) findViewById(R.id.txtName);
contactMail = (EditText) findViewById(R.id.txtMail);
contactPhone1 = (EditText) findViewById(R.id.txtPhone1);
contactPhone2 = (EditText) findViewById(R.id.txtPhone2);
contactAddress = (EditText) findViewById(R.id.txtAddress);
contactNotes = (EditText) findViewById(R.id.txtNotes);
// saveContact = (Button) findViewById(R.id.btnSaveContact);
Button btnSaveContact = (Button) findViewById(R.id.btnSaveContact);
btnSaveContact.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
boolean working = true;
try{
String name = contactName.getText().toString();
String mail = contactMail.getText().toString();
String phone1 = contactPhone1.getText().toString();
String phone2 = contactPhone2.getText().toString();
String address = contactAddress.getText().toString();
String notes = contactNotes.getText().toString();
DatabaseSetup entry = new DatabaseSetup(AddContact.this);
DatabaseSetup.getDb();
DatabaseSetup.createEntry(name, mail, phone1, phone2, address, notes);
entry.close();
}
catch(Exception e)
{
working = false;
String error = e.toString();
Dialog d = new Dialog(AddContact.this);
d.setTitle("Error");
TextView tv = new TextView(AddContact.this);
tv.setText(error);
}
finally
{
if(working)
{
Dialog d = new Dialog(AddContact.this);
d.setTitle("Success");
TextView tv = new TextView(AddContact.this);
tv.setText("The database changes have succeeded.");
d.setContentView(tv);
d.show();
}
}
}
});
}
}
最佳答案
这是 Activity 示例代码:
public class MainActivity extends Activity {
ListView listView;
public static final String fields[] = { DatabaseSetup.colName };
Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.managecontacts);
listView = (ListView) findViewById(R.id.lstContacts);
DatabaseSetup.init(this);
for (Integer i = 0; i < 120; ++i) {
DatabaseSetup.createEntry("Name " + i, "Mail " + i, "Phone1 " + i,
"Phone2 " + i, "Address " + i, "Notes " + i);
}
}
@Override
protected void onPause() {
listView.setAdapter(null);
cursor.close();
DatabaseSetup.deactivate();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
DatabaseSetup.init(this);
cursor = DatabaseSetup.getContactNames();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, fields, new int[] { R.id.item_text });
listView.setAdapter(adapter);
}
}
这是 DatabaseSetup 示例代码:
/*
* usage:
* DatabaseSetup.init(egActivityOrContext);
* DatabaseSetup.createEntry() or DatabaseSetup.getContactNames() or DatabaseSetup.getDb()
* DatabaseSetup.deactivate() then job done
*/
class DatabaseSetup extends SQLiteOpenHelper {
static DatabaseSetup instance = null;
static SQLiteDatabase db = null;
public static void init(Context context) {
if (null == instance) {
instance = new DatabaseSetup(context);
}
}
public static SQLiteDatabase getDb() {
if (null == db) {
db = instance.getWritableDatabase();
}
return db;
}
public static void deactivate() {
if (null != db && db.isOpen()) {
db.close();
}
db = null;
instance = null;
}
public static long createEntry(String name, String mail, String phone1,
String phone2, String address, String notes) {
ContentValues cv = new ContentValues();
cv.put(colName, name);
cv.put(colMail, mail);
cv.put(colPhone1, phone1);
cv.put(colPhone2, phone2);
cv.put(colAddress, address);
cv.put(colNotes, notes);
return getDb().insert(contactsTable, null, cv);
}
public static Cursor getContactNames() {
// TODO Auto-generated method stub
String[] columns = new String[] { "_id", colName };
return getDb().query(contactsTable, columns, null, null, null, null,
null);
}
private DatabaseSetup(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS " + contactsTable
+ " (_id integer primary key autoincrement, " + colName
+ " TEXT NOT NULL, " + colMail + " TEXT NOT NULL, " + colPhone1
+ " TEXT NOT NULL, " + colPhone2 + " TEXT NOT NULL, "
+ colAddress + " TEXT NOT NULL, " + colNotes
+ " TEXT NOT NULL)");
db.execSQL("CREATE TABLE IF NOT EXISTS " + templatesTable
+ " (_id integer primary key autoincrement, " + colSubject
+ " TEXT NOT NULL, " + colBody + " TEXT NOT NULL)");
db.execSQL("CREATE TABLE IF NOT EXISTS " + tagsTable
+ " (_id integer primary key autoincrement, " + colTagName
+ " TEXT NOT NULL, " + colContact + " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + contactsTable);
db.execSQL("DROP TABLE IF EXISTS " + templatesTable);
db.execSQL("DROP TABLE IF EXISTS " + tagsTable);
onCreate(db);
}
static final String dbName = "DB";
static final int dbVersion = 1;
static final String contactsTable = "Contacts";
static final String colName = "Name";
static final String colMail = "Email";
static final String colPhone1 = "Phone1";
static final String colPhone2 = "Phone2";
static final String colAddress = "Address";
static final String colNotes = "Notes";
static final String templatesTable = "Templates";
static final String colSubject = "Subject";
static final String colBody = "Body";
static final String tagsTable = "Tags";
static final String colTagName = "Name";
static final String colContact = "Contact";
}
这里是'res/layout/row.xml'
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
希望对您有所帮助。
关于android - 安卓 : Fill ListView with database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12566763/
我之前在论坛上发布过这个,但我想我不太清楚。我有一个侧边栏 ListView 和一个包含我的控件的主面板。我想在 oncreate() 中突出显示 listview 中的 Activity 项,因为每
这里有没有人知道我如何通过 onTap() 扩展 ListView 项(在本例中为卡片)的内容 - 以显示更多选项?您知道那里有可以提供帮助的酒吧吗? 我已经看过了,但我不知道要搜索什么?我相信你们都
我的 ListView 控件有问题。我希望我的奇数行和偶数行具有不同的颜色,但我想通过代码而不是 FXML 来实现。例如: 第一行 - 绿色 第二行 - 红色 第三行 - 绿色 第四行 - 红色 现在
我有一个 ListView 。我想让单元格背景透明。目前,我正在做以下事情: .list-cell { -fx-background-color: transparent; } 但是,细胞的颜
我想创建一个几乎无限的元素列表,但我想将列表的初始位置设置为某个特定元素。像这张图片: 其中索引 0 将是初始位置,并且此列表可能会也可能不会在两个方向上延伸很长。 我可以像这样创建我的元素: W
有没有办法在JavaFX中获取ListView的可见项?我想确定 JavaFX 应用程序中 ListView 显示的第一个可见项。 以下代码found here不适合我(仅适用于 TableView)
开发人员。 我尝试在水平方向拉伸(stretch) ListView 项目。我确实重置了 ItemContainerStyle ,并且水平对齐是拉伸(stretch)的。这是 MainPage.xam
有没有办法在JavaFX中获取ListView的可见项?我想确定 JavaFX 应用程序中 ListView 显示的第一个可见项。 以下代码found here不适合我(仅适用于 TableView)
我想问一下: 我有一个预定义顺序的数组。 var order=new Array(); order[0]="Tesco"; order[1]="Interspar"; order[2]="
我希望创建以下内容:当到达内部 Listview 的顶部或底部时,我想继续在顶部 Listview 中滚动。见动图: Gif of what I got so far 一个选项是在到达底部时将内部 L
我正在尝试在 ajax 发布后刷新 jQuery 移动 ListView ,我一直在尝试使用 .trigger("create") 来执行此操作,如下所示: Most Played
我真的不明白是什么导致了错误我检查了文档,这里有一个非常相似的例子是我的 views.py,我使用的应用程序下的 urls.py 包含,以及模板 View .py class SchoolListVi
我有这个父布局 parent_listview 的列表项具有这种布局 child_listview 的项目有这个布局
我在单击列表项时获取 listview 项 时遇到问题。我得到了 simple listview(Arrayadapter) 的 listview item,但我遇到了 custom listview
我有一个工作正常的 DropdownMenu,但我需要一个 ListView,但我无法转换它。 DropdownMenu 如下所示: // Create the List of devices t
我想实现一个可滚动(垂直)且其中包含元素的屏幕。所以我把它放在 listview.builder 上。问题是,其中一个元素是另一个水平滚动的 listview.builder。当我实现它时,horiz
帮助!我想不通! 为了帮助我学习 SwiftUI,我正在使用 ListView 制作一个简单的杂货 list 应用程序。点击列表中的每个项目时,有些按钮会变成绿色或红色。 在我向列表数组中添加太多项目
所以我知道我们可以等待 ListView 加载,然后显示它。但是,如果我们动态加载正在下载的图像,ListView 将加载但图像尚未加载,因此它们不会出现在 ListView 中。 可接受的等待 Li
我是 Android 的新手,正在努力提高编程技能。 在这里,我有自定义 ListView 适配器类,我曾在其中显示 ListView 项目,如 TextView 、图像等。 我需要做的是,我在 Li
So, what I want is to individually disable a button for the concerned item in the ListView when clic
我是一名优秀的程序员,十分优秀!