Android上的AutoCompleteTextView点击事件

All*_*lly 8 sqlite android autocomplete onclick

我已成功实现了基于SQLite查询的AutoCompleteTextView,并放置在数组适配器中.这一切都很美妙,但我不能让我的onclickevent工作.

我只想创建一个将所选值传递给新活动的意图.我知道如何创建onclicklistener.我只是不确定如何将它应用于AutoCompleteTextView的下拉框.

All*_*lly 15

没关系.我已经解决了.我只是表现不佳.下面的代码根据一个简单的SELECT SQLite语句自动填充我的textview,并在用户从下拉列表中选择大学时执行.

onclick事件创建一个新意图并启动一个新活动,将选择内容传递给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);
    }
Run Code Online (Sandbox Code Playgroud)