如何从JSON中添加表中的行

时间:2013-05-06 14:42:33

标签: android android-tablelayout

由于我还在学习Android,我认为我的问题很容易。 我正在尝试显示一个包含来自mysql的信息标记的表。 例如,我想显示客户端排序的命令。如果他订购了一种产品,我将有一排,所以我将拥有与他订购的产品数量一样多的行。我真的不知道该怎么做,而且我没有在网上找到解决方案。 (表格中的所有方框都可以编辑)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/degrade"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="111dp" >

    <Button
        android:id="@+id/ButtonMenu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/menu"
        android:textColor="#ffffff"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/app_name"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="250dp"
        android:text="@string/mapp_net_crm"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="60sp" />

    <Button
        android:id="@+id/ButtonLogoff"
        android:layout_width="108dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="150dp"
        android:background="@drawable/logout"
        android:textColor="#ffffff"
        android:textSize="40sp" />
</LinearLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.67" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:background="@drawable/tableborder" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/commandProductname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/productName"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/commandProductQuantity"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:text="@string/quatity"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/commandPrice"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:text="@string/price"
                android:textSize="20dp" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>

    </TableLayout>

    <Button
        android:id="@+id/exitCommandDetail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/bachToCommand"
        android:layout_below="@+id/bachToCommand"
        android:layout_marginTop="36dp"
        android:background="@drawable/green"
        android:text="@string/exit" />

    <Button
        android:id="@+id/bachToCommand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tableLayout1"
        android:layout_marginRight="24dp"
        android:layout_marginTop="34dp"
        android:background="@drawable/green"
        android:text="@string/backToCommand" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/bachToCommand"
        android:text="ToggleButton" />

    <Button
        android:id="@+id/editCommand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/exitCommandDetail"
        android:layout_below="@+id/exitCommandDetail"
        android:layout_marginRight="23dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/green"
        android:text="@string/edit" />

</RelativeLayout>

CommandDetails.java

public class CommandDetails extends Activity {



Button logout;  
Button editCommand;
Button bachToCommand;
Button exit;
ToggleButton toggleButton;

private Spinner spinner_contatct_Model ;
EditText contatct_Model;

//service web
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();

JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> contactlist;  

// url to get all accounts list
private static String url_command_details= "WebService/Command_Details.php";

// JSON Node names 
private static final String TAG_SUCCESS = "success";
private static final String TAG_COMMAND = "command";

private static final String TAG_ID = "ID";
private static final String TAG_COMMANDID = "COMMANDID";
private static final String TAG_QUANTITYCOMMANDED = "QUANTITYCOMMANDED";
private static final String TAG_COMMANDPRICE = "COMMANDPRICE";
private static final String TAG_PRODUCTID = "PRODUCTID";


// url to get all Contacts list
        private static String url_update_contact = "WebService/ContactUpdate.php";

// products JSONArray
JSONArray commands = null;
TextView Id;
@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);

    setContentView(R.layout.command_details);

    Bundle data = getIntent().getExtras() ;
    final String username =data.getString("username");
    final String id =data.getString("id");

    logout=(Button)findViewById(R.id.ButtonLogoff);
    logout.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {  
            finish();
        }});

    bachToCommand=(Button)findViewById(R.id.bachToCommand);
    bachToCommand.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) {  
                Intent intent = new Intent(v.getContext(),Command.class);
                intent.putExtra("username",username );
                finish();
                startActivity(intent);
            }});

    exit=(Button)findViewById(R.id.exitCommandDetail);
    exit.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) {  
                Intent intent = new Intent(v.getContext(),Home.class);
                intent.putExtra("username",username );
                finish();
                startActivity(intent);
            }});

    //addListenerOnSpinnerItemSelection();

toggleButton=(ToggleButton) findViewById(R.id.toggleButton1);

    toggleButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (toggleButton.isChecked()) {

            TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setEnabled(true);
                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setEnabled(true);
                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setEnabled(true);


                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.VISIBLE);
                editCommand.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v) {  

                            //new UpdateCommand().execute();

                        }});


            } else {
                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.INVISIBLE);

                TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setEnabled(false);
                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setEnabled(false);
                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setEnabled(false);

            }
        }
    });



    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("id", id));  

    // getting JSON string from URL

    JSONObject json = jParser.makeHttpRequest(getString(R.string.server_address)+url_command_details, "GET", params);

    // Check your log cat for JSON reponse
    Log.d("All Commands: ", json.toString());

    try {
        // Checking for SUCCESS TAG

            commands = json.getJSONArray(TAG_COMMAND);

            // looping through All Contacts
            for (int i = 0; i < commands.length(); i++) {
                JSONObject c = commands.getJSONObject(i);

                // Storing each json item in variable
                String commandid = c.getString(TAG_ID);
                String quantitycommand = c.getString(TAG_QUANTITYCOMMANDED);
                String commandprice = c.getString(TAG_COMMANDPRICE);
                String productid = c.getString(TAG_PRODUCTID);


                editCommand=(Button)findViewById(R.id.editCommand);
                editCommand.setVisibility(View.INVISIBLE);

                TextView commandProductQuantity=(TextView) findViewById(R.id.commandProductQuantity);
                commandProductQuantity.setText(quantitycommand);
                commandProductQuantity.setEnabled(false);   


                TextView commandPrice=(TextView) findViewById(R.id.commandPrice);
                commandPrice.setText(commandprice);
                commandPrice.setEnabled(false); 

                TextView commandProductname=(TextView) findViewById(R.id.commandProductname);
                commandProductname.setText(productid);
                commandProductname.setEnabled(false);   


            }

    }
    catch(Exception e){}

}

          }

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。您可以使用ListView,例如this example.

我喜欢使用TableLayout。您将TableLayout放在XML文件中,然后在代码中动态添加行。 Here is an example如何做到这一点。