0

I have retrieved all the first column data in Spinner. I want to retrieve SQLite data when I select a spinner item. The data will be displayed in EditTexts. But when I run the app, data cannot be displayed in the EditText. Can someone give me a suggestion?Thanks in advance.

Here is my code

spinner_searchByEmpName = (Spinner)findViewById(R.id.searchByEmpName);
        loadSerachBYProject() ;


spinner_searchByEmpName = (Spinner)findViewById(R.id.searchByEmpName);
        loadSerachBYProject() ;

        spinner_searchByEmpName.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                selectedEmployeeName = spinner_searchByEmpName.getSelectedItem().toString().trim();
                System.out.println("selectedProjectName " + selectedEmployeeName);

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });



            etEmpName=(EditText)findViewById(R.id.Delete_editText_StaffEmployee_Name);
          etDepartment=(EditText)findViewById(R.id.Delete_editText_Department);
          etDesignation=(EditText)findViewById(R.id.Delete_editText_Designation);

        try {


            databaseHelper = new DatabaseHelper(this);
            db=databaseHelper.getReadableDatabase();

            Cursor cursor = db.rawQuery("SELECT staff_emp_name, department, designation FROM employee_details WHERE staff_emp_name = ?",
                            new String[] { "" + selectedEmployeeName });

            if(cursor!=null && cursor.moveToFirst())
            {

                  etEmpName.setText(cursor.getString(cursor
                            .getColumnIndex("staff_emp_name")));



                    etDepartment.setText(cursor.getString(cursor
                            .getColumnIndex("department")));

                    etDesignation.setText(cursor.getString(cursor
                            .getColumnIndex("designation")));  
                }
        }

        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }


    }
6
  • What happens while running this code? Commented Jun 27, 2014 at 5:29
  • @Gunaseelan: While i run it is not showing any error and not showing any row data in edit_text's. Commented Jun 27, 2014 at 5:33
  • okay, now print cursor.getCount() and tell me the answer. Commented Jun 27, 2014 at 6:04
  • @Gunaseelan: It shows cursor size is 0. Commented Jun 27, 2014 at 6:27
  • @Gunaseelan : why not display my row data in Activity. Where is the problem. Commented Jun 27, 2014 at 6:31

1 Answer 1

1

I suggest you take a look at tutorial in android database here take a look: Android SQLite database and content provider - Tutorial

Never use a hardcode name for your table name and columns store them in variables like this one:

public static final String EMP_TABLE = "employee_details";
public static final String EMP_COL_NAME = "staff_emp_name"

You can create an object representing your data in your database like this one:

public class Employee{
    private String name;
    private String department;
    private String designation;

    public Employee(String name,String dept,String designation){
        this.name = name;
        this.department = dept;
        this.designation = designation;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public String getDesignation() {
        return designation;
    }
    public void setDesignation(String designation) {
        this.designation = designation;
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Also refactor your method like the one below:

  private Employee getEmployeeByProj(String name){
 try {


            databaseHelper = new DatabaseHelper(this);
            db=databaseHelper.getReadableDatabase();


             final String where = EMP_TABLE + "=\"" + name + "\"";

            Cursor cursor = db.query("employee_details",new String[] {"staff_emp_name","department","designation"},where,null,null,null,null);

            if(cursor!=null && cursor.moveToFirst())
            {

                        final String name = cursor.getString(cursor
                            .getColumnIndex(EMP_COL_NAME));
                        final String dept = cursor.getString(....
                        final String designation cursor.getString(.....

                      //do the rest of the code here

                  return new Employee(name,dept,designation);
                }
        }

        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    }

I suggest you take a look on the link i given you above

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

4 Comments

Still not display the data.
Make sure you have data first, try it in terminal using sqlite or you can download a sqlite browser for mozilla firefox
the problem must be in your query @Robotics
Thanks allot million, my code is working , you are great !! Thanks once again.

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.