Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoCompleteTextView click event on Android

I have successfully implemented my AutoCompleteTextView which is based off an SQLite query and is placed in an array adapter. That's all working beautifully, however I can't get my onclickevent working.

I just want to create an intent to pass the selected value to a new activity. I know how to create an onclicklistener. I am just unsure about how to apply it to the dropdown box of the AutoCompleteTextView.

like image 284
Ally Avatar asked Aug 08 '10 16:08

Ally


1 Answers

Nevermind. I've solved it. I was just executing poorly. The code below autocompletes my textview based off a simple SELECT SQLite statement and executes when the user selects the university from the dropdown list.

The onclick event creates a new intent and starts a new activity passing the selection to this activity within the intent.

final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.ac_university);
String[] universities = myDbHelper.getAllUnis(db);

// Print out the values to the log
for(int i = 0; i < universities.length; i++)
{
    Log.i(this.toString(), universities[i]);
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, universities);
textView.setAdapter(adapter);

//textView.setOnItemSelectedListener(this);
textView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {

        Intent intent = new Intent(Main.this, Campus.class);
        Bundle bundle = new Bundle();

        bundle.putString("university_name", arg0.getItemAtPosition(arg2).toString());
        bundle.putLong("_id", arg3);
        intent.putExtras(bundle);
        startActivity(intent);
    }
like image 97
Ally Avatar answered Sep 29 '22 05:09

Ally