mercredi 22 juillet 2015

Unable to retrieve data from database through spinner

I am creating a blood bank app. I have used spinners to store gender ,state and blood group names. I cannot retrieve data from database using spinners.I need more knowledge on how to retrieve data from database using spinners. I need to get data using two filters bloodgroup and state. kindly help.

Following is the java code for finddonoractivity

public class SecondTabActivity extends Activity implements AdapterView.OnItemSelectedListener { String[] bloodGroupArr = {"A +", "A -", "B +", "B -", "AB +", "AB -", "O +", "O -"}; String[] stateArr = {"Andaman and Nicobar Islands", "Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chandigarh", "Chhattisgarh", "Dadra and Nagar Haveli", "Daman and Diu", "Delhi", "Goa", "Gujarat", "Haryana", "Himachal Pradesh", "Jammu and Kashmir", "Jharkhand", "Karnataka", "Kerala", "Lakshadweep", "Madhya Pradesh", "Maharashtra", "Manipur", "Meghalaya", "Mizoram", "Nagaland", "Odisha", "Puducherry", "Punjab", "Rajasthan", "Sikkim", "Tamil Nadu", "Telangana", "Tripura", "Uttar Pradesh", "Uttarakhand", "West Bengal"}; Button find; Context ctx = this; DatabaseHelper myDb; SQLiteDatabase sdb;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondtablayout);
    find = (Button) findViewById(R.id.findButton);
    final Spinner spinnerRBloodGroup;
    final Spinner spinnerRState;
    spinnerRBloodGroup = (Spinner) findViewById(R.id.spinnerRBloodGroup);
    ArrayAdapter<String> adapterRBloodGroup = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, bloodGroupArr);
    spinnerRBloodGroup.setAdapter(adapterRBloodGroup);

    spinnerRState = (Spinner) findViewById(R.id.spinnerRState);
    ArrayAdapter<String> adapterRState = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, stateArr);
    spinnerRState.setAdapter(adapterRState);

    find.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            String r_donor_group=spinnerRBloodGroup.getSelectedItem().toString();
            String r_donor_state=spinnerRState.getSelectedItem().toString();
            myDb=new DatabaseHelper(getApplicationContext());
            sdb=myDb.getReadableDatabase();

            Cursor CR = myDb.getInformation(r_donor_group);
            StringBuffer buffer=new StringBuffer();
            CR.moveToFirst();
            if(CR.getCount()==0){
                Toast.makeText(SecondTabActivity.this,"No Donor Found",Toast.LENGTH_LONG).show();
            }
            else {
                do {
                    String NAME = CR.getString(2);
                    String NUMBER = CR.getString(8);
                    buffer.append(NAME + "  " + NUMBER + "\n");
                    Toast.makeText(SecondTabActivity.this, "Data retrieved", Toast.LENGTH_LONG).show();
                    System.out.println(buffer);

                } while (CR.moveToFirst());
            }




        }
                            }
    );
}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

}

Aucun commentaire:

Enregistrer un commentaire