jeudi 29 octobre 2015

retrieve value from sqlite database to listview using cursor in android

I'm new in android and tomorrow is my final submission to my android project... here is my java code

SimpleListActivity.java

public class SimpleListActivity extends ActionBarActivity{
private ArrayList<HashMap<String,Object>> names = new     
ArrayList<HashMap<String, Object>>();
private ListView myList;
public static final String NAME = "name";
public static final String MNAME = "mname";
public static final String LNAME = "lname";
public static final String COURSE = "course";
public static final String ROLLNUM = "rollnum";
public static final String ID = "_index";
public static final String ISEDIT = "is_edit";
public static final int REG_REQ_CODE = 55;
public static final int REG_RESULT_CODE = 56;
public static final int ADD = 110;
public static final int EDIT = 111;
public static final int DELETE = 112;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_simple_list);
    myList = (ListView)findViewById(R.id.mylist);
    getAllFromDB();
    registerForContextMenu(myList);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
}
private void getAllFromDB(){
    names = new ArrayList<HashMap<String, Object>>();
     MyDB myDB = new MyDB(this);
   myDB.openDB();
     Cursor cursor = myDB.getAllUsers();
    cursor.moveToFirst();
  for(int i = 0; i < cursor.getCount(); i++){
       HashMap<String,Object> hm = new HashMap<String,Object>();
        hm.put(NAME,cursor.getString(cursor.getColumnIndex(MyDB.FIRSTNAME)));
       hm.put(MNAME,cursor.getString(cursor.getColumnIndex(MyDB.MIDDLENAME)));
        hm.put(NAME,cursor.getString(cursor.getColumnIndex(MyDB.LASTNAME)));
        hm.put(NAME,cursor.getString(cursor.getColumnIndex(MyDB.COURSE)));
        hm.put(ROLLNUM,cursor.getInt(cursor.getColumnIndex(MyDB.STUDENT_ROLL)));
        hm.put(ID,cursor.getLong(cursor.getColumnIndex(MyDB.STUDENT_ID)));
        names.add(hm);
        cursor.moveToNext();
  }
    cursor.close();
    myDB.closeDB();
    SimpleAdapter adapter = new SimpleAdapter(this,
            names,
            R.layout.simplelistbox,
            new String[]{NAME,MNAME,LNAME,COURSE,ROLLNUM},
            new int[]{R.id.firstname, R.id.middlename,R.id.lastname,R.id.course,R.id.rollnumholder});
    myList.setAdapter(adapter); 
}

and myDB.java

public class MyDB {
public static final String DB_NAME = "mydb.db";
public static final String TABLE_NAME = "students_tbl";
public static final String FIRSTNAME = "firstname";
public static final String MIDDLENAME = "middlename";
public static final String LASTNAME = "lastname";
public static final String COURSE = "course";
public static final String STUDENT_ROLL = "student_roll_num";
public static final String STUDENT_ID = "_id";
//public static final String CREATE_TBL = "CREATE TABLE students_tbl (_id  
INTEGER PRIMARY KEY AUTOINCREMENT, student_name TEXT, student_roll_num 
 INTEGER)";
public static final String CREATE_TBL = "CREATE TABLE "+TABLE_NAME
                                        +" ("+STUDENT_ID+" INTEGER 
  PRIMARY KEY AUTOINCREMENT, "
                                        +FIRSTNAME+" TEXT, "
                                        +MIDDLENAME+"TEXT, "
                                        +LASTNAME+"TEXT, "
                                        +COURSE+"TEXT, "
                                        +STUDENT_ROLL+" INTEGER)";
public static final String DROP_TBL = "DROP TABLE IF EXISTS "+TABLE_NAME;
public static final int VER = 1;
private SQLiteDatabase db;
private MyDBOpenHelper myDBOpenHelper;
public MyDB(Context context){
    myDBOpenHelper = new MyDBOpenHelper(context);
}
public void openDB() throws SQLiteException{
    db = myDBOpenHelper.getWritableDatabase();
}
public void closeDB(){
    db.close();
}
public long addUser(String fname, String mname, String lname, String 
course,  int rollNum){
    ContentValues contentValues = new ContentValues();
    contentValues.put(FIRSTNAME,fname);
    contentValues.put(MIDDLENAME, mname);
    contentValues.put(LASTNAME, lname);
    contentValues.put(COURSE, course);
    contentValues.put(STUDENT_ROLL,rollNum);
    return db.insert(TABLE_NAME,null,contentValues);
}
public int updateUserById(long studId,String fname, String mname, 
String lname, String course,int rollNum){
    //UPDATE SET
    ContentValues contentValues = new ContentValues();
    contentValues.put(FIRSTNAME,fname);
    contentValues.put(MIDDLENAME, mname);
    contentValues.put(LASTNAME, lname);
    contentValues.put(COURSE, course);
    contentValues.put(STUDENT_ROLL,rollNum);
    return db.update(TABLE_NAME,contentValues,STUDENT_ID+ "= ?",
  new String[]{Long.toString(studId)});
}
public Cursor getAllUsers(){
    //SELECT * FROM students_tbl;
    Cursor cursor = db.query(TABLE_NAME,new String[]   
{STUDENT_ID,FIRSTNAME,STUDENT_ROLL},null,null,null,null,null);
    cursor.moveToFirst();
    return cursor;
}
public Cursor getUserById(long stuId){
    //SELECT * FROM students_tbl WHERE _id = ?
    Cursor cursor = db.query(TABLE_NAME,new String[]
  {STUDENT_ID,FIRSTNAME,STUDENT_ROLL},STUDENT_ID+" = ?",new String[]
  {Long.toString(stuId)},null,null,null);
    cursor.moveToFirst();
    return cursor;
  }
public int deleteUser(long stuId){
    return db.delete(TABLE_NAME,STUDENT_ID+" = ?",new String[]
 {Long.toString(stuId)});
}
private class MyDBOpenHelper extends SQLiteOpenHelper{
    public MyDBOpenHelper(Context context) {
        super(context, DB_NAME, null, VER);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TBL);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int   
    newVersion) {
        if (newVersion > oldVersion){
            db.execSQL(DROP_TBL);
            onCreate(db);
        }
    }
}
}

i want to add 4 column from database like firstname,middlename, lastname and course but when im trying to add some column, i cannot display all of data from database. pls help...

this is not my own code. pls click this link for the full code so that you can trace the problem... i want to display the 4 columns in activitysimplelist.xml. thanks...

i referring this link... pls visit this website... http://ift.tt/1jTWs0Uadd-edit_-delete-_retrive

Aucun commentaire:

Enregistrer un commentaire