我是 android 新手,我不知道如何使用 addView()。我无法找出代码中的错误。(应用程序中的所有其他文件都可以正常工作,除了这个。)不知道将其声明为..请帮助我。
public class example extends Activity {
private static String url = "http://nissarcs.net78.net/android/example.php";
private static final String TAG_Assessments = "Assessments1";
private static final String TAG_CODE = "COURSE";
private static final String TAG_CNH = "COURSE_NAME";
private static final String TAG_CR = "CR";
private static final String TAG_CT = "CT";
JSONArray degree = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablemain);
tl.addView(tr_head.new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
Integer count = 0;
ArrayList<HashMap<String, String>> contactList = new
ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
degree = json.getJSONArray(TAG_Assessments);
// looping through All Contacts
for (int i = 0; i < degree.length(); i++) {
JSONObject c = degree.getJSONObject(i);
// Storing each json item in variable
String code = c.getString(TAG_CODE);
String name = c.getString(TAG_CNH);
String cr = c.getString(TAG_CR);
String ct = c.getString(TAG_CT);
TableRow tr = new TableRow(this);
if (count % 2 != 0) tr.setBackgroundColor(Color.BLACK);
tr.setPadding(0, 0, 0, 2);
tr.setId(100 + count);
tr.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//Create two columns to add as table data
// Create a TextView to add date
TextView labelCR = new TextView(this);
labelCR.setId(200 + count);
labelCR.setText(cr);
labelCR.setPadding(5, 5, 5, 5);
labelCR.setTextColor(Color.BLACK);
labelCR.setBackgroundColor(Color.GRAY);
tr.addView(labelCR);
TextView labelSC = new TextView(this);
labelSC.setId(200 + count);
labelSC.setText(code.toString());
labelSC.setPadding(5, 5, 5, 5);
labelSC.setTextColor(Color.BLACK);
labelSC.setBackgroundColor(Color.LTGRAY);
tr.addView(labelSC);
TextView labelACTIVITY = new TextView(this);
labelACTIVITY.setId(200 + count);
labelACTIVITY.setText(name.toString());
labelACTIVITY.setPadding(5, 5, 5, 5);
labelACTIVITY.setTextColor(Color.BLACK);
labelACTIVITY.setBackgroundColor(Color.GRAY);
tr.addView(labelACTIVITY);
TextView labelSAT = new TextView(this);
labelSAT.setId(200 + count);
labelSAT.setText(cr.toString());
labelSAT.setPadding(5, 5, 5, 5);
labelSAT.setTextColor(Color.BLACK);
labelSAT.setBackgroundColor(Color.LTGRAY);
tr.addView(labelSAT);
// finally add this to the table row
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CODE, code);
map.put(TAG_CNH, name);
map.put(TAG_CR, cr);
map.put(TAG_CT, ct);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}