gpt4 book ai didi

java - 将 JSON 设置为 EditText

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:46 26 4
gpt4 key购买 nike

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

例如,如果我点击公司列表中的公司 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;
}

我知道这是一篇很长的文章,但我认为我仍然错过了在这里显示的部分代码。请尝试帮我解决这个问题,谢谢:)

最佳答案

Now I have a problem setting text to these custom fields (EditText) in creating a Employee Contact, as these custom fields are not defined in the layout xml... ... How can I setText to these Custom fields Editext?

要在 EditText 中设置文本,您需要在目标 EditText 上调用 setText()。示例:

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

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

// 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 的 super 父级并遍历 View 层次结构即可。在最坏的情况下,您将需要从 Root View 开始工作可以通过调用findViewById(android.R.id.content)获取。

关于java - 将 JSON 设置为 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35215227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com