我使用JSON字符串从我的Web服务器获取数据(见下文)
{"identification":[{"question":"Who created the C programming language?","answers":"Dennis Ritchie","subject":"1","points":"1","question-id":"1"},{"question":"What company created Visual Basic?","answers":"Microsoft","subject":"1","points":"1","question-id":"2"}],"multiplechoice":[{"question":"Which of the following programming languages are not Object Oriented?","answers":"C","choices":["Java","Visual Basic"],"subject":"1","points":"1","question-id":"3"},{"question":"The person who is responsible for the creation of Facebook?","answers":"Mark Zuckerberg","choices":["Steve Jobs","Bill Gates","Larry Page"],"subject":"1","points":"1","question-id":"4"}]}
使用该数据我创建了一个动态组件,例如一些问题有3个选择,因此3个单选按钮有5个选项,5个单选按钮等。
现在主要的问题是我真的不知道如何获得我以编程方式创建的组件的引用,我知道在使用XML时如何使用findViewById,但这次没有使用XML文件,除非对于父布局。
我计划检索并解析我创建的组件的所有值到JSON字符串中。 (我知道如何做而不是这个问题的主要问题)
以下代码负责动态创建组件:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject jObject = new JSONObject(jsonObj.toString().trim());
Iterator<?> keys = jObject.keys();
Log.d("TEST: ", "WORKING> " + "ENABLED");
JSONArray temp_json_arr = null;
JSONObject temp_json_obj = null;
String temp_string = "";
int question_number = 1;
LinearLayout ll = (LinearLayout)findViewById(R.id.test_layout);
while( keys.hasNext() ){
String key = (String)keys.next();
Log.d("TEST: ", "KEYS> " + key);
// Getting JSON Array node
temp_json_arr = jsonObj.getJSONArray(key);
// looping through All Questions
for (int i = 0; i < temp_json_arr.length(); i++) {
Log.d("TEST: ", "EXECUTE> before if condition");
JSONObject q = temp_json_arr.getJSONObject(i);
if( key.equals("identification") ) {
Log.d("TEST: ", "EXECUTE> START");
int element_ctr = 0;
TextView[] question_textview = new TextView[10];
TextView[] label_textview = new TextView[10];
EditText[] answer_edittext = new EditText[10];
TextView[] seperator_textview = new TextView[10];
//question node
question_textview[element_ctr] = new TextView(getApplicationContext());
question_textview[element_ctr].setText(question_number+". "+q.getString("question"));
question_textview[element_ctr].setTextAppearance(getApplicationContext(), R.style.GlobalTextView);
//question_textview[element_ctr].
ll.addView(question_textview[element_ctr]);
//label node
label_textview[element_ctr] = new TextView(getApplicationContext());
label_textview[element_ctr].setText("Answer:");
label_textview[element_ctr].setTextAppearance(getApplicationContext(), R.style.GlobalTextView);
ll.addView(label_textview[element_ctr]);
//answer node
answer_edittext[element_ctr] = new EditText(getApplicationContext());
answer_edittext[element_ctr].setTextAppearance(getApplicationContext(), R.style.GlobalTextView);
ll.addView(answer_edittext[element_ctr]);
if(i!=temp_json_arr.length()-1) {
//seperator
seperator_textview[element_ctr] = new TextView(getApplicationContext());
seperator_textview[element_ctr].setText("--------------------------------------------------");
ll.addView(seperator_textview[element_ctr]);
Log.d("TEST: ", "EXECUTE> END");
}
}
if( key.equals("multiplechoice") ) {
Log.d("TEST: ", "EXECUTE> START");
int element_ctr = 0;
TextView[] question_textview = new TextView[10];
TextView[] label_textview = new TextView[10];
RadioButton[] answer_radiobutton = new RadioButton[10];
TextView[] seperator_textview = new TextView[10];
//question node
question_textview[element_ctr] = new TextView(getApplicationContext());
question_textview[element_ctr].setText(question_number+". "+q.getString("question"));
question_textview[element_ctr].setTextAppearance(getApplicationContext(), R.style.GlobalTextView);
ll.addView(question_textview[element_ctr]);
//label node
label_textview[element_ctr] = new TextView(getApplicationContext());
label_textview[element_ctr].setText("Answer:");
label_textview[element_ctr].setTextAppearance(getApplicationContext(), R.style.GlobalTextView);
ll.addView(label_textview[element_ctr]);
//choices node
JSONArray temp_json_arr2 = null;
temp_json_arr2 = q.getJSONArray("choices");
temp_string = q.getString("answers");
temp_json_arr2.put(temp_string);
Random rnd = new Random();
for (int k = temp_json_arr2.length() - 1; k >= 0; k--)
{
int j = rnd.nextInt(k + 1);
// Simple swap
Object object = temp_json_arr2.get(j);
temp_json_arr2.put(j, temp_json_arr2.get(k));
temp_json_arr2.put(k, object);
}
Log.d("TEST: ", "TRAP> START");
int group_ctr = 0;
RadioButton[] choices_radiobutton = new RadioButton[10];
RadioGroup[] choices_radiogroup = new RadioGroup[10]; //create the RadioGroup
choices_radiogroup[group_ctr] = new RadioGroup(getApplicationContext());
choices_radiogroup[group_ctr].setOrientation(RadioGroup.VERTICAL);
Log.d("TEST: ", "TRAP> END");
for (int t = 0; t < temp_json_arr2.length(); t++) {
choices_radiobutton[t] = new RadioButton(getApplicationContext());
choices_radiobutton[t].setText(temp_json_arr2.getString(t));
choices_radiobutton[t].setTextAppearance(getApplicationContext(), R.style.GlobalTextView);
choices_radiogroup[group_ctr].addView(choices_radiobutton[t]); //the RadioButtons are added to the radioGroup instead of the layout
}
ll.addView(choices_radiogroup[group_ctr]);//you add the whole RadioGroup to the layout
group_ctr++;
if(i!=temp_json_arr.length()-1) {
//seperator
seperator_textview[element_ctr] = new TextView(getApplicationContext());
seperator_textview[element_ctr].setText("--------------------------------------------------");
ll.addView(seperator_textview[element_ctr]);
Log.d("TEST: ", "EXECUTE> END");
}
}
if( key.equals("truefalse") ) {
//Log.d("TEST: ", "hi iam true or false");
}
question_number++;
}//end for
}//end while
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
我很抱歉代码,我刚开始创建Android应用程序,我仍然试图在进入复杂的代码结构之前先解决基本问题,我只是想解决问题我&#39我现在正在处理其他事情。
答案 0 :(得分:0)
虽然一开始可能看起来很复杂,但可能会帮助您提高进一步的要求。
最好创建一个DataStructure(自定义类)来将此json反序列化为Object(请参阅GSON库),然后将其绑定到UI。然后,如果UI更新,则将更新的值分配给Object并发送回服务器(请参阅Retrofit库以便于REST操作)
答案 1 :(得分:0)
一个简单的解决方案是在代码中创建视图/组件时设置ID。
使用此方法。
someView.setId(int);
int必须是正面的。 (任何数字)
设置ID后,您可以在其他地方的代码中引用它。
示例强>
TextView1.setId(10);
TextView tv = (TextView)findViewById(10);