如何从JSONARRAY数据动态添加行到表格布局?

时间:2012-02-15 12:54:54

标签: android tableview

我想动态地将行添加到我的tableview,就像iphone一样,有两个TextView,一个EditText用于编辑数据到webservices,我也想要滚动tableview。因为我可以继续,或者如果有任何改进的解决方案PLZ建议我。

我有这个Json,想要将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帮助实现这个新手知识,所以我需要帮助 任何好友都可以为此解决问题。 提前致谢

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;
}

1 个答案:

答案 0 :(得分: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数据   然后将它存储在String []中,将值分配给表   根据您的要求。例如,请仔细阅读以下内容。

     

活动: -

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;

    }
}
相关问题