从JSON

时间:2016-02-05 02:17:32

标签: java android json android-edittext

我的任务是使用现有的联系人管理应用程序。在这个应用程序中,有2个部分,公司列表和员工列表。公司列表显示公司联系人列表,员工列表显示公司列表中的员工联系人列表。

例如,如果我点击公司列表中的公司A,它将会显示一个页面,其中包含另一个公司A的员工联系人列表。

创建 新的公司联系人时,会出现一个名为自定义字段的部分,其中包含复选框。选中这些复选框将确定在此特定公司联系人中 创建 员工联系人时将显示的自定义字段(例如,性别,参与日期等)

现在我在 创建 员工联系人中为这些自定义字段(EditText)设置文本时出现问题,因为这些自定义字段为<布局xml中的strong> 已定义 ,但是从首次创建公司联系人时选中的复选框中删除了。

如何将setText设置为这些自定义字段Editext?我一直在看代码,但无法理解。

以下是CreateContactActivity.java中的相关代码:

class GetCustomList extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(CreateContactActivityOCR.this);
            progressDialog.setMessage("Loading.. Please wait...");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            try {
                CustomFieldsList = startData();
                Log.d("customfieldlist gettask",
                        Integer.toString(CustomFieldsList.size()));
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            for (int b = 0; b < CustomFieldsList.size(); b++) {
                CustomFields field = CustomFieldsList.get(b);
                Log.d("fieldtype", field.getFieldType());
                if (!field.getFieldType().equals("image")) {
                    TextView myTextView = new TextView(
                            CreateContactActivityOCR.this);
                    if (field.getRequired() == 1) {
                        myTextView.setText("(*) " + field.getFieldName());
                    } else {
                        myTextView.setText(field.getFieldName());
                    }

                    myTextView.setBackgroundColor(getResources().getColor(
                            R.color.lightBlue));
                    myTextView.setPadding(28, 8, 8, 8);
                    myTextView.setTextAppearance(CreateContactActivityOCR.this,
                            android.R.style.TextAppearance_Medium);
                    myTextView.setTextColor(getResources().getColor(
                            R.color.white));
                    // myTextView.setId(field.getFieldID());
                    myTextView.setTag(field.getFieldID());
                    myTextView.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
                    myLayout.addView(myTextView);

                    View viewA = new View(CreateContactActivityOCR.this);
                    float dp = TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, 2, getResources()
                                    .getDisplayMetrics());
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.FILL_PARENT, (int) dp);
                    lp.setMargins(30, 0, 0, 0);
                    viewA.setLayoutParams(lp);
                    viewA.setBackgroundColor(getResources().getColor(
                            R.color.grey));
                    myLayout.addView(viewA);
                }
                if (field.getFieldType().equals("text")) {
                    Log.d("fieldtypeinsideif" + field.getFieldID(),
                            field.getFieldType());
                    EditText myEditText = new EditText(
                            CreateContactActivityOCR.this);
                    myEditText.setText(field.getDefaultValue());
                    myEditText.setSingleLine();
                    myEditText.setId(field.getFieldID());
                    myEditText.setTag(field.getFieldID());
                    myEditText.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));

                    myLayout.addView(myEditText);
                }
                if (field.getFieldType().equals("textarea")) {
                    Log.d("fieldtypeinsideif" + field.getFieldID(),
                            field.getFieldType());
                    EditText myEditText = new EditText(
                            CreateContactActivityOCR.this);
                    myEditText.setText(field.getDefaultValue());
                    myEditText.setMinLines(field.getNoOfRows());
                    myEditText.setId(field.getFieldID());
                    myEditText.setTag(field.getFieldID());
                    myEditText.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));

                    myLayout.addView(myEditText);
                }
                if (field.getFieldType().equals("number")) {
                    Log.d("fieldtypeinsideif" + field.getFieldID(),
                            field.getFieldType());
                    EditText myEditText = new EditText(
                            CreateContactActivityOCR.this);
                    myEditText.setText(field.getDefaultValue());
                    myEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
                    myEditText.setId(field.getFieldID());
                    myEditText.setTag(field.getFieldID());
                    myEditText.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));

                    myLayout.addView(myEditText);
                }
                if (field.getFieldType().equals("dropdown")) {
                    Log.d("fieldtypeinsideif" + field.getFieldID(),
                            field.getFieldType());
                    Spinner spinner = new Spinner(CreateContactActivityOCR.this);
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            CreateContactActivityOCR.this,
                            android.R.layout.simple_spinner_item,
                            field.getValueLists());
                    // Specify the layout to use when the list of choices
                    // appears
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    // Apply the adapter to the spinner
                    spinner.setAdapter(adapter);
                    spinner.setId(field.getFieldID());
                    spinner.setTag(field.getFieldID());

                    myLayout.addView(spinner);
                }
                if (field.getFieldType().equals("checkbox")) {
                    Log.d("fieldtypeinsideif" + field.getFieldID(),
                            field.getFieldType());
                    for (int d = 0; d < field.getValueLists().size(); d++) {
                        CheckBox myCb = new CheckBox(CreateContactActivityOCR.this);
                        myCb.setText(field.getValueLists().get(d));
                        myCb.setId(Integer.parseInt(Integer.toString(d) + "000")
                                + field.getFieldID());
                        myCb.setTag(field.getFieldID());
                        myCb.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT));

                        myLayout.addView(myCb);
                    }
                }
                if (field.getFieldType().equals("radiobutton")) {
                    Log.d("fieldtypeinsideif" + field.getFieldID(),
                            field.getFieldType());
                    RadioGroup rg = new RadioGroup(CreateContactActivityOCR.this);
                    rg.setId(field.getFieldID());
                    for (int d = 0; d < field.getValueLists().size(); d++) {
                        RadioButton myRb = new RadioButton(
                                CreateContactActivityOCR.this);
                        myRb.setText(field.getValueLists().get(d));
                        Log.d("radioButton text", field.getValueLists().get(d));
                        myRb.setTag(field.getFieldID());
                        if (d == 0) {
                            myRb.setChecked(true);
                        }
                        if (Integer.toString(
                                field.getValueLists().get(d).hashCode())
                                .charAt(0) == '-') {
                            myRb.setId(Integer.parseInt(Integer.toString(
                                    field.getValueLists().get(d).hashCode())
                                    .substring(1)));
                        } else {
                            myRb.setId(field.getValueLists().get(d).hashCode());
                        }
                        Log.d("radiobutton id",
                                Integer.toString(field.getValueLists().get(d)
                                        .hashCode()));
                        myRb.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT));
                        rg.addView(myRb);

                    }
                    myLayout.addView(rg);
                }
                if (field.getFieldType().equals("date")) {
                    Log.d("fieldtypeinsideif" + field.getFieldID(),
                            field.getFieldType());

                    DatePicker myDP = new DatePicker(CreateContactActivityOCR.this);
                    myDP.setCalendarViewShown(false);
                    myDP.setId(field.getFieldID());
                    Calendar c = Calendar.getInstance();
                    if (!field.getValueLists().get(3).isEmpty()) {
                        Log.d("min date", field.getValueLists().get(3));
                        c.set(Integer.parseInt(field.getValueLists().get(3)),
                                0, 1);
                        myDP.setMinDate(c.getTimeInMillis());
                    }
                    if (!field.getValueLists().get(4).isEmpty()) {
                        Log.d("max date", field.getValueLists().get(4));
                        c.set(Integer.parseInt(field.getValueLists().get(4)),
                                11, 31);
                        myDP.setMaxDate(c.getTimeInMillis());
                    }

                    // myDP.setMinDate(minDate)
                    myDP.setTag(field.getFieldID());
                    myDP.setLayoutParams(new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));

                    myLayout.addView(myDP);
                }

                if (field.getFieldType().equals("image")) {
                    imageList.add(field);
                }

            }
            for (int a = 0; a < imageList.size(); a++) {
                final CustomFields field = imageList.get(a);

                TextView myTextView = new TextView(CreateContactActivityOCR.this);
                myTextView.setText(field.getFieldName());
                myTextView.setBackgroundColor(getResources().getColor(
                        R.color.lightBlue));
                myTextView.setPadding(28, 8, 8, 8);
                myTextView.setTextAppearance(CreateContactActivityOCR.this,
                        android.R.style.TextAppearance_Medium);
                myTextView.setTextColor(getResources().getColor(R.color.white));
                // myTextView.setId(field.getFieldID());
                myTextView.setTag(field.getFieldID());
                myTextView.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                myLayout.addView(myTextView);

                View viewA = new View(CreateContactActivityOCR.this);
                float dp = TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, 2, getResources()
                                .getDisplayMetrics());
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT, (int) dp);
                lp.setMargins(30, 0, 0, 0);
                viewA.setLayoutParams(lp);
                viewA.setBackgroundColor(getResources().getColor(R.color.grey));
                myLayout.addView(viewA);
                ImageView myImageView = new ImageView(
                        CreateContactActivityOCR.this);
                LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                lp3.setMargins(60, 20, 60, 0);
                myImageView.setLayoutParams(lp3);
                myImageView.setId(field.getFieldID());
                myImageView.setVisibility(View.INVISIBLE);
                myLayout.addView(myImageView);

                imageButton = new Button(CreateContactActivityOCR.this);

                imageButton.setText("Choose Image");

                LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                lp1.setMargins(60, 30, 60, 30);
                imageButton.setLayoutParams(lp1);
                imageButton.setBackgroundColor(getResources().getColor(
                        R.color.lightBlue));
                imageButton.setWidth(300);
                imageButton
                        .setTextColor(getResources().getColor(R.color.white));
                imageButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        buttonFieldID = field.getFieldID();
                        selectImage();
                    }
                });

                myLayout.addView(imageButton);

            }
            progressDialog.dismiss();
        }

    }

    public ArrayList<CustomFields> startData() {
        int success;

        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("userID", userid));
            params.add(new BasicNameValuePair("listID", listid));
            JSONParser jParserCustomField = new JSONParser();
            JSONObject jsonObjectC = jParserCustomField
                    .makeHttpRequest(Constant.URL
                            + "RetrieveCustomFieldsSVC.php", "GET", params);
            success = jsonObjectC.getInt(Constant.TAG_SUCCESS);
            Log.d("customfields", jsonObjectC.toString());
            Log.d("createcontact", Integer.toString(success));
            if (success == 1) {
                JSONArray jsonArray = jsonObjectC.getJSONArray("CustomFields");

                for (int i = 0, length = jsonArray.length(); i < length; i++) {
                    JSONObject attribute = jsonArray.getJSONObject(i);
                    Log.d("createcontact", "aaaa");
                    int dBFieldID = attribute.getInt(Constant.TAG_FIELDID);
                    String dBName = attribute.getString(Constant.TAG_FIELDNAME);
                    String dBFieldType = attribute
                            .getString(Constant.TAG_FIELDTYPE);
                    String dBFieldDefault = attribute
                            .getString(Constant.TAG_FIELDEFAULT);
                    int dBRequired = attribute.getInt(Constant.TAG_REQUIRED);
                    Log.d("createcontact", "bbbbbbbbbb");
                    CustomFields customObj = new CustomFields(dBFieldID,
                            dBName, dBFieldType, dBFieldDefault, dBRequired);
                    Log.d("createcontact", "cccccccccc");

                    JSONObject dBFieldSetting = attribute
                            .getJSONObject(Constant.TAG_FIELDSETTINGS);
                    if (dBFieldSetting.has("ApplyDefault")) {
                        customObj.setApplyDefault(dBFieldSetting
                                .getString("ApplyDefault"));
                    }

                    if (dBFieldSetting.has("FieldLength")) {
                        if (dBFieldSetting.getString("FieldLength").isEmpty() == false) {
                            customObj.setFieldLength(dBFieldSetting
                                    .getInt("FieldLength"));
                        } else {
                            customObj.setFieldLength(0);
                        }

                    }

                    if (dBFieldSetting.has("MaxLength")) {
                        if (dBFieldSetting.getString("FieldLength").isEmpty() == false) {
                            customObj.setMaxLength(dBFieldSetting
                                    .getInt("MaxLength"));
                        } else {
                            customObj.setMaxLength(0);
                        }
                    }

                    if (dBFieldSetting.has("MinLength")) {
                        if (dBFieldSetting.getString("MinLength").isEmpty() == false) {
                            customObj.setMinLength(dBFieldSetting
                                    .getInt("MinLength"));
                        } else {
                            customObj.setMinLength(0);
                        }
                    }

                    if (dBFieldSetting.has("Rows")) {
                        if (dBFieldSetting.getString("Rows").isEmpty() == false) {
                            customObj
                                    .setNoOfRows(dBFieldSetting.getInt("Rows"));
                        } else {
                            customObj.setNoOfRows(0);
                        }
                    }

                    if (dBFieldSetting.has("Columns")) {
                        if (dBFieldSetting.getString("Columns").isEmpty() == false) {
                            customObj.setNoOfColumns(dBFieldSetting
                                    .getInt("Columns"));
                        } else {
                            customObj.setNoOfColumns(0);
                        }
                    }

                    if (dBFieldSetting.has("Key")) {
                        JSONArray keyArray = dBFieldSetting.getJSONArray("Key");
                        ArrayList<String> keyArrayList = new ArrayList<String>();
                        Log.d("arraylistSize",
                                Integer.toString(keyArray.length()) + dBFieldID);
                        for (int a = 0, lengthA = (keyArray.length()); a < lengthA; a++) {
                            Log.d("Key", Integer.toString(a));
                            Log.d("Key", keyArray.get(a).toString());

                            keyArrayList.add(keyArray.get(a).toString());
                        }
                        customObj.setValueLists(keyArrayList);
                        keyArrayList = null;
                    }

                    CustomFieldsList.add(customObj);
                    Log.d("createcontact", "fffffff");
                    customObj = null;
                }

            }
            jsonObjectC = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return CustomFieldsList;
    }

我知道这是一个很长的帖子,但是我想我仍然会错过这里展示的部分代码。请尽量帮我搞清楚,谢谢:)

1 个答案:

答案 0 :(得分:0)

  

现在我在创建Employee Contact时将文本设置为这些自定义字段(EditText)时出现问题,因为这些自定义字段未在布局xml中定义... ...如何将setText设置为这些自定义字段Editext?

要在EditText中设置文字,您需要在目标setText()上致电EditText。例如:

EditText myEditText = findViewById(R.id.my_edit_text);
myEditText.setText("Hello world");

您需要获取目标EditText的引用才能为其设置文本。如果由于某些原因您没有要使用的视图ID,则需要从其父级查找EditText视图。例如:

// You do not have the view id of EditText but you know that the targeted 
// EditText is in a LinearLayout with id R.id.my_linear_layout
LinearLayout myLinearLayout = findViewById(R.id.my_linear_layout);

// This will give you the number of child views in myLinearLayout and
// the targetted EditText is one of the child view.
int childCount = myLinearLayout.getChildCount();
for(int i=0; i<childCount; i++){
    View v = myLinearLayout.getChildAt(i);

    // Test if v is the target EditText. You will have to find a way
    // to test this yourself. If you know that there is only a single
    // EditText in myLinearLayout, then you can simply test if v is
    // an instance of EditText.
}

即使您无权访问EditText的直接父级,也可以使用此工作。您只需要抓住EditText的超级父级并完成视图层次结构。在最坏的情况下,您需要从根视图中工作 可以通过致电findViewById(android.R.id.content)获得。