2013-05-06 25 views
0

由於我還在學習Android,我認爲我的問題對您來說很簡單。 我試圖顯示一個包含來自mysql的信息令牌的表。例如,我想顯示客戶端命令的命令。如果他訂購了一種產品,我會有一排,所以我將擁有與他訂購的產品數量一樣多的行。我真的不知道該怎麼做,而且我也沒有在網上找到解決方案。 (表格中的所有框都可以編輯)。如何從JSON中添加表格中的行

<?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){} 

} 

      } 

回答

0

有幾個方法可以做到這一點。您可以使用ListView,如在this example.

我喜歡使用TableLayout。將TableLayout放入XML文件中,然後在代碼中動態添加行。 Here is an example如何做到這一點。

+0

在我的情況下,適當的方法是使用表,因爲它可以編輯。有些框會有EditText類型,其他框會有一個微調框類型... – user2235164 2013-05-07 08:06:30