自定义警报对话框搜索项结果错误
customized alert dialog search item result wrong
我正在创建一个 Android 应用程序。它有一些带有搜索栏的弹出对话框。但问题是:当我使用它搜索某些产品时,该搜索的匹配项目应该添加到弹出对话框内的列表视图中,这很好用。
但是当我选择在弹出对话框中搜索项目的项目时,应该保存在我之前创建的对象 (OrderView object) 中,但是该对象中存储的项目是总是错的 (这种情况只有在我搜索项目时才会发生)。问题发生在这个代码行,
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
因为我正在获取 postion 来识别所选项目。
但我不知道如何将此代码行替换为正确的方式。我真的很感谢你的帮助。谢谢你。
这是我的代码:
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();
}
});
}
您是否尝试将 onItemClick 功能添加到适配器?
示例:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code here
}
});
好的。问题是 products_data
列表在您搜索项目时没有更新。但是您正在尝试获取已创建但未更新的项目表单 products_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();
在您的 class 中,然后尝试在搜索项目时更新它。希望这会有所帮助。
示例如下:
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);
我正在创建一个 Android 应用程序。它有一些带有搜索栏的弹出对话框。但问题是:当我使用它搜索某些产品时,该搜索的匹配项目应该添加到弹出对话框内的列表视图中,这很好用。
但是当我选择在弹出对话框中搜索项目的项目时,应该保存在我之前创建的对象 (OrderView object) 中,但是该对象中存储的项目是总是错的 (这种情况只有在我搜索项目时才会发生)。问题发生在这个代码行,
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
因为我正在获取 postion 来识别所选项目。
但我不知道如何将此代码行替换为正确的方式。我真的很感谢你的帮助。谢谢你。
这是我的代码:
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();
}
});
}
您是否尝试将 onItemClick 功能添加到适配器?
示例:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code here
}
});
好的。问题是 products_data
列表在您搜索项目时没有更新。但是您正在尝试获取已创建但未更新的项目表单 products_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();
在您的 class 中,然后尝试在搜索项目时更新它。希望这会有所帮助。
示例如下:
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);