适配器textview恢复旧值

时间:2017-03-18 01:22:21

标签: android android-adapter

我遇到了一个奇怪且令人沮丧的问题。我想根据我从baseadapter中的数字选择器获得的值来更改textview的文本。我试图直接在textview中放一个值并且它正在工作但是当我显示一个alertdialog时,textview恢复到我在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>

请帮助

2 个答案:

答案 0 :(得分:0)

这是您的完整代码吗?请尝试以下方法!

if (v.equals(endGame)) { //if "end game" button was clicked
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
        Toast t = Toast.makeText(getApplicationContext(), "Thanks for playing", Toast.LENGTH_LONG);
        t.show();
    }, 2000, 2000);

    System.exit(0); //quits the app

}

答案 1 :(得分:0)

我不确定你是否真的计划

  1. 从HashMap移至POJO类。
  2. ListView替换为RecyclerView
  3. 但暂时在这里是你的解决方案

    你在哪里有像

    这样的行
    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

    < / p>

      

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