2012-02-15 1 views
0

나는 두 개의 TextViews와 하나의 EditText로 iphone과 같은 내 tableview에 동적으로 행을 추가하고 싶습니다. 또한 webservices에 대한 데이터 편집을 위해 Scrolling tableview를 원합니다. ineed som 샘플 코드 그래서 내가 진행할 수 또는 어떤 alternet 솔루션 plz 날 제안하는 경우.JSONARRAY Data에서 테이블 레이아웃에 동적으로 행을 추가하는 방법은 무엇입니까?

나는이 JSON을 가지고 내가 어떤 친구는 이것에 대한 해결책을 가지고 있습니다 도움이 필요 그래서 안드로이드 새로운이 메신저를 실현하려있는 tableview의 각 행에

try { 
     json = new JSONObject(status); 
     getArray_Meter_Reading = new JSONArray(); 
     getArray_Meter_Reading = json.getJSONArray("meterReadings"); 
     if (getArray_Meter_Reading.length() == 0) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(
        NewTransaction.this); 
      builder.setTitle("WARNING"); 
      builder.setIcon(android.R.drawable.ic_dialog_alert); 
      builder.setMessage("No Meters Found"); 
      builder.setPositiveButton("ok", 
        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, 
           int which) { 


         } 
        }); 

      AlertDialog diag = builder.create(); 
      diag.show(); 
     } 

    } catch (JSONException e) { 


     e.printStackTrace(); 
    } 

Plz은 도움말을 JSON 데이터를 바인딩 할. 사전에 덕분에

public View getView(final int position, View convertView, ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 
    EditText ed_Current = (EditText) view.findViewById(R.id.ed_Current); 
    ed_Current.setTag(position); 






    ed_Current.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

     public void onFocusChange(View arg0, boolean arg1) { 

      if (v == null) { 
       v = arg0; 

       int tag = (Integer) arg0.getTag(); 
       Caption = (EditText) arg0; 

       previous_Meter_Reading = new HashMap<String, String>(); 
       previous_Meter_Reading = c.get(tag); 
       String getPreviousReading = previous_Meter_Reading 
         .get("previousMeterReading"); 
       String CumulativeIndex = previous_Meter_Reading 
         .get("Cumulative"); 
       previousMeterReading = Integer.parseInt(getPreviousReading); 
       Cumulative = Integer.parseInt(CumulativeIndex); 
      } 

      Toast.makeText(context, "getPrevious" + previousMeterReading, 
        Toast.LENGTH_SHORT); 
      Toast.makeText(context, "Cumulative" + Cumulative, 
        Toast.LENGTH_SHORT); 

      Log.i("Hello", "getPrevious" + previousMeterReading); 
      Log.i("Hello1", "Cumulative" + Cumulative); 

      if (v != arg0) { 

       if (!Caption.getText().toString().equals("") 
         && Cumulative == 1) { 

        int tag = ((Integer) arg0.getTag()); 
        CurrentMeterReading = Integer.valueOf(Caption.getText() 
          .toString()); 

        CurrentReading =new HashMap<String, Integer>(); 
        CurrentReading.put("Tag"+tag,CurrentMeterReading); 
        getReading.add(CurrentReading); 

        Log.i("Curr", "Current" + CurrentMeterReading); 
        Log.i("Pre", "previous" + previousMeterReading); 
        if (CurrentMeterReading < previousMeterReading) { 
         AlertDialog.Builder builder = new AlertDialog.Builder(
           context); 
         builder.setTitle("WARNING"); 
         builder.setIcon(android.R.drawable.ic_dialog_alert); 
         builder.setMessage("Please Enter UserName"); 
         builder.setPositiveButton("ok", 
           new DialogInterface.OnClickListener() { 

            public void onClick(
              DialogInterface dialog, 
              int which) { 

             // Caption.requestFocus(); 

            } 
           }); 

         AlertDialog diag = builder.create(); 
         diag.show(); 

         // Caption.requestFocus(); 
         // v = null; 
         // Caption = null; 
        }else if(Cumulative==0 && !Caption.getText().toString().equals("")){ 

         //int tag1 = ((Integer) arg0.getTag()); 
         CurrentMeterReading = Integer.valueOf(Caption.getText() 
           .toString()); 

         CurrentReading =new HashMap<String, Integer>(); 
         CurrentReading.put("Tag"+tag,CurrentMeterReading); 
         getReading.add(CurrentReading); 
        } 

       } 

       v = null; 
       Caption = null; 
      } 

     } 
    }); 

    return view; 
} 

답변

0
Layout:- 
레이아웃을 생성 한 후
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="154dp" 
     android:layout_marginLeft="42dp" 
     android:text="Total" 
     android:textColor="#000000" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView1" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="80dp" 
     android:text="TextView" 
     android:textColor="#000000" /> 

    <TableLayout 
     android:id="@+id/tableprovision" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="74dp" > 
    </TableLayout> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="33dp" 
     android:text="Expenses " 
     android:textColor="#000000" 
     android:textSize="18sp" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/textView3" 
     android:layout_marginRight="30dp" 
     android:text="Add" /> 

</RelativeLayout> 

은 먼저 당신은 테이블에 값을 할당 한 다음 JSON 데이터 를 검색 문자열 []에 저장합니다 귀하의 요구 사항에 에 따라. 예를 들어 다음을 통해 이동하십시오.

활동 : -

public class ProvisionActivity extends Activity { 

    private TableLayout mTable; 
    private static int sCount = 0; 
    Button btnAdd; 
    String[] pnames = { "provision1", "provision2", "provision3", "provision4", 
      "provision5" }; 
    String[] pprice = { "45", "85", "125", "15", "198" }; 
    StringBuffer xpenses; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnAdd = (Button) findViewById(R.id.button1); 
     mTable = (TableLayout) findViewById(R.id.tableprovision); 
     xpenses = new StringBuffer(); 
     btnAdd.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       mTable.addView(addRow(pnames, pprice)); 

       for (int i = 0; i < mTable.getChildCount(); i++) { 
        mTable.getChildAt(i); 

        System.out.println("Table Row values are " 
          + mTable.getChildAt(i)); 

        xpenses = xpenses.append(mTable.getChildAt(i).toString()); 

        System.out 
          .println("Expense Table Values Are After Storing it in a String variable " 
            + xpenses); 

       } 
      } 
     }); 

    } 

    private TableRow addRow(String[] sname, final String[] sprice) { 

     TableRow tr = new TableRow(this); 
     tr.setId(1000 + sCount); 
     tr.setLayoutParams(new TableLayout.LayoutParams(
       TableLayout.LayoutParams.FILL_PARENT, 
       TableLayout.LayoutParams.WRAP_CONTENT)); 
     TableRow.LayoutParams blparams = new TableRow.LayoutParams(150, 35); 
     final Spinner spinner = new Spinner(this); 
     spinner.setLayoutParams(blparams); 
     spinner.setBackgroundColor(Color.DKGRAY); 
     ArrayAdapter<String> xtypeadapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_dropdown_item, sname); 

     spinner.setAdapter(xtypeadapter); 
     tr.addView(spinner); 
     TableRow.LayoutParams tlparams = new TableRow.LayoutParams(55, 
       TableLayout.LayoutParams.WRAP_CONTENT); 
     final TextView textView = new TextView(this); 
     textView.setLayoutParams(tlparams); 
     textView.setTextColor(Color.BLACK); 

     spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

      public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       textView.setText(" " 
         + sprice[spinner.getSelectedItemPosition()]); 

      } 

      public void onNothingSelected(AdapterView<?> arg0) { 

      } 
     }); 
     tr.addView(textView); 
     TableRow.LayoutParams blparams1 = new TableRow.LayoutParams(
       TableRow.LayoutParams.WRAP_CONTENT, 
       TableRow.LayoutParams.WRAP_CONTENT); 
     final Button button = new Button(this); 
     button.setLayoutParams(blparams1); 
     button.setBackgroundColor(Color.LTGRAY); 
     button.setText(" - "); 
     button.setId(2000 + sCount); 
     button.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       mTable.removeView(findViewById(v.getId() - 1000)); 
      } 
     }); 
     tr.addView(button); 
     sCount++; 
     return tr; 

    } 
} 
관련 문제