从数据库中查看 ListView 中的项目
Posted
技术标签:
【中文标题】从数据库中查看 ListView 中的项目【英文标题】:Viewing Items in a ListView from Database 【发布时间】:2015-12-28 15:37:35 【问题描述】:我正在 android Studio 中开发一个 android 应用程序。在这个函数中,我想将 ListView 中的项目保存到数据库中,并在另一个 java 页面上的 listView 中查看它。现在可以将arraylist中的元素添加到数据库部分,因为logcat正在工作。现在我想在另一个页面上查看它。我已经为此实现了一个代码。但是当我单击查看按钮时,它没有给出任何响应。下面我发布了 DBAdapter 类和 java 类的代码。谢谢:)
viewHistory.java
public class viewHistoryScr extends Activity implements View.OnClickListener
private Button viewHist;
private Button back;
private ListView historyView;
DBUserAdapter dbUserAdapter = new DBUserAdapter(viewHistoryScr.this);
String username = DBUserAdapter.KEY_USERNAME;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.historyitmscr);
viewHist = (Button) findViewById(R.id.viewHistory);
back = (Button) findViewById(R.id.goBack);
historyView = (ListView) findViewById(R.id.historyList);
viewHist.setOnClickListener(this);
back.setOnClickListener(this);
private void viewHistoryItm()
Cursor cursor = dbUserAdapter.viewItm(username);
List<String> values = new ArrayList<String>();
if (cursor != null && cursor.moveToFirst())
do
String itmName = cursor.getString(Integer.parseInt(DBUserAdapter.KEY_ITMNAME));
//String[]values = new String[]itmName;
values.add(itmName);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.historyitmscr, R.id.historyList, values);
historyView.setAdapter(adapter);
cursor.close();
while (cursor.moveToNext());
@Override
public void onClick(View view)
if (view.getId() == R.id.addItems)
try
dbUserAdapter.open();
viewHistoryItm();
dbUserAdapter.close();
catch (SQLException e)
e.printStackTrace();
else if (view.getId() == R.id.goBack)
Intent i = new Intent(viewHistoryScr.this, MainActivity.class);
startActivity(i);
DBUserAdapter.java
public class DBUserAdapter
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_PASSHINT = "passHint";
public static final String KEY_ITMNAME = "itmName";
//public static final String KEY_ITMNAME = ""
public static final String TAG = "DBAdapter";
String userN = KEY_USERNAME;
String manualUser = KEY_USERNAME;
public static final String DATABASE_NAME = "usersdb";
public static final String DATABASE_TABLE = "userInfo";
public static final String ITM_DATABASE_NAME = "manualItm";
public static final int DATABASE_VERSION = 320;
public static final String DATABASE_CREATE = "CREATE TABLE "+DATABASE_TABLE+"(username TEXT NOT NULL, password TEXT NOT NULL, passHint TEXT NOT NULL);";
public static final String ITM_DATABASE_CREATE = "CREATE TABLE "+ITM_DATABASE_NAME+"(username TEXT NOT NULL, itmName TEXT NOT NULL)";
private Context context = null;
private DatabaseHelper dbHelper;
public SQLiteDatabase db;
public DBUserAdapter(Context context)
this.context = context;
dbHelper = new DatabaseHelper(context);
private static class DatabaseHelper extends SQLiteOpenHelper
DatabaseHelper(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL(DATABASE_CREATE);
db.execSQL(ITM_DATABASE_CREATE);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
Log.w(TAG, "Upgrading Database from Version "+oldVersion+" to "+newVersion+", Which will Destroy all old Data");
db.execSQL("DROP TABLE IF EXISTS userInfo");
db.execSQL("DROP TABLE IF EXISTS manualItm");
onCreate(db);
public void open() throws SQLException
db = dbHelper.getWritableDatabase();
public void close()
db.close();
public SQLiteDatabase getDatabaseInstance()
return db;
public boolean AddUser(String username, String password, String passHint)
try
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, username);
initialValues.put(KEY_PASSWORD, password);
initialValues.put(KEY_PASSHINT, passHint);
db.insert(DATABASE_TABLE, null, initialValues);
db.close();
return true;
catch (Exception e)
e.printStackTrace();
return false;
public boolean Login(String username, String password) throws SQLException
Cursor cursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]username, password);
if(cursor != null)
if (cursor.getCount() > 0)
return true;
return false;
public boolean register(String username, String password, String passHint)throws SQLException
Cursor cursor = db.rawQuery("INSERT INTO " + DATABASE_TABLE + " VALUES('?', '?', '?', '?');", new String[]username, password, passHint);
if(cursor != null)
if(cursor.getCount() > 0)
return true;
return false;
public void insertItm(ArrayList<String>itemsList)
try
db = dbHelper.getWritableDatabase();
for (int a = 0; a < itemsList.size(); a++)
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ITMNAME, itemsList.get(a));
contentValues.put(KEY_USERNAME, manualUser);
db.insert(ITM_DATABASE_NAME, null, contentValues);
db.close();
catch (Exception ex)
Log.e("Error in Adding Items", ex.toString());
//public boolean viewItm(String userN)
//String WHERE = KEY_USERNAME + "=" + username;
// Cursor cursor = db.rawQuery("SELECT * FROM " + ITM_DATABASE_NAME + " WHERE username=?", new String[]userN);
// if(cursor != null)
// cursor.moveToFirst();
// return true;
//
// return false;
//
public Cursor viewItm(String userN)
Cursor cursor = db.rawQuery("SELECT * FROM " + ITM_DATABASE_NAME + " WHERE username=?", new String[]userN);
if (cursor != null)
cursor.moveToFirst();
return cursor;
【问题讨论】:
【参考方案1】:设置Adpater
Outside 和Cursor.close()
while 循环,或者每次它都会创建新的适配器并关闭光标,这样下次它就会返回null,就像
private void viewHistoryItm()
Cursor cursor = dbUserAdapter.viewItm(username);
List<String> values = new ArrayList<String>();
if (cursor != null && cursor.moveToFirst())
do
String itmName = cursor.getString(Integer.parseInt(DBUserAdapter.KEY_ITMNAME));
//String[]values = new String[]itmName;
values.add(itmName);
while (cursor.moveToNext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.historyitmscr, R.id.historyList, values);
historyView.setAdapter(adapter);
cursor.close();
【讨论】:
以上是关于从数据库中查看 ListView 中的项目的主要内容,如果未能解决你的问题,请参考以下文章
Nativescript Vue:从ListView中的itemTap获取数组中的项目