2011-12-03 44 views
5

ho una stringa JSON restituito dal mio servizio web come segue:popolamento Tabella della Struttura utilizzando JSON String

{ "checkrecord" [{ "rollno": "ABC2", "percentuale": 40 ", cui hanno partecipato ": 12," mancato ": 34}]," Tabella1 ": []}

La stringa sopra rappresenta il mio set di dati. Ho convertito la stringa in un oggetto JSON e ora voglio inserire i dati nella stringa in un TableLayout.

Voglio che il tavolo per essere come questo, quando visualizzato su App Android:

rollno  percentage attended missed 
    abc2   40    12  34 

Questo è il codice che sto usando fin d'ora:

//json processing code 

public void jsonprocessing(String c) 
    { 
     try 
     { 

     JSONObject jsonobject = new JSONObject(c); 

     JSONArray array = jsonobject.getJSONArray("checkrecord"); 

     int max = array.length(); 

     for (int j = 0; j < max; j++) 
     { 

    JSONObject obj = array.getJSONObject(j); 
    JSONArray names = obj.names(); 

    for (int k = 0; k < names.length(); k++) 
     { 
      name = names.getString(k); 
      value= obj.getString(name); 
       createtableLayout(); 

     } 


     }  

    } 

    catch (JSONException e)   
    {     
     // TODO Auto-generated catch block 
    e.printStackTrace();   
} 

} 

//create table layout 
public void createtableLayout() 
{ 

    Log.d("values",value); //I am able to see the values abc2,40,12 and 34 over here in logcat 
    TableLayout t= (TableLayout)findViewById(R.id.tablelayout); 
     TableRow r1=new TableRow(this); 
     r1.setLayoutParams(new LayoutParams(
     LayoutParams.FILL_PARENT, 
     LayoutParams.WRAP_CONTENT));  
     TextView t1 = new TextView(this); 
     t1.setText(value); 
     t1.setTextColor(Color.BLACK); 
     t1.setLayoutParams(new LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT)); 
     r1.addView(t1); 
     t.addView(r1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

} 

mio TableLayout contiene un pulsante e un EditText come primo TableRow. Dopo aver fatto clic sul pulsante, ottengo la stringa JSON. Quindi ho bisogno di inserire 2 TableRows dinamicamente dopo quello secondo il mio requisito.

mio XML a partire da ora è la seguente:

<?xml version="1.0" encoding="utf-8"?> 
    <TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tablelayout" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:background="#000044"> 
    <TableRow> 

    <EditText 
     android:id="@+id/edittext" 
     android:width="200px" /> 

    <Button 
     android:id="@+id/button" 
     android:text="Check Record"/> 

    </TableRow> 

    </TableLayout> 

Quindi il mio problema è come aggiungere quei 2 TableRows in più in modo da li popolano con i valori di stringa?

Il mio codice non funziona correttamente. Non sono in grado di ottenere l'output richiesto.

Qualcuno può aiutarmi in questo?

Grazie

+1

Perché non provare per listview multicolumn invece di tablelayout – Abhi

+0

puoi dare un esempio di listview a più colonne? –

+0

http://www.google.co.in/#sclient=psy-ab&hl=it&source=hp&q=multicolon+listview+in+android&pbx=1&oq=multicolon+listview+in+android&aq=f&aqi=q-w1&aql=&gs_sm= e & gs_upl = 1166l20720l0l20855l66l46l16l0l0l8l1628l26087l2-5.5.11.10.5.4.2l58l0 & bav = on.2, or.r_gc.r_pw., cf.osb & fp = 105f40160a393e37 & biw = 1024 & bih = 653 – Abhi

risposta

3

è possibile utilizzare ListView multicolonna per il vostro scopo,

analizzare la risposta JSON e ottenere i valori in diverse stringhe e usare questo blog per il vostro scopo

Multicolumn Listview

2

È, probabilmente, possono definire quei 2 più righe nella vostra XML e renderli inizialmente invisibile. Quindi, dopo aver ricevuto i tuoi dati, inserisci i tuoi dati in quelle file e rendili visibili.

+0

ok bene e dovrei renderli visibili all'interno di createtablelayout metodo giusto? –

+0

Penso di sì, qualcosa come View v = findViewById (R.id.viewid); v.setVisibility (View.Visible) –

1
// Every UI object must have a layout parameter 
     int widthContent = RelativeLayout.LayoutParams.WRAP_CONTENT; 
     int heightContent = RelativeLayout.LayoutParams.WRAP_CONTENT; 

     // Create relative layout 
     RelativeLayout.LayoutParams rlParamsName = new RelativeLayout.LayoutParams(widthContent,heightContent); 

     // Create table row layout 
     int trWidthContent = TableRow.LayoutParams.WRAP_CONTENT; 
     int trHeightContent = TableRow.LayoutParams.WRAP_CONTENT; 

     TableRow.LayoutParams trLayout = new TableRow.LayoutParams(trWidthContent, trHeightContent); 

     // Create a linear layout to add new object as vertical 
     LinearLayout objLinearLayout = new LinearLayout(context); 
     objLinearLayout.setOrientation(LinearLayout.VERTICAL); 

     // Add number of categories to menu 
     for (int i = 0; i < SpecialCategories.length(); i++) { 

      try { 

       // Json for special categories 
       JSONObject jsonData = SpecialCategories.getJSONObject(i); 

       // Get name and count of category 
       String specialCategoryNames = jsonData.getString("label").toString(); 
       String specialCategoryCount = jsonData.getString("coupon_count").toString(); 

       final TableRow tr = new TableRow(this); 
       tr.setLayoutParams(trLayout); 
       tr.setId(i); 
       //tr.setBackgroundColor(0xFFBEBEBE);  

       // What is the text view for category name 
       TextView categoryName = new TextView(this); 
       categoryName.setPadding(8, 8, 8, 8); 
       categoryName.setTextSize(18); 
       categoryName.setLayoutParams(trLayout);   
       categoryName.setText(specialCategoryNames); 
       categoryName.setId(i); 
       categoryName.setTypeface(null, Typeface.BOLD); 
       categoryName.setTextColor(0xFF000000); 

       // What is count for category 
       TextView categoryCount = new TextView(this); 
       categoryCount.setPadding(7, 8, 8, 8); 
       categoryCount.setLayoutParams(trLayout); 
       categoryCount.setBackgroundColor(Color.WHITE); 
       categoryCount.setText("("+specialCategoryCount+")"); 

       // Add disclosure image to row 
       ImageView objDrawLineImageView = new ImageView(this); 
       objDrawLineImageView.setPadding(280, 19, 6, 6); 
       objDrawLineImageView.setImageResource(R.drawable.disclosure); 
       objDrawLineImageView.setLayoutParams(trLayout); 

       // Add name of category 
       tr.addView(categoryName); 

       // Add count for category 
       tr.addView(categoryCount); 

       // Add disclosure back to table row 
       tr.addView(objDrawLineImageView); 

       // Add table row into linear layout 
       objLinearLayout.addView(tr); 


       //Add Row End Line to Linear layout 
       ImageView rowEndLine = new ImageView(context); 
       LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,2); 
       params.setMargins(0, 5, 0, 0); 
       rowEndLine.setLayoutParams(params); 
       rowEndLine.setBackgroundColor(0xFFBEBEBE); 

       // Add end line image to linear layout 
       objLinearLayout.addView(rowEndLine); 

       // What happen when user click on table row 
       tr.setOnClickListener(new OnClickListener() {      
        @Override 
        public void onClick(View arg0) { 

         Toast.makeText(Main.this, ""+tr.getId(),Toast.LENGTH_SHORT).show(); 

         Log.e("TAG", " clicked ID: "+tr.getId()); 
        } 
       }); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      }// end try 

     }// end for SpecialCategories.length() 

     //Add button number two to the activity. 
     RelativeLayout objRelativeLayout = (RelativeLayout)findViewById(R.id.row_special);  
     objRelativeLayout.addView(objLinearLayout); 
Problemi correlati