1

在我的 android 应用程序中,我有一个 autocompletetextview 字段,可以向用户建议不同的州名称。代码如下:

 ArrayAdapter<String> state_adapter=new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,state);
    actv_state=(AutoCompleteTextView)findViewById(R.id.edt_state);
    actv_state.setThreshold(1);
    actv_state.setAdapter(state_adapter);

然后我以这种方式使用 TextWatcher 在字符串中获取此状态的值:

 TextWatcher state_text=new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        str_state=actv_state.getText().toString();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};
actv_state.addTextChangedListener(state_text); 

在此之后,我试图根据所选状态在另一个自动完成文本视图中显示城市名称。我似乎无法弄清楚我在哪里放置此代码以根据所选状态显示城市名称????

 if(str_state.equalsIgnoreCase("MADHYA PRADESH"))
         {
          ArrayAdapter<String> mp_city_adapter=new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,mp_city);
          actv_city=(AutoCompleteTextView)findViewById(R.id.app_city);
          actv_city.setThreshold(1);
          actv_city.setAdapter(mp_city_adapter);
         }

         if(str_state.equalsIgnoreCase("CHATTISGARH"))
         {
          ArrayAdapter<String> mp_city_adapter=new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,mp_city);
          actv_city=(AutoCompleteTextView)findViewById(R.id.app_city);
          actv_city.setThreshold(1);
          actv_city.setAdapter(mp_city_adapter);
         }

我该怎么做?我似乎无法弄清楚!如果有人知道任何解决方案,请帮助!谢谢!

4

2 回答 2

1

它应该在自动完成文本视图 itemclick 监听器内

 actv_state.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {

         if(actv_state.getText().toString().equals("MADHYA PRADESH"))
     {
      ArrayAdapter<String> mp_city_adapter=new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,mp_city);
      actv_city=(AutoCompleteTextView)findViewById(R.id.app_city);
      actv_city.setThreshold(1);
      actv_city.setAdapter(mp_city_adapter);
     }

     if(actv_state.getText().toString().equals("CHATTISGARH"))
     {
      ArrayAdapter<String> mp_city_adapter=new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,mp_city);
      actv_city=(AutoCompleteTextView)findViewById(R.id.app_city);
      actv_city.setThreshold(1);
      actv_city.setAdapter(mp_city_adapter);
     }

        }
    });
于 2013-05-20T08:51:44.450 回答
1

试试这样

 TextWatcher state_text=new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
             str_state=s.toString();
}
};
于 2013-05-20T08:57:17.807 回答