我正在开发一个为审核酒店而创建的应用。我已经就此问了几个问题。再次需要帮助。
这是一种用于根据'问题的类型以编程方式创建布局的方法。从JSON中提取:
public void getQuestion(String page) {
.
.
.
for (int i = 0; i < questionList.size(); i++) {
LL.setId(i);
if (questionList.get(i).categoryName.equalsIgnoreCase("general")) {
TableRow row = new TableRow(getActivity());
row.setId(i);
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//LinearLayout generalLayout = new LinearLayout(AuditForm.this,)
if (questionList.get(i).questionType.equalsIgnoreCase("editText")) {
TextView txtLable = new TextView(getActivity());
txtLable.setId(i);
txtLable.setHeight(110);
txtLable.setPadding(0, 8, 0, 0);
txtLable.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
txtLable.setText(questionList.get(i).questionText);
txtLable.setTag(questionList.get(i).questId);
if (questionList.get(i).questionText.equalsIgnoreCase("Date of inspection(dd-mm-yyyy)")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3) edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
edtdateBox.setText(inspctDate);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else if (questionList.get(i).questionText.equalsIgnoreCase("This Audit Is For City:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.selectedCity);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
} else if (questionList.get(i).questionText.equalsIgnoreCase("This Audit Is For Property:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.selectedProperty);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else if (questionList.get(i).questionText.equalsIgnoreCase("Property Address:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.propAddress);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else {
EditText edtBox = new EditText(getActivity());
edtBox.setHeight(70);
edtBox.setPadding(2, 5, 3, 3);
edtBox.setId(edtId);
edtBox.setPadding(5, 0, 0, 3);
edtBox.setTag(questionList.get(i).questionText);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtBox);
LL.addView(v);
edtId++;
}
.
.
.
.
//Similar code for other types of questions (like spinner, ratingBar etc.)
}
上述方法中的视图用于此方法:
private void loopQuestions(ViewGroup parent) {
JSONArray ansArry = new JSONArray();
JSONObject params;
String inspectedDate = "", InspectedBy = "" , prop_city = "", prop_Name = "" ;
// Property p = null;
try {
//loop1
Log.e("Total Child", parent.getChildCount() + "");
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
String header =""; // getHeader(i);
if (child instanceof LinearLayout) {
//Support for linearlayout
ViewGroup llchild = (LinearLayout) child;
//loop 2
for (int j = 0; j < llchild.getChildCount(); j++) {
View subChild = llchild.getChildAt(j);
params = new JSONObject();
if (subChild instanceof EditText) {
nosOfQustion++;
EditText edtText = (EditText) subChild;
//USING THE BELOW LINE PREVENTS CRASHING OF APP
if (edtText.getTag() == null){ break;}
if (edtText.getTag().toString().equalsIgnoreCase("Inspected By")) {
InspectedBy = getText(edtText.getText().toString().trim());
} else {
params.put("Question", edtText.getTag() + "");
params.put("Answer", getText(edtText.getText().toString()));
params.put("Category", header);
ansArry.put(params);
}
。 。
我在上面的方法评论中指出使用`&#34; if(edtText.getTag()== null){break;}&#34;防止应用程序崩溃。但问题在于&#39; edtTxt&#39;仍然为空,因此我无法执行保存审核等任务。
此外,上述方法是在一个片段中创建的,用作TabLayout中的Tab。我在onViewCreated()方法中调用了第一个方法(getQuestion())。试图在onCreateView()中调用它,但在这样做时得到了NullPointerException。
请帮忙!
答案 0 :(得分:0)
如果您想从edittext文本中获取文本,请使用下面的代码。
edtText.getText().toString();
对于编辑文本的比较值,请使用以下代码。
String edtText = edtAns.getText().toString();
if (edtText.isEmpty()) {
Toast.makeText(QuestionActivity.this, "Please enter correct value.", Toast.LENGTH_SHORT).show();
}