gpt4 book ai didi

android - 多个 AutoCompleteTextView 的 onItemClickListener

转载 作者:行者123 更新时间:2023-11-29 15:07:31 25 4
gpt4 key购买 nike

我有三个 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
}
}

关于android - 多个 AutoCompleteTextView 的 onItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35619280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com