2013-08-23 6 views
0

Je suis nouveau à android et je ne sais pas comment utiliser addView(). Je ne suis pas capable de comprendre l'erreur dans le code. (Tous les autres fichiers dans l'application fonctionnent correctement, sauf celui-ci.) tl.addView (tr, nouveaux TableLayout.LayoutParams ( Ceci est la ligne où je reçois l'erreur et il dit: « t1 ne peut être résolu en tant que variable. Et Je n'ai aucune idée de quoi déclarer comme .. S'il vous plaît aidez-moiErreur de syntaxe dans addView() android java

package kishita.learning; 
import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 



import android.app.ActionBar.LayoutParams; 
import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class example extends Activity { 
/** Called when the activity is first created. */ 
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(); 
} 
} 
} 
+1

indentation correcte serait bien. – ChiefTwoPencils

+0

qu'est-ce que t1, écrire la définition de t1 –

Répondre

0

tl ici ressemble à un TableLayout

Essayez d'utiliser:.

TableLayout tl = (TableLayout)findViewById(R.id.tablemain); 
0
question

est

tl.addView(tr_head,new TableLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT));