Android 上的 AutoCompleteTextView 单击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3434978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:10:51  来源:igfitidea点击:

AutoCompleteTextView click event on Android

androidsqliteautocompleteonclick

提问by Ally

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

我已经成功实现了我的 AutoCompleteTextView,它基于SQLite查询并放置在数组适配器中。这一切都很好,但是我无法让我的 onclickevent 工作。

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.

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

回答by Ally

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.

没关系。我已经解决了。我只是表现不佳。下面的代码基于一个简单的 SELECT SQLite 语句自动完成我的文本视图,并在用户从下拉列表中选择大学时执行。

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

onclick 事件创建一个新的意图并启动一个新的活动,将选择传递给该意图内的该活动。

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);
    }

回答by Shouvik

putExtra function can be used for this purpose.

putExtra 函数可用于此目的。

Here is an example...

这是一个例子...

Form the sending activity:

形成发送活动:

    lv.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
            ApplicationInfo x = appinstalled.get(pos);
            PackageInfo y = appinstall.get(pos);
            //Intent i = new Intent(InstalledPackages.this, Information.class);
            i = new Intent(InstalledPackages.this, Information.class);
            i.putExtra("i",x);
            i.putExtra("j", y);
            startActivity(i);
        }
    });
}

On the receiving side:

在接收端:

    super.onCreate(savedInstanceState);
    Intent myIntent = getIntent();
    ApplicationInfo i = (ApplicationInfo)myIntent.getParcelableExtra("i");
    PackageInfo j = (PackageInfo)myIntent.getParcelableExtra("j");