mardi 13 janvier 2015

how to save data using sqlite and retrieve the data from any device through admin dashboard

i want the user to store data on database and submit, the admin should be able to retrieve the same data on any device through admin dashboard using sqlite in android, how to do? i am new to android programming.



private SQLiteStatement insertStmt;

private static final String INSERT = "insert into " + TABLE_NAME
+ " (name,number,skypeId,address) values (?,?,?,?)";

public DataManipulator(Context context) {
DataManipulator.context = context;
OpenHelper openHelper = new OpenHelper(DataManipulator.context);
DataManipulator.db = openHelper.getWritableDatabase();
this.insertStmt = DataManipulator.db.compileStatement(INSERT);

}

public long insert(String name, String number, String skypeId,
String address) {
this.insertStmt.bindString(1, name);
this.insertStmt.bindString(2, number);
this.insertStmt.bindString(3, skypeId);
this.insertStmt.bindString(4, address);
return this.insertStmt.executeInsert();
}

public void deleteAll() {
db.delete(TABLE_NAME, null, null);
}

public List<String[]> selectAll() {

List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME, new String[] { "id", "name",
"number", "skypeId", "address" }, null, null, null, null,
"name asc");

int x = 0;
if (cursor.moveToFirst()) {
do {
String[] b1 = new String[] { cursor.getString(0),
cursor.getString(1), cursor.getString(2),
cursor.getString(3), cursor.getString(4) };

list.add(b1);

x = x + 1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();

return list;
}

public void delete(int rowId) {
db.delete(TABLE_NAME, null, null);
}

private static class OpenHelper extends SQLiteOpenHelper {

OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "
+ TABLE_NAME
+ " (id INTEGER PRIMARY KEY, name TEXT, number TEXT, skypeId TEXT, address TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}


} Checkdata.java



List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 = null;
String[] stg1;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
dm = new DataManipulator(this);
names2 = dm.selectAll();

stg1 = new String[names2.size()];

int x = 0;
String stg;

for (String[] name : names2) {
stg = name[1] + " - " + name[2] + " - " + name[3] + " - " + name[4];

stg1[x] = stg;
x++;
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stg1);
this.setListAdapter(adapter);
selection = (TextView) findViewById(R.id.selection);

}

public void onListItemClick(ListView parent, View v, int position, long id) {
selection.setText(stg1[position]);
}


}


Aucun commentaire:

Enregistrer un commentaire