qq_27074467
qq_27074467
采纳率100%
2016-06-30 09:33

代码问题,为什么我每次获取到的EditText都一样啊!烦请大神指点下

3
已采纳

package com.shiyu.httpclient.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shiyu.httpclient.R;
import com.shiyu.httpclient.httpclient.OkHttpClientManager;
import com.shiyu.httpclient.httpclient.Url;
import com.shiyu.httpclient.moudle.ToastUtil;
import com.shiyu.httpclient.moudle.bean.ReturnInfo;
import com.shiyu.httpclient.moudle.bean.WeightInfo;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

  • Created by maxb on 2016/5/31.
    */
    public class RegistryFormActivity extends BaseActivity implements View.OnClickListener {
    private EditText weigh, et_return, paracelNum,e;
    private TextView bt_overweight, bt_commit;
    private Button addedit;
    private Map map;
    private Map[] parce;
    private List adds;
    private EditText[] addedittext;
    private LinearLayout linearLayout;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
    setContentView(R.layout.activity_registry_form);
    et_return = (EditText) findViewById(R.id.et_return);
    bt_overweight = (TextView) findViewById(R.id.bt_overweigh);
    addedit = (Button) findViewById(R.id.bt_adds);
    addedit.setOnClickListener(this);
    linearLayout = (LinearLayout) findViewById(R.id.line);
    bt_overweight.setOnClickListener(this);
    adds = new ArrayList();
    bt_commit = (TextView) findViewById(R.id.bt_commit);
    bt_commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bt_overweigh:
    postOverweight();
    break;
    case R.id.bt_commit:
    postReturn();
    break;
    case R.id.bt_adds:
    addEditText();
    break;
    }
    }

    //动态添加一组控件
    private void addEditText() {
    addedittext = new EditText[9];
    for (int i = 0; i < 2; i++) {
    e = new EditText(getContext());
    addedittext[i] = e;
    adds.add(i, addedittext[i].getText().toString());
    linearLayout.addView(addedittext[i]);
    }
    }
    //取控件的text.tostring()
    private void prifit_add() {
    map = new HashMap();
    List addsss = new ArrayList();
    Map parcelnum = new HashMap();

    for (int i = 0; i < adds.size(); i++) {
        if (addedittext[i].getText().length() != 0) {
            parcelnum.put("parcelNum", addedittext[i].getText().toString());
            parce = new Map[]{parcelnum};
        }
    }
    map.put("containedParcels", parce);
    

    }

    private void postReturn() {
    prifit_add();
    OkHttpClientManager.arrayPostAsyn(Url.URL_RETURN, new OkHttpClientManager.ResultCallback() {
    @Override
    public void onError(Request request, Exception e) {

        }
    
        @Override
        public void onResponse(ReturnInfo response) {
            switch (response.getStatus()) {
                case "success":
                    ToastUtil.showMessage(R.string.result_ok, 500);
                    break;
                case "failure":
                    ToastUtil.showMessage(R.string.result_reson);
                    break;
                default:
                    ToastUtil.showMessage(R.string.result_false);
                    break;
    
            }
        }
    
    }, new Gson().toJson(map));
    

    }
    }

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • nanjun520 nanjun520 5年前

    看你的代码里面 e = new EditText(getContext());
    addedittext[i] = e;
    adds.add(i, addedittext[i].getText().toString()); 你的e都没有赋值Tex吧? 你试一下在你 addEditText函数你们 添加一下
    e.setText("test "+i);
    也就是

    e = new EditText(getContext());
    e.setText("test "+i);
    addedittext[i] = e;
    adds.add(i, addedittext[i].getText().toString());

    点赞 评论 复制链接分享