如何使项目中选择不自动选择第一项 [英] How to make On Item Selected not automatically choose the first entry

查看:183
本文介绍了如何使项目中选择不自动选择第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建这是当一个人使用阵列适配器增加了家电与家电的名称自动更新好手。我创建了,所以当在微调的名字中选择一个微调的OnItemSelected方法,将出现一个新的窗口。然而,当活动开始,因此用户不会有机会实际进行选择,直到出现新的窗口OnItemSelected自动选择所述列表上的第一项。

下面是code:

 公共无效onItemSelected(适配器视图<>为arg0,ARG1观,诠释ARG2,
        长ARG3){
    // TODO自动生成方法存根
    startActivity(新意图(com.lukeorpin.theappliancekeeper.APPLIANCESELECTED));
    }公共无效onNothingSelected(适配器视图<>为arg0){
    // TODO自动生成方法存根

有谁知道在名单上的第一个项目不会被自动选择一种方式?

下面是code为微调的其余部分:

  ArrayAdapter<串GT; appliancenameadapter =新ArrayAdapter<串GT;(这一点,
            android.R.layout.simple_spinner_item,ApplianceNames); //设置了包含ApplianceNames字符串数组的值的数组适配器
    applianceName =(微调)findViewById(R.id.spinner_name); //给出了XML布局变量名微调
    applianceName.setAdapter(appliancenameadapter); //添加数组适配器的内容到微调    applianceName.setOnItemSelectedListener(本);


解决方案

  

有谁知道在名单上的第一个项目不会被自动选择一种方式?


有是的总是的关于微调一个选择,你无法改变这一点。

恕我直言,你不应该使用微调来触发启动的一项活动。

这就是说,你可以使用布尔来跟踪这是否是第一次评选活动,而忽略它,如果它是。

I have created a spinner which is automatically updated with appliance names when a person adds an appliance using an array adapter. I created an OnItemSelected method with the spinner so when one of the names in the spinner is selected, a new window appears. However the OnItemSelected is automatically selecting the first item on the list when the activity starts and so the user does not have a chance to actually make a selection until the new window appears.

Here is the code:

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    startActivity(new Intent("com.lukeorpin.theappliancekeeper.APPLIANCESELECTED"));
    }

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

Does anyone know a way in which the first item on the list wont be automatically selected?

Here is the code for the rest of the spinner:

ArrayAdapter<String> appliancenameadapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, ApplianceNames); //Sets up an array adapter containing the values of the ApplianceNames string array
    applianceName = (Spinner) findViewById(R.id.spinner_name); //Gives the spinner in the xml layout a variable name
    applianceName.setAdapter(appliancenameadapter); //Adds the contents of the array adapter into the spinner

    applianceName.setOnItemSelectedListener(this);

解决方案

Does anyone know a way in which the first item on the list wont be automatically selected?

There is always a selection on Spinner, and you cannot change that.

IMHO, you should not be using a Spinner to trigger starting an activity.

That being said, you can use a boolean to track whether this is the first selection event, and ignore it if it is.

这篇关于如何使项目中选择不自动选择第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆