mercredi 13 janvier 2016

I want to show the column items from an existing sqlite database into an autocompletetextview using AsyncTask

This is my getall() method which is in DataBaseHelper Class

public ArrayList<String> getAll(){
        ArrayList<String> listUsers = new ArrayList<String>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c;

        try {
            c = db.rawQuery("SELECT * FROM bank"  , null);
            if(c == null) return null;

            String name;
            c.moveToFirst();
            do {
                name = c.getString(1);
                listUsers.add(name);
            } while (c.moveToNext());
            c.close();
        } catch (Exception e) {
            Log.e("blushmaq", e.getMessage());
        }

        db.close();

        return listUsers;
    }

This is my other class where I want to populate the records from the getall() method which is in DataBaseHelper

public class BankInfo extends ActionBarActivity {


    private Toolbar toolbar;
    Button searchButton;
    DataBaseHelper myDbHelper = new DataBaseHelper(this);
    TextView sample;
    AutoCompleteTextView t1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_info);

        sample = (TextView) findViewById(R.id.sampleTextView);
        searchButton = (Button) findViewById(R.id.searchButton);
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DBOperations().execute();
            }
        });

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        DBOperations.execute(true);
        try {
            myDbHelper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private class DBOperations extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {

            return params[0];
        }

        @Override
        protected void onPostExecute(String s) {

            ArrayList<String> brands = myDbHelper.getAll();
            t1 = (AutoCompleteTextView) findViewById(R.id.bank_name);
            if(brands != null){
                ArrayAdapter<String> adapter=new ArrayAdapter<String>(BankInfo.this,android.R.layout.simple_dropdown_item_1line,brands);
                t1.setThreshold(1);
                t1.setAdapter(adapter);
            }
            sample.setText(s);
            myDbHelper.close();
        }
    }

I'm getting the error ArrayIndexOutofBoundException Length=0 Index=0

Aucun commentaire:

Enregistrer un commentaire