【问题标题】:How to invoke getText() on the primitive type int?如何在原始类型 int 上调用 getText()?
【发布时间】:2012-03-12 02:27:47
【问题描述】:

我正在尝试使用可点击的 AutoCompleteTextView 建议并为用户带来另一个活动。如果你们当中有人知道如何使用 AutoCompleteTextView,请前往AutoCompleteTextView or SearchDialog?

除了调用 getText() 之外,这就是我正在使用的内容。另外如何在我的开关案例中定义我的意图?

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class Search extends Activity
{


    public void onCreate(Bundle savedInstanceSate)
    {
        final int autoComplete;
        super.onCreate(savedInstanceSate);
        setContentView(R.layout.searchshop);

        AutoCompleteTextView autoCompletee = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, shops);
        autoCompletee.setAdapter(adapter); 
        autoCompletee.setThreshold(1);
        autoCompletee.setOnItemClickListener(new OnItemClickListener() {

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


                Intent intent;
                int index=999;
                for(int i=0;i<shops.length;i++)
                {

                    if(shops[i].equals(autoComplete.getText().toString().trim()))
                    {
                        index=i;
                        break;
                    }
                }
                switch(index)
                {
                    case 0:
                        //The constructor Intent(Search, int) is undefined
                        intent=new Intent(Search.this, R.layout.adidas);
                        startActivity(intent);
                        setContentView(R.layout.adidas);
                        break;
                    case 1:
                        //The constructor Intent(Search, int) is undefined
                        intent=new Intent(Search.this, R.layout.affin);
                        startActivity(intent);  
                        setContentView(R.layout.affin);
                        break; 
                }
            }
        });


    }
    static final String[] shops = new String[]
            {
                "Adidas", " Affin Bank", "Alam Art", "Al Amin"

            };
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    基本类型(或一般引用类型)没有getText() 方法。但我认为这应该做我认为你想做的事情:

    替换

        autoComplete.getText().toString().trim()
    

        Integer.toString(autoComplete)
    

    【讨论】:

    • 好的,Integer.toString(autoComplete) 给我这个。局部变量 autoComplete 尚未初始化。我必须像在 static final int autoComplete 中一样声明 autoComplete;但是在该方法中不允许使用 final 声明。
    【解决方案2】:

    我不知道您在哪里尝试在 int 上调用 getText()。要将int 转换为String,您可以使用String.valueOf()Integer.toString()

    你应该以另一种方式构造意图:

    intent = new Intent(Search.this, YourAnotherActivityName.class);
    

    补充阅读:
    How to start activities
    How to pass data between activities

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 2018-11-03
      • 2012-04-15
      • 2014-05-19
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多