根据第一个值更新第二个autocompletetextview

时间:2013-05-20 08:34:58

标签: android autocompletetextview

在我的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); 

在此之后我试图根据所选的状态在另一个autocompletetextview中显示城市名称。 我似乎无法弄清楚我在哪里根据所选的状态显示城市名称????

 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);
         }

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

2 个答案:

答案 0 :(得分:1)

应该在autocompletetextview项目内部点击监听器

 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);
     }

        }
    });

答案 1 :(得分: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();
}
};