我遇到了一個奇怪而令人沮喪的問題。我想根據我從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>
請幫忙
請提供完整的適配器代碼。 –
已經添加了完整的適配器代碼 –
正如我想的那樣,您的適配器已經搞亂了。 –