我正在尝试setOnItemClickListener
AutoCompleteTextView
和ListView
同一活动,我为两者提供了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
}
}
}