0

I am stuck on a project, need help to solve this issue.
I receive the data from 3 different inputs (2 are Spinners and 1 is a TextView).
The table column name are: customer id, quantity and production id.

I need to check production id for duplicate entries before inserting to my table.

Here is what I currently do:

if(res.getCount()==0){
            boolean isInserted = mytempDB.insertdataintotemptable(
                    ProducationSpinner.getSelectedItem().toString(), QuantityText.getText().toString(),
                    CustomerSpinner.getSelectedItem().toString());
            if (isInserted == true)
                Toast.makeText(orderChalan.this, "Data save successfully", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(orderChalan.this, "Data not Inserted", Toast.LENGTH_LONG).show();
        } else {

            while(res.isAfterLast()){

                if (res.getString(4).toString() == CustomerSpinner.getSelectedItem().toString()) {

                    boolean isInserted = mytempDB.insertdataintotemptable(ProducationSpinner.getSelectedItem().toString(),
                            QuantityText.getText().toString(), CustomerSpinner.getSelectedItem().toString());

                    if (isInserted == true)
                        Toast.makeText(orderChalan.this, "Data save successfully", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(orderChalan.this, "Data not Inserted", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(orderChalan.this, "Production ID can not different", Toast.LENGTH_LONG).show();
                }
            }
        }

I select the first row and receive a production ID value.
After that I try to match the user input and data from the table each time, then I insert into the table.
If the data don't match, I then display an error message "Production Id can not different" and also I check the first time entry if there is no data for the first input; in that case there are no checks.

The error I get is "Production id can not be different" - I get this message from the 2nd time input.

Any suggestion?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user3728517
  • 157
  • 3
  • 10
  • 1
    try doing sting comparison with .equals() rather than ==. You might find this useful [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MikeT Nov 11 '16 at 10:41

2 Answers2

0

I am describing the logic. What you have to do in your code .

  1. First check your table contains any data or not .
  2. If table is empty then insert your current data into table .
  3. If table is not empty then run a query for checking the current "production id" available in table or not . query is " select * from TABLE_NAME WHERE Column_name(production id) = your current production id.
  4. If Cursor.getCount >0
    // current production id exists
    else
    // insert current details in table
anu
  • 213
  • 1
  • 3
  • 10
0

I believe that the issue that you are describing, is due to using == to compare strings. == will test if the string objects/instances are the same it does not compare the values held by the strings.

That is this line

if (res.getString(4).toString() == CustomerSpinner.getSelectedItem().toString()) {

will consistently return false.

Instead of using == you need to implement .equals()

So, something along the lines of

if ((res.getString(4).toString()).equals(CustomerSpinner.getSelectedItem().toString())) { 
MikeT
  • 51,415
  • 16
  • 49
  • 68