如何从android中的AutoCompleteTextView获取字符串文本?

时间:2012-03-24 17:39:29

标签: android autocompletetextview

public class FareActivity extends Activity {


int fareid;
String Source;
String Dest;
AutoCompleteTextView source;
AutoCompleteTextView dest;


static final String[] SOURCE = new String[] {
      "Delhi", "Mumbai", "Agra", "Jaipur};


static final String[] DEST = new String[] {
      "Delhi", "Mumbai", "Agra", "Jaipur};




/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fare);




    dest = (AutoCompleteTextView) findViewById(R.id.acdest);
    ArrayAdapter<String> dadapter = new ArrayAdapter<String>(this, R.layout.list_item, DEST);
    dest.setAdapter(dadapter);



source = (AutoCompleteTextView) findViewById(R.id.acsource);
ArrayAdapter<String> sadapter = new ArrayAdapter<String>(this, R.layout.list_item, SOURCE);
    dest.setAdapter(sadapter);




 // Fare id calculation

     if(Source=="Delhi" && Dest=="Jaipur")
     {
         fareid=1;
     }
     else  if(Source=="Delhi" && Dest=="Agra")
     {
         fareid=2;
     }
     else  if(Source=="Delhi" && Dest=="Mumbai")
     {
         fareid=3;
     }

}

我只想将autocompletetextview'source'和autocompletetextview'dest'值存储到String变量'Source'和String Variable'Dest'。我将在我的项目中使用两个字符串变量进行进一步处理,所以请帮助我。

1 个答案:

答案 0 :(得分:2)

嗯,首先你需要定义threshold value。它是你想要用。填充字符列表的字符数。

acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextbox); acTextView.setThreshold(3);

然后,

add a textwatcher and implement addTextChangedListener AutoCompleteTextView

以下是一个例子:

TextWatcher fieldValidatorTextWatcher = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (filterLongEnough()) {

                String str=acTextView.getText().toString().trim();
                populateList(str);//Function in which i am using the strings from DB, u dnt need it but just in case
            }
        }

        private boolean filterLongEnough() {
            return acTextView.getText().toString().trim().length() > 2;
        }
    };
    acTextView.addTextChangedListener(fieldValidatorTextWatcher);
相关问题