如何为同一活动中的AutoCompleteTextView和ListView设置setOnItemClickListener?

时间:2017-11-02 16:16:03

标签: android listview autocompletetextview onitemclicklistener

我正在尝试setOnItemClickListener AutoCompleteTextViewListView同一活动,我为两者提供了OnItemClickListener,当我这样做时,两者都无效。但是,当我把它提供给一个运作良好的任何一个。

我的代码:

public class MainActivity extends Activity implements AdapterView.OnItemClickListener{
ListView listView;
Button button;
AutoCompleteTextView autoCompleteTextView;


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listView);
    button =(Button) findViewById(R.id.button) ;
    autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
    autoCompleteTextView.setOnItemClickListener(this);
listView.setOnItemClickListener(this);
    loadAutoCompleteTextViewData();


}

public void loadAutoCompleteTextViewData()
{
    DbHandler db = new DbHandler(getApplicationContext());
    List<String> BusLocationList = db.getBusLocation();
    ArrayAdapter<String> ListViewAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,BusLocationList);
    autoCompleteTextView.setThreshold(1);
    autoCompleteTextView.setAdapter(ListViewAdapter);


}

@Override
public boolean onTouchEvent(MotionEvent event) {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    return true;

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (view.getId() == R.id.autoCompleteTextView) {
        String loc = parent.getItemAtPosition(position).toString();
        DbHandler db = new DbHandler(getApplicationContext());
        List<String> BusRouteList = db.getBusRoute(loc);
        ArrayAdapter<String> lvAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, BusRouteList);
        listView.setAdapter(lvAdapter);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
    else if (view.getId() == R.id.listView) {
        //Intent will be used here after fixing the problem
    }       


}

}

0 个答案:

没有答案