Android 适配器textview恢复为旧值
我遇到了一个奇怪而令人沮丧的问题。我想根据从baseadapter中的numberpicker获得的值更改textview的文本。我试图直接在textview中输入一个值,但当我显示alertdialog时,textview将恢复为我在xml中输入的值,即0。我不知道为什么会这样 更新 以下是适配器的完整代码:Android 适配器textview恢复为旧值,android,android-adapter,Android,Android Adapter,我遇到了一个奇怪而令人沮丧的问题。我想根据从baseadapter中的numberpicker获得的值更改textview的文本。我试图直接在textview中输入一个值,但当我显示alertdialog时,textview将恢复为我在xml中输入的值,即0。我不知道为什么会这样 更新 以下是适配器的完整代码: public class AddOnsAdapter extends BaseAdapter { Context context; LayoutInflater inflater; Ar
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;
}
公共类AddOnAdapter扩展了BaseAdapter{
语境;
充气机;
阵列列表数据;
HashMap resultp=新的HashMap();
数据库辅助数据库;
int txtAddOnId,txtAddOnOperation;
字符串添加名称、添加价格、订单代码;
int numPickVal=0;
私用号码窃听器;
公共加载项适配器(上下文,
ArrayList(ArrayList){
this.context=上下文;
数据=数组列表;
db=新数据库助手(上下文);
}
@凌驾
public int getCount(){
返回data.size();
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回0;
}
私有类视窗持有者{
text查看lblAddOnName、lblPrice、lblQty;
按钮btnModifyAddOn;
ToggleButton toggleRemove;
}
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
充气器=(充气器)上下文
.getSystemService(上下文布局\充气机\服务);
最终视图持有者=新的视图持有者();
视图项视图=充气机。充气(R.layout.activity\u addons\u list,parent,false);
resultp=data.get(位置);
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=(按钮)itemView.findViewById(R.id.btnQty);
holder.toggleRemove=(ToggleButton)itemView.findViewById(R.id.btnRemove);
holder.lblAddOnName.setText(resultp.get(“desc”);
添加_on_name=resultp.get(“desc”);
holder.lblPrice.setText(“₱”+resultp.get(“价格”);
添加价格=结果获取(“价格”);
txtAddOnId=Integer.parseInt(resultp.get(“id”);
txtAddOnOperation=Integer.parseInt(resultp.get(“操作”);
订单代码=resultp.get(“订单代码”);
持有人lblQty.SETEXT(“0”);
if(txtAddOnOperation==1){
holder.toggleRemove.setEnabled(假);
}else if(txtAddOnOperation==2){
holder.btnModifyAddOn.setEnabled(false);
}else if(txtAddOnOperation==3){
}
holder.toggleRemove.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
检查更改后的公共无效(复合按钮视图,布尔值已检查){
如果(已检查){
TempOrderAddOn tmpOrderAddon=新的TempOrderAddOn();
tmpOrderAddon.setOrderCode(订单代码);
tmpOrderAddon.setAddOnId(txtAddOnId);
tmpOrderAddon.setAddOnName(在名称上添加);
tmpOrderAddon.setQty(Integer.parseInt(holder.lblQty.getText().toString());
tmpOrderAddon.setAddOnPrice(Double.parseDouble(add_on_price));
db.addToTempOrderAddOn(tmpOrderAddon);
}否则{
TempOrderAddOn tmpOrderAddOnDel=新的TempOrderAddOn();
tmpOrderAddOnDel.setAddOnId(txtAddOnId);
tmpOrderAddOnDel.setOrderCode(订单代码);
db.deletemporderaddon(tmpOrderAddOnDel);
}
}
});
holder.btnModifyAddOn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
numPicker=新的NumberPicker(上下文);
numPicker.setMinValue(0);
numPicker.setMaxValue(99);
numPicker.setValue(Integer.parseInt(holder.lblQty.getText().toString());
numPicker.SetWrapsElectrorWheel(假);
numPicker.setOnValueChangedListener(新的NumberPicker.OnValueChangeListener(){
@凌驾
public void onValueChange(NumberPicker选择器、int-oldVal、int-newVal){
}
});
RelativeLayout relative=新的RelativeLayout(上下文);
RelativeLayout.LayoutParams参数=新的RelativeLayout.LayoutParams(50,50);
RelativeLayout.LayoutParams numPickerParams=新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容);
numPickerParams.addRule(父项中的RelativeLayout.CENTER);
相对.setLayoutParams(参数);
addView(numPicker,numPickerParams);
支架LBL数量setText(“+24”);
AlertDialog.Builder=新建AlertDialog.Builder(上下文);
builder.setTitle(“选择数量”);
builder.setView(相对);
builder.setCancelable(false);
setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
holder.lblQty.setText(“+numPicker.getValue());
如果(numPicker.getValue()==0){
TempOrderAddOn tmpOrderAddOnDel=新的TempOrderAddOn();
tmpOrderAddOnDel.setAddOnId(txtAddOnId);
tmpOrderAddOnDel.setOrderCode(订单代码);
db.deletemporderaddon(tmpOrderAddOnDel);
holder.lblQty.setText(“+numPicker.getValue());
}否则{
TempOrderAddOn tmpOrderAddon=新的TempOrderAddOn();
tmpOrderAddon.setOrderCode(订单代码);
tmpOrderAddon.setAddOnId(txtAddOnId);
tmpOrderAddon.setAddOnName(在名称上添加);
tmpOrderAddon.setQty(numPicker.getValue());
<?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>
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string..alert_dialog_two_buttons_title);
newFragment.setCancelable(false);
newFragment.show(getFragmentManager(), "dialog");
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"));