开发者

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.


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


putExtra function can be used for this purpose.

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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜