我正在Android Studio中开发一个Android应用程序。在这个函数中,我想将ListView中的项目保存到数据库中,并在另一个java页面上的listView中查看它。将arraylist中的元素添加到数据库部分ok,现在作为logcat工作。现在我想在另一页上查看它。我为此实现了一个代码。但是,当我点击查看按钮时,它没有给出任何回复。下面我发布了DBAdapter类和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;
}
}
答案 0 :(得分:0)
设置Adpater
外部和Cursor.close()
while循环或每次创建新适配器并关闭Cursor,以便下次返回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();
}
}