Home > OS >  Searching from sqlite database and displaying record in EditText causes crash/restart, what went wro
Searching from sqlite database and displaying record in EditText causes crash/restart, what went wro

Time:03-24

I have a crud app project in android studio. I have done insert, update, delete and view all. The only one I'm having error with is the search function. The search code looks good to me, so it would be great if you can help me find my mistake. Thank you!

The table is created this way:

    public void onCreate(SQLiteDatabase DB) {
    DB.execSQL("create Table ProdDetails(id INTEGER primary key, name TEXT, description TEXT, price NUMERIC, quantity INTEGER)");
}

The search method, accepts input from EditText which was converted to int:

    public Cursor searchData (int id)
{
    SQLiteDatabase DB = this.getWritableDatabase();
    Cursor cursor = DB.rawQuery("Select * from ProdDetails where id = ?", new String[]{String.valueOf(id)});
    return cursor;
}

The Main class:

public class RUDActivity extends AppCompatActivity {
EditText prodId, name, description, price, quantity;
Button update, delete, view, search;
DBHelper DB;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retrieve);
    prodId = (EditText) findViewById(R.id.prodId);
    name = (EditText) findViewById(R.id.name);
    description = (EditText) findViewById(R.id.description);
    price = (EditText) findViewById(R.id.price);
    quantity = (EditText) findViewById(R.id.quantity);
    search = findViewById(R.id.btnSearch);
    DB = new DBHelper(this);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int prodTXT = Integer.parseInt(prodId.getText().toString());
            Cursor res = DB.searchData(prodTXT);
            if(res.getCount()==0){
                Toast.makeText(RUDActivity.this, "Product ID Not Found", Toast.LENGTH_SHORT).show();
                return;
            }

            // app crash/restart after this line, am I doing this wrong?

            prodId.setText("Product ID: " res.getString(0));
            name.setText("Name: " res.getString(1));
            description.setText("Description: " res.getString(2));
            price.setText("Price: " res.getString(3));
            quantity.setText("Quantity: " res.getString(4));

        }
    });
}
}

When I search prodId that does not exist it would show toast message "Product ID Not Found" and the app won't crash/restart. But if I search a prodId that does exist the app will crash.

CodePudding user response:

You are missing a call to the method moveToFirst() before you retrieve the values of the columns:

res.moveToFirst();
prodId.setText("Product ID: " res.getString(0));
................................................

because the cursor's index is initially positioned before the 1st row of the results.

Or, instead of using getCount() to check if the cursor returned any rows use moveToFirst():

if (!res.moveToFirst()) {
    Toast.makeText(RUDActivity.this, "Product ID Not Found", Toast.LENGTH_SHORT).show();
} else {
    prodId.setText("Product ID: " res.getString(0));
    ................................................
}
  • Related