适配器文本视图恢复为旧值



我遇到了一个奇怪而令人沮丧的问题。 我想根据从基本适配器中的数字选择器获得的值更改文本视图的文本。 我试图直接在文本视图中放置一个值并且它的工作,但是当我显示警报对话框时,文本视图恢复为我在 XML 中输入的值 0。 我不知道为什么会这样。

更新

以下是适配器的完整代码:

public class AddOnsAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
DataBaseHelper db;
int txtAddOnId, txtAddOnOperation;
String add_on_name, add_on_price, order_code;
int numPickVal = 0;
private NumberPicker numPicker;
public AddOnsAdapter(Context context,
                     ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    db = new DataBaseHelper(context);
}
@Override
public int getCount() {
    return data.size();
}
@Override
public Object getItem(int position) {
    return null;
}
@Override
public long getItemId(int position) {
    return 0;
}
private class ViewHolder{
    TextView lblAddOnName, lblPrice, lblQty;
    Button btnModifyAddOn;
    ToggleButton toggleRemove;
}
public View getView(final int position, View convertView, ViewGroup parent) {
   inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final ViewHolder holder = new ViewHolder();
    View itemView = inflater.inflate(R.layout.activity_addons_list, parent, false);
    resultp = data.get(position);
    holder.lblAddOnName = (TextView) itemView.findViewById(R.id.txtAddOns);
    holder.lblPrice = (TextView) itemView.findViewById(R.id.txtPrice);
    holder.lblQty = (TextView) itemView.findViewById(R.id.txtQty);
    holder.btnModifyAddOn = (Button) itemView.findViewById(R.id.btnQty);
    holder.toggleRemove = (ToggleButton) itemView.findViewById(R.id.btnRemove);
    holder.lblAddOnName.setText(resultp.get("desc"));
    add_on_name = resultp.get("desc");
    holder.lblPrice.setText("₱ " + resultp.get("price"));
    add_on_price = resultp.get("price");
    txtAddOnId = Integer.parseInt(resultp.get("id"));
    txtAddOnOperation = Integer.parseInt(resultp.get("operation"));
    order_code = resultp.get("order_code");
    holder.lblQty.setText("0");
    if(txtAddOnOperation == 1){
        holder.toggleRemove.setEnabled(false);
    }else if(txtAddOnOperation == 2){
        holder.btnModifyAddOn.setEnabled(false);
    }else if(txtAddOnOperation == 3){
    }
    holder.toggleRemove.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                TempOrderAddOn tmpOrderAddon = new TempOrderAddOn();
                tmpOrderAddon.setOrderCode(order_code);
                tmpOrderAddon.setAddOnId(txtAddOnId);
                tmpOrderAddon.setAddOnName(add_on_name);
                tmpOrderAddon.setQty(Integer.parseInt(holder.lblQty.getText().toString()));
                tmpOrderAddon.setAddOnPrice(Double.parseDouble(add_on_price));
                db.addToTempOrderAddOn(tmpOrderAddon);
            } else {
                TempOrderAddOn tmpOrderAddOnDel = new TempOrderAddOn();
                tmpOrderAddOnDel.setAddOnId(txtAddOnId);
                tmpOrderAddOnDel.setOrderCode(order_code);
                db.deleteTempOrderAddOn(tmpOrderAddOnDel);
            }
        }
    });
    holder.btnModifyAddOn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            numPicker = new NumberPicker(context);
            numPicker.setMinValue(0);
            numPicker.setMaxValue(99);
            numPicker.setValue(Integer.parseInt(holder.lblQty.getText().toString()));
            numPicker.setWrapSelectorWheel(false);
            numPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                }
            });
            RelativeLayout relative = new RelativeLayout(context);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
            RelativeLayout.LayoutParams numPickerParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            numPickerParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            relative.setLayoutParams(params);
            relative.addView(numPicker, numPickerParams);
            holder.lblQty.setText(""+24);
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Choose quantity");
            builder.setView(relative);
            builder.setCancelable(false);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    holder.lblQty.setText(""+numPicker.getValue());
                    if(numPicker.getValue() == 0){
                        TempOrderAddOn tmpOrderAddOnDel = new TempOrderAddOn();
                        tmpOrderAddOnDel.setAddOnId(txtAddOnId);
                        tmpOrderAddOnDel.setOrderCode(order_code);
                        db.deleteTempOrderAddOn(tmpOrderAddOnDel);
                        holder.lblQty.setText(""+numPicker.getValue());
                    }else{
                        TempOrderAddOn tmpOrderAddon = new TempOrderAddOn();
                        tmpOrderAddon.setOrderCode(order_code);
                        tmpOrderAddon.setAddOnId(txtAddOnId);
                        tmpOrderAddon.setAddOnName(add_on_name);
                        tmpOrderAddon.setQty(numPicker.getValue());
                        tmpOrderAddon.setAddOnPrice(Double.parseDouble(add_on_price));
                        db.addToTempOrderAddOn(tmpOrderAddon);
                        Log.d("log1","numPickVal: "+numPicker.getValue());
                        holder.lblQty.setText(""+numPicker.getValue());
                    }
                    holder.lblQty.setText(""+numPicker.getValue());
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            AlertDialog dlg = builder.create();
            dlg.show();
        }
    });
    return itemView;
}

}

我不知道这是否有帮助,但这是适配器的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txtAddOns"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:textStyle="bold"
            android:textSize="34sp"
            android:text="Add On Name"
            style="@style/RobotoTextViewStyle_normal400" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txtPrice"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:textSize="25sp"
            android:layout_weight="1"
            android:text="0"
            style="@style/RobotoTextViewStyle_light300" />

        <TextView
            android:gravity="right"
            android:id="@+id/txtQty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:textSize="25sp"
            android:layout_weight="1"
            android:text="0"
            style="@style/RobotoTextViewStyle_light300" />
        <Button
            android:layout_marginBottom="5dp"
            android:id="@+id/btnQty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.10"
            android:textSize="20sp"
            android:text="QTY"
            />
        <ToggleButton
            android:layout_marginRight="10dp"
            android:layout_marginBottom="5dp"
            android:id="@+id/btnRemove"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:textSize="20sp"
            android:textOn="Remove"
            android:textOff="Remove" />
    </LinearLayout>
</LinearLayout>

请帮忙

我不确定你是否真的打算

  1. 从 HashMap 移动到 POJO 类。
  2. ListView替换为RecyclerView

但暂时这里是你的解决方案

无论你在哪里都有这样的台词

holder.lblQty.setText(""+0);

holder.lblQty.setText(""+24);

将这些行替换为

resultp.put("Qty", "24");
holder.lblQty.setText(resultp.get("Qty"));

resultp.put("Qty", String.valueOf(numPicker.getValue()));
holder.lblQty.setText(resultp.get("Qty"));

解释

1.正如您所说,您正在使用HashMap绑定数据,但您的数量值实际上并未绑定。

2.这意味着您是在静态基础上设置数据

3.因此,每当您打开警报并关闭它时,您的列表都会刷新,因为数据不是绑定而是静态给出的,因此您的静态数据被重置为TextView

如果您需要更多解释,请告诉我

这是你完整的代码吗?请尝试以下操作!

void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
        R.string..alert_dialog_two_buttons_title);
newFragment.setCancelable(false);
newFragment.show(getFragmentManager(), "dialog");

}

最新更新