2017-03-18 72 views
-1

我遇到了一个奇怪而令人沮丧的问题。我想根据我从baseadapter中的numberpicker获得的值更改textview的文本。我试图直接在textview中放置一个值,但是当我显示alertdialog时,textview恢复为我放入xml的值为0的值。我不知道它为什么会这样。适配器textview恢复到旧值

UPDATE

这里是适配器的全码:

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> 

请帮忙

+0

请提供完整的适配器代码。 –

+0

已经添加了完整的适配器代码 –

+0

正如我想的那样,您的适配器已经搞乱了。 –

回答

0

我不知道你是否真的打算

  1. 移动到POJO类从HashMap中。
  2. 更换ListViewRecyclerView

但目前这里是您的解决方案

在以往任何时候你有喜欢

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

让我知道如果你需要更多的解释

+0

这就是我一直在寻找的。 textview为什么会回到旧文本的原因。非常感谢你@MohammedAtif。 –

0

这是您的完整码吗?尝试以下!

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

}

+0

谢谢你的答案,但我认为这个代码是为了显示一个对话框片段。那不是我的问题。 –

+0

希望你能解决问题 –

+0

谢谢@Nguyen Van uc –