2017-07-18 54 views
0

当我仍然使用EditText时,代码可以正常工作,但我将其更改为AutoCompleteTextView,以使用户更容易,现在我遇到了问题。这里是大部分的代码:getText with AutoCompleteTextView

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 

    AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item); 

    String[] items = getResources().getStringArray(R.array.items_array); 
    java.util.Arrays.sort(items); 
    ArrayAdapter<String> adapter = 
      new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); 
    edit.setAdapter(adapter); 

public void find(View view) { 

    String name = edit.getText().toString(); 

    if(name.equalsIgnoreCase("Brown Turkey Fig")){ 
     Intent intent = new Intent(this, BrownTurkeyFig.class); 
     startActivity(intent); 
    } 
    else if(name.equalsIgnoreCase("Emerald Green Arborvitae")){ 
     Intent intent = new Intent(this, EmeraldGreenArborvitae.class); 
     startActivity(intent); 
    } 
    else if(name.equalsIgnoreCase("Delaware Valley White Azalea")){ 
     Intent intent = new Intent(this, DelawareValleyWhiteAzalea.class); 
     startActivity(intent); 
    } 

我包括了与问题相关的部分。基本上,用户在AutoCompleteTextView中输入工厂名称,然后单击一个按钮,根据他们输入的任何工厂(我在末尾包括三个作为示例)将其引入新活动。在添加自动填充部分之前,代码的工作方式很好。

问题是,单击按钮时,不会提出下一个活动。它使应用程序崩溃。

我不知道现在有什么问题。也许我不正确地从我的if语句中比较AutoCompleteTextView中的实际文本?任何帮助表示赞赏!

回答

0

对不起,我不明白你的意思。也许你可以尝试使用AutoCompleteTextView.setOnItemClickListener。例如:

edit.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       String item = items.get(position); 
       if(item.equalsIgnoreCase("Brown Turkey Fig")){ 
        Intent intent = new Intent(this, BrownTurkeyFig.class); 
        startActivity(intent); 
       } else if(item.equalsIgnoreCase("Emerald GreenArborvitae"){ 
        Intent intent = new 
           Intent(this,EmeraldGreenArborvitae.class); 
        startActivity(intent); 
       } else if(item.equalsIgnoreCase("Delaware Valley White Azalea")){ 
        Intent intent = new Intent(this, DelawareValleyWhiteAzalea.class); 
        startActivity(intent); 
} 
      } 
     }) 

希望能帮助你。

0

您在oncreate方法中声明了一个局部变量“edit”,但是您在“find”方法中使用了一个全局变量。