2012-06-06 39 views
0

朋友們,我使用AutoCompleteTextView。該建議:如何使用AutoCompleteTextView下拉列表中的seletedItem?

String[] recipes={ "Fish", "Chicken", "Mutton"}; 

如何做到這一點:

當我從下拉列表中的項目之一,它會去到另一個事件?

例如,I型網絡,它會從下拉列表中走出來,然後我選擇魚,它會去另一個活動

package net.learn2develop.Activities; 

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

public class AutoCompleteTextActivity extends Activity { 

String[] recipes ={ 
     "Nasi Lemak With Ikan Bilis", 
     "Steamed Cod Fish" 
}; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,    android.R.layout.simple_dropdown_item_1line, recipes); 


    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtRecipes); 

    textView.setThreshold(3); 
    textView.setAdapter(adapter); 

    textView.setOnItemSelectedListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> av, View view, int index, long id){ 

      Intent i=new Intent(this,Activity2.class); 
      i.putExtra("item",recipes[index]); 
      StartActivity(i); 
     } 

    }); 
} 

}

回答

0

您可以使用實現TextWatcher一個類並重寫下列方法:

 @Override 
     public void afterTextChanged(final Editable editable) { 
      // check if entered text is "fish" and if yes then start the new activity. 
     } 


     @Override 
     public void beforeTextChanged(final CharSequence string, 
       final int start, final int count, final int after) { 
     } 


     @Override 
     public void onTextChanged(final CharSequence string, final int start, 
       final int before, final int count) { 
     } 
} 
0

receipesBelow片段會幫助你。

autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener() 
      { 

       @Override 
       public void onItemSelected(AdapterView<?> av, View view, 
         int index, long id) 
       { 
        //index will give you item which you selected 
            Now start another activity here 
            Intent i=new Intent(context,SecondActivity.class); 
            i.putExtra("item",recipes[index]); 
            StartActivity(i); 


       } 

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

       } 
      }) 

SecondActivity.java

您可以檢索值的onCreate像下面

String selectedItem=getIntent().getStringExtra("item"); 
+0

我對setOnItemSelectedListener錯誤..我有進口的財產以後? – user1438955

+0

你有什麼錯誤!如果你將光標指向紅線,你將會看到錯誤。 –

+0

錯誤顯示:在類型AutoCompleteTextView的方法setOnItemSelectedListener(AdapterView.OnItemSelectedListener)不適用於參數(新OnItemClickListener {}) – user1438955

0

你應該使用setOnItemClickListener代替setOnItemSelectedListener

相關問題