mardi 31 mars 2015

Retrieve Specific data from database android for each user

My question is that i want each user to get information that is relevent to him ie, in this local password protected program if user name matches when reading table ,i should only get his history(here latitude and longitude) any way to do the same??



package com.sset.jibin.wakemethere;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

com.sset.jibin.wakemethere.DatabaseOperations2.HISTORY_COLUMN_NAME;

public class History extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
DatabaseOperations2 databaseHelper;
String tex = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
Intent intent = getIntent();
String tex = intent.getStringExtra("text");

databaseHelper = new DatabaseOperations2(this);

Log.d("=====>", "valc0");

populatehistorylist();
}
private void populatehistorylist(){


Cursor cr = databaseHelper.getAllRecords();


startManagingCursor(cr);

String[] from = new String[]{databaseHelper.HISTORY_COLUMN_ID, databaseHelper.HISTORY_COLUMN_LAT, databaseHelper.HISTORY_COLUMN_LNG};

int[] to = new int[]{R.id.cid, R.id.lat, R.id.lng};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, cr, from, to);

ListView lv = (ListView) findViewById(android.R.id.list);

lv.setAdapter(adapter);

}










public void vmap(View view) {
Log.d("History","VMAP");

Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);

}

public void goToGoogleMaps(View view){
TextView textLng = (TextView)view.findViewById(R.id.lng);
String lng = (String) textLng.getText();
TextView textLat = (TextView)view.findViewById(R.id.lat);
String lat = (String) textLat.getText();
Log.d("History","lat : " + lat + " lng " + lng);
SavePreferences("lat", lat);
SavePreferences("lng", lng);

Intent intent = new Intent(this, MapsActivity.class);
intent.putExtra("lat", lat);
intent.putExtra("lan", lng);
startActivity(intent);



}

private void SavePreferences(String key, String value){


SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key,value);



editor.commit();


}





}


Database





package com.sset.jibin.wakemethere;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



public class DatabaseOperations2 extends SQLiteOpenHelper {

static int DATABASE_VERSION = 1;
static String DATABASE_NAME = "data";
static String TABLE_NAME = "history";
static String HISTORY_COLUMN_LNG = "lng";
static String HISTORY_COLUMN_LAT = "lat";
static String HISTORY_COLUMN_NAME = "name";
static String HISTORY_COLUMN_ID = "_id";


public DatabaseOperations2(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("DatabaseHelper", "Database2 constructor");
}

@Override
public void onCreate(SQLiteDatabase database) {
Log.d("DatabaseOP2","onCreate");
database.execSQL("CREATE TABLE " + TABLE_NAME +
"( " + HISTORY_COLUMN_ID + " integer primary key, " + HISTORY_COLUMN_LNG + " text, " +
HISTORY_COLUMN_LAT +" text, "+ HISTORY_COLUMN_NAME + " text)");

}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVer, int newVer) {
Log.d("DatabaseHelper","onUpgrade");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME );
onCreate(database);

}

public void saveRecord(String lat, String lng, String name){
Log.d("DatabaseHelper","saveRecord");
SQLiteDatabase database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(HISTORY_COLUMN_LAT, lat);
contentValues.put(HISTORY_COLUMN_LNG, lng);
contentValues.put(HISTORY_COLUMN_NAME, name);

database.insert(TABLE_NAME , null, contentValues );

}

public Cursor getAllRecords(){



SQLiteDatabase database = this.getReadableDatabase();
return database.rawQuery("SELECT * FROM " + TABLE_NAME , null); }



}



Aucun commentaire:

Enregistrer un commentaire