lundi 23 février 2015

Use getText().toString(); As Database Query Filter

I am just learning about Android and databases and would appreciate some assistance, hope I can be clear.


I have an Activity class where I get the getText().toString() value which works fine when I log the value returned, I would like to take this value and use it in my DatabaseHelper class to filter my query by this value which comes from a user click.


Activity Class:



@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
// TODO Auto-generated method stub

LinearLayout ll = (LinearLayout) viewClicked;
TextView tv = (TextView) ll.findViewById(R.id.textViewArtistsSingle);
String tvName = tv.getText().toString()

// tv.getText().toString() will return JOHN if the user clicked on John

startActivity(new Intent(CURRENT_ACTIVITY.this, NEXT_ACTIVITY.class));


DatabaseHelper Class:



public List<String> getDBNameSingle(){

List<String> listRows = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();

Cursor c;

try {

c = db.rawQuery("SELECT name FROM " + TABLE_LM + " WHERE name = 'JOHN'", null); //Get Specific Name HARD)


I would like to HAVE THIS: (note tvName from getText().toString()



c = db.rawQuery("SELECT name FROM " + TABLE_LM + " WHERE name = '" + tvName +"'", null); //Get Specific Name VARIABLE)


INSTEAD of THIS:



c = db.rawQuery("SELECT name FROM " + TABLE_LM + " WHERE name = 'JOHN'", null); //Get Specific Name HARD)


But my DatabaseHelper class does not know about tvName, how can I pass this variable from CURRENT_ACTIVITY to DatabaseHelper so that I may filter the name column to return the row WHERE name = tvName (from getText().toString();)


Aucun commentaire:

Enregistrer un commentaire