简体   繁体   中英

How to use value entered in textView as where clause in getData method

Im an extreme noob to Android development, please help me out. I wrote a app that inserts data into a SQLite db.

In the app I have a "Check DB" button that displays all data in the sqlite db via a message box, but now i want to filter the info based on one unique value added in textview.

At this stage my getData method only select * from db, but no where clause.

How can I use the value entered in the textView as the where clause in other words:

select * from tableName where columnName = textView's entered value

    public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "AssetVerification.db";
public static final String TABLE_NAME = "AssetDetails";
public static final String Col1 = "ID";
public static final String Col2 = "Asset_Location_Code";
public static final String Col3 = "Asset_Barcode_Number";
public static final String Col4 = "Asset_Description";
public static final String Col5 = "Asset_Condition";
public static final String Col6 = "Asset_Category";
public static final String Col7 = "Asset_Picture_Reference";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, Asset_Location_Code TEXT, Asset_Barcode_Number TEXT, Asset_Description TEXT, Asset_Condition TEXT, Asset_Category TEXT, Asset_Picture_Reference TEXT)");

}

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

}

public boolean insertData(String Asset_Location_Code,String Asset_Barcode_Number,String Asset_Description,String Asset_Condition,String Asset_Category,String Asset_Picture_Reference) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(Col2,Asset_Location_Code);
    contentValues.put(Col3,Asset_Barcode_Number);
    contentValues.put(Col4,Asset_Description);
    contentValues.put(Col5,Asset_Condition);
    contentValues.put(Col6,Asset_Category);
    contentValues.put(Col7, Asset_Picture_Reference);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if(result == -1)
        return false;
    else
        return true;
}



public Cursor getAllData() {

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from AssetDetails" ,null);
    return res;
}

And my mainActivity code:

    public void getAssetInfo(){
    btnCheckAssetData.setOnClickListener(
            new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor res = myDb.getAllData();
                    if (res.getCount() == 0) {
                        //Show message
                        showMessage("Error", "No Data");
                        return;
                    }

                    StringBuffer buffer = new StringBuffer();
                    while (res.moveToNext()) {
                        //txtAssetCatData.setText(res.getString(0));
                        buffer.append("Asset Location : " + res.getString(1) + "\n");
                        buffer.append("Barcode : " + res.getString(2) + "\n");
                        buffer.append("Description : " + res.getString(3) + "\n");
                        buffer.append("Condition : " + res.getString(4) + "\n");
                        buffer.append("Category : " + res.getString(5) + "\n");
                        buffer.append("Picture reference : " + res.getString(6) + "\n\n");
                    }

                    //Show all data
                    showMessage("Data", buffer.toString());
                }


            }
    );
}

public void showMessage(String title, String Message){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(Message);
    builder.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

to get data from textview check below code

String text=textview.getText().toString();

now you can use text in your where clause.

WHERE columnName =text.

in your button clickListener do like below

DatabaseHelper dbHelper=new DatabaseHelper(this);
dbHelper.getAllData(_Your_Text_View.getText().toString());

and in your helper class change your getAllData() to

public Cursor getAllData(String text)
{
    //your code
}

Try update function below

public Cursor getAllData(String text)
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from AssetDetails where columnName ='" + text + "'", null);
    return res;
}

Example

Cursor c = db.getAllData(textView.getText().toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM