我有三个AutoCompleteTextView并将其设置为onItemClickListener,请您帮助我知道单击了哪个AutoCompleteTextView
public class ProductMainListViewer extends AppCompatActivity implements AdapterView.OnItemClickListener {
CustomerDataBaseAdapter customerDataBaseAdapter;
AutoCompleteTextView txtUserName;
AutoCompleteTextView txtContactNo;
AutoCompleteTextView txtSearchProduct;
TextView txtDateAndTime;
TextView txtQty;
TextView txtSalePrize;
TextView txtRunit;
TextView txtWunit;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_main_list_viewer);
customerDataBaseAdapter = new CustomerDataBaseAdapter(this);
customerDataBaseAdapter = customerDataBaseAdapter.open();
ArrayList<Customer> customers = new ArrayList<>();
ArrayList<ProductsDetails> productsDetails = new ArrayList<>();
try {
customers = customerDataBaseAdapter.getCustomerList();
} catch (Exception e) {
e.printStackTrace();
}
try {
productsDetails = customerDataBaseAdapter.getProductList();
} catch (Exception e) {
e.printStackTrace();
}
txtUserName = (AutoCompleteTextView) findViewById(R.id.editTextUserNamecheck);
CustomArrayAdapterAutoCmplt adapter1 = new CustomArrayAdapterAutoCmplt(this, R.layout.activity_auto_cmplt_custom, customers);
txtUserName.setThreshold(1);
txtUserName.setAdapter(adapter1);
txtUserName.setOnItemClickListener(this);
txtContactNo = (AutoCompleteTextView) findViewById(R.id.editTextPhoneNoNew);
CustomPhoneArrayAdapterAutoCmplt adapter2 = new CustomPhoneArrayAdapterAutoCmplt(this, R.layout.activity_auto_cmplt_custom, customers);
txtContactNo.setThreshold(1);
txtContactNo.setAdapter(adapter2);
txtContactNo.setOnItemClickListener(this);
txtSearchProduct = (AutoCompleteTextView) findViewById(R.id.searchProduct);
ProductAutoCmplt adapter3 = new ProductAutoCmplt(this, R.layout.activity_auto_cmplt_custom, productsDetails);
txtSearchProduct.setThreshold(1);
txtSearchProduct.setAdapter(adapter3);
txtSearchProduct.setOnItemClickListener(this);
txtDateAndTime = (TextView) findViewById(R.id.dateAndTime);
txtQty = (TextView) findViewById(R.id.txtQty);
txtSalePrize = (TextView) findViewById(R.id.txtPrize);
txtRunit = (TextView) findViewById(R.id.txtRUnit);
txtWunit = (TextView) findViewById(R.id.txtWUnit);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
if (txtUserName.getText().equals("")) {
Customer item = (Customer) txtContactNo.getAdapter().getItem(position);
txtUserName.setText(item.getMember_name());
} else {
Customer item = (Customer) txtUserName.getAdapter().getItem(position);
txtContactNo.setText(item.getContactType());
}
DateFormat df = new SimpleDateFormat("dd.MM.yyyy 'at' HH:mm:ss ");
String date = df.format(Calendar.getInstance().getTime());
txtDateAndTime.setText(date);
} catch (Exception e) {
}
}
}
最佳答案
在onItemClick中,您可以标识Adapter AdapterView父级适配器,因此可以首先标识父级适配器。
像下面这样
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (parent.getAdapter().getClass().equals(ProductAutoCmplt .class)) {
//do your stuff
}
else if(parent.getAdapter().getClass().equals(CustomPhoneArrayAdapterAutoCmplt .class)){
//do your stuff
}else if(parent.getAdapter().getClass().equals(CustomArrayAdapterAutoCmplt.class)){
//do your stuff
}
}