2016-02-05 65 views
1

我的任務是現有的聯繫人管理應用程序。在這個應用程序,有2個部分,公司名單和員工名單。公司列表顯示公司聯繫人列表,員工列表顯示公司列表中的員工聯繫人列表。setText從JSON的EditText

例如,如果我在公司名單點擊A公司,它就會導致頁面與A公司的員工接觸的另一份名單

創建公司聯繫,有一個稱爲自定義字段的部分有複選框。勾選這些複選框將確定出現什麼樣的自定義字段時創建這個特殊的公司聯繫內部員工聯繫(如性別,訂婚日期等)

現在我有一個問題,將文本這些自定義字段(EditText上)在創建一個僱員聯繫,因爲這些自定義字段在佈局定義XML ,但是在首次創建公司聯繫人時選中的複選框被剝奪。

如何將文本設置爲這些自定義字段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; 
    } 

我知道這是一個很長的帖子,但我想我還是錯過了代碼的一部分顯示在這裏。請嘗試幫我弄明白,謝謝:)

+0

歡迎來到Stackoverflow。請添加您收到的錯誤或例外。 – Daenarys

回答

0

現在我有一個問題,在創建一個Employee Contact的時候給這些自定義字段(EditText)設置文本,因爲這些自定義字段沒有在layout xml中定義。 .. ...我怎樣才能將TextText設置爲這些自定義字段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)來獲得根視圖,其中可以獲得 。

相關問題