0

由于我仍在学习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>

命令详细信息.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){}

}

          }
4

1 回答 1

0

有几种方法可以做到这一点。您可以使用 ListView,就像在这个例子中一样。

我喜欢使用 TableLayout。您将 TableLayout 放在 XML 文件中,然后在代码中动态地向其中添加行。 这是一个如何做到这一点的例子。

于 2013-05-06T18:09:33.980 回答