0

I am beginner. I just want to make a program that user will enter the data on my EditText then program will search this data from database. If it is found, it will get all data related to the data(which user entered) then they will be shown in my TextView. I writed some codes, but it didn't work. Please help me.

"bNum" is name of coloumn in my table.

This is some part of my code:

final EditText bnumber = (EditText) findViewById(R.id.editText1);
    Button search = (Button) findViewById(R.id.button1);
    search.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            try {
                DataSearch((bnumber.getText()).toString());

            } finally {
                book.close();
            }

        }
    });

}

private static final String[] SELECT = { "bNum" };
protected void DataSearch(String s) {
    SQLiteDatabase db = book.getReadableDatabase();
    Cursor c = db.query("btable", SELECT, null, null, null, null, null);
    while (c.moveToNext()) {
        if (c.toString() == s)
        ShowingData(c);

    }}
       private void ShowingData(Cursor c) {


    StringBuilder builder = new StringBuilder("Information\n");
    String pName = c.getString(c.getColumnIndex("pName"));
    String price = c.getString(c.getColumnIndex("price"));
    String quantity = c.getString(c.getColumnIndex("quantity"));
    builder.append(pName).append("Product Name:");
    builder.append(price).append("Price:");
    builder.append(quantity).append("Quantity:");

    TextView result = (TextView) findViewById(R.id.textView1);
    result.setText(builder);

}}

2 Answers 2

1

I can´t see Your database, and don´t know how your query method works. But for example, if You query the whole db with a cursor object that returns all values from every row, this should work:

    protected void DataSearch(String s) {
       SQLiteDatabase db = book.getReadableDatabase();
       Cursor c = db.getAllDatas();
       c.moveToFirst();
    while (c.moveToNext()) {
      if (c.getString(0).equalsIgnoreCase(s))
      ShowingData(c);

   }}

getAllDatas() should be a method where You get all Datas from the database.

Sign up to request clarification or add additional context in comments.

Comments

1

String comparison is done with equals() method

try replacing your

if (c.toString() == s)

with

if (c.toString().equals(s))

10 Comments

yes, you are right! Thank you. But it doesn't solve my question.
what problem/exception you are getting?
The data isn't shown in my TextView. So it means that my codes aren't working.
if capital letters are not important, maybe try c.toString().equalsIgnoreCase(s));, because by opening the keyboard, in some cases the first letter is automatically a capital.
@Opiatefuchs Okey, i'll change it. But how about my DataSearch method? Is there any something wrong?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.