我正在创建一个Android应用程序。它具有一些带有搜索栏的弹出对话框。但是问题是:当我使用该产品搜索某个产品时,该搜索的匹配项应添加到弹出对话框内的列表视图中,这很好。
但是当我选择要在弹出对话框中搜索项目的项目时,应该将其保存在我先前创建的对象(OrderView对象)中,但是该对象中存储的项目始终是错误的 (仅当我搜索商品时才会发生这种情况)。问题是从此代码行发生的,
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
因为我要获得位置来标识所选项目。
但是我不知道如何替换此代码行以正确的方式。非常感谢您的帮助。谢谢。
这是我的代码:
final List<Product> products_data = dbHelper.productListView();
if (products_data.size() > 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select A Product");
//insert array to constructor
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.product_list_pop_up, null);
final ProdductPopupAdapter testAdapter = new ProdductPopupAdapter(getContext(), products_data);
final ListView listView = dialogLayout.findViewById(R.id.product_list_view);
TextView cancel_btn = dialogLayout.findViewById(R.id.cancel_btn);
final EditText search_view = dialogLayout.findViewById(R.id.list_item_search);
listView.setAdapter(testAdapter);
search_view.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
final ProdductPopupAdapter testAdapter = new ProdductPopupAdapter(getContext(),dbHelper.searchProductList(search_view.getText().toString()));
listView.setAdapter(testAdapter);
testAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
builder.setView(dialogLayout);
final AlertDialog alert = builder.create();
alert.show();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
OrderView orderView = new OrderView();
orderView.setProduct_name(products_data.get(position).getProductName());
orderView.setProduct_qty(0);
orderView.setProduct_price(products_data.get(position).getPrice());
orderView.setNewItemtotal(Double.parseDouble(products_data.get(position).getPrice()));
orderView.setTotalPrice(products_data.get(position).getPrice());
orderView.setProduct_id(Integer.parseInt(products_data.get(position).getProductID()));
myItems.add(orderView);
calculatePrice(myItems);
binding.itemList.setAdapter(newOrderListAdaptor);
newOrderListAdaptor.notifyDataSetChanged();
alert.dismiss();
}
});
}
答案 0 :(得分:0)
您是否尝试将onItemClick功能添加到适配器?
示例:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code here
}
});
答案 1 :(得分:0)
好的。问题是products_data
列表在您搜索项目时未更新。但是,您正在尝试创建项目表单,而不是更新product_data列表。 此处
OrderView orderView = new OrderView();
orderView.setProduct_name(products_data.get(position).getProductName());
orderView.setProduct_qty(0);
orderView.setProduct_price(products_data.get(position).getPrice());
orderView.setNewItemtotal(Double.parseDouble(products_data.get(position).getPrice()));
orderView.setTotalPrice(products_data.get(position).getPrice());
orderView.setProduct_id(Integer.parseInt(products_data.get(position).getProductID()));
myItems.add(orderView);
如果您可以放下List<Product> products_data = dbHelper.productListView();
在班级内部,然后在搜索项目时尝试对其进行更新。希望这会有所帮助。
这是示例:
products_data = dbHelper.productListView();
final ListView listView;
if (products_data.size() > 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
//insert array to constructor
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.product_list_pop_up, null);
testAdapter = new ProdductPopupAdapter(getContext(), products_data);
listView = dialogLayout.findViewById(R.id.product_list_view);
TextView cancel_btn = dialogLayout.findViewById(R.id.cancel_btn);
final EditText search_view = dialogLayout.findViewById(R.id.list_item_search);
listView.setAdapter(testAdapter);
search_view.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
products_data = dbHelper.searchProductList(s.toString());
testAdapter = new ProdductPopupAdapter(getContext(), products_data);
listView.setAdapter(testAdapter);
testAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
builder.setView(dialogLayout);