mercredi 30 septembre 2015

Viewing Items in a ListView from Database

I'm developing an android application in Android Studio. In this function i wanted to save items in a ListView into a Database and View it in a listView on another java page. Adding the elements in arraylist into database part ok for now as the logcat its working. Now i wants to view it on another page. I've implemented a code for that. But when i clicked view button its not giving any response. Below i've posted the Code of DBAdapter class and java class. Thanks :)

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;
}
}

Aucun commentaire:

Enregistrer un commentaire