2012-11-21 86 views
0

按鈕多的onClick我有一個表的佈局,我顯示爲以下值:在TableLayout

enter image description here

現在我能夠實現對排一下,但我每排三個按鈕。

點擊開始,完成和交付我需要更新MySQL表中的列(狀態)。
如果它只是一個錶行,我們可以做這樣的:

tr.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View pos) { 
       // TODO Auto-generated method stub 

       System.out.println("Row clicked: " + pos.getId()); 
      } 
     }); 

下面是我的表佈局代碼:

/*******getting values from mysql db and assigning it to teach row***********/ 
try{ 
      allarray = new JSONArray(result); 
      System.out.println("array is " +allarray.length()); 
      JSONObject json_data=null; 
       for(int j=0;j<allarray.length();j++){ 
       json_data = allarray.getJSONObject(j); 
       json_data.getString("PreparationLot"); 
       json_data.getString("Prep_MenuItem"); 
       json_data.getString("Prep_Quantity"); 
       json_data.getString("Prep_Order_Number"); 
       json_data.getString("Prep_Notes"); 

       TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1); 

       TableRow tr = new TableRow(DashBoard.this); 
       if(count%2!=0) { 
        tr.setBackgroundColor(Color.rgb(222, 219, 219)); 
       }else{ 
        tr.setBackgroundColor(Color.rgb(255, 255, 255)); 
       } 
       tr.setId(20); 
       tr.setLayoutParams(new LayoutParams()); 

       TextView lotno = new TextView(DashBoard.this); 
       lotno.setId(200+count);// define id that must be unique 
       lotno.setText(""+json_data.getString("PreparationLot")); // set the text for the header 
       lotno.setTextColor(Color.BLACK); // set the color 
       lotno.setPadding(10, 10, 0, 10); // set the padding (if required) 
       lotno.setTextSize(18); 
       tr.addView(lotno); 

       TextView itemname = new TextView(DashBoard.this); 
       itemname.setId(200+count);// define id that must be unique 
       itemname.setText(""+json_data.getString("Prep_MenuItem")); // set the text for the header 
       itemname.setTextColor(Color.BLACK); // set the color 
       itemname.setPadding(30, 10, 0, 10); // set the padding (if required) 
       itemname.setTextSize(18); 
       tr.addView(itemname); 

       TextView qty = new TextView(DashBoard.this); 
       qty.setId(200+count);// define id that must be unique 
       qty.setText(""+json_data.getString("Prep_Quantity")); // set the text for the header 
       qty.setTextColor(Color.BLACK); // set the color 
       qty.setPadding(70, 10, 0, 10); // set the padding (if required) 
       qty.setTextSize(18); 
       tr.addView(qty); 

       TextView order = new TextView(DashBoard.this); 
       order.setId(200+count);// define id that must be unique 
       order.setText(""+json_data.getString("Prep_Order_Number")); // set the text for the header 
       order.setTextColor(Color.BLACK); // set the color 
       order.setPadding(60, 10, 0, 10); // set the padding (if required) 
       order.setTextSize(18); 
       tr.addView(order); 

       TextView notes = new TextView(DashBoard.this); 
       notes.setId(200+count);// define id that must be unique 
       notes.setText(""+json_data.getString("Prep_Notes")); // set the text for the header 
       notes.setTextColor(Color.BLACK); // set the color 
       notes.setPadding(60, 10, 0, 10); // set the padding (if required) 
       notes.setTextSize(18); 
       tr.addView(notes); 

       Button start = new Button(DashBoard.this); 
       start.setText("Start"); 
       start.setMaxWidth(10); 
       tr.addView(start); 

       Button complete = new Button(DashBoard.this); 
       complete.setText("Complete"); 
       complete.setMaxWidth(10); 
       tr.addView(complete); 

       Button delivered = new Button(DashBoard.this); 
       delivered.setText("Delivered"); 
       delivered.setMaxWidth(10); 
       tr.addView(delivered); 

       tl.addView(tr, new TableLayout.LayoutParams(
         LayoutParams.FILL_PARENT, 
         LayoutParams.WRAP_CONTENT)); 

      count++; 
      tr.setClickable(true); 
      tr.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View pos) { 
       // TODO Auto-generated method stub 
      // Toast.makeText(DashBoard.this, v.getId(), 1000).show(); 
       System.out.println("Row clicked: " + pos.getId()); 
      } 
     }); 
       } 
      } 
      catch(JSONException e1){ 
        Toast.makeText(getBaseContext(), "No data Found" ,Toast.LENGTH_LONG).show(); 
      } 

問題:如果我點擊開始,完成或交付我需要更新該行的狀態。怎麼做?

+0

請包含您的設計xml文件 – kittu88

+0

@ kittu88我正在添加表格佈局 – Goofy

+2

爲'TableRows'中的所有'Buttons'設置一個'OnClickListener'。在該偵聽器的'onClick'方法中:'TableRow tr =(TableRow)v.getParent();'。然後你可以在'findViewById'中使用'tr'來從'Button'的行中獲取其他'View'來更新它。 – Luksprog

回答

0

問題:如果我點擊開始,已完成或者交付我需要更新 該行的狀態。怎麼做?

首先,我不知道您通過更新行的狀態瞭解什麼。如果你想「看」點擊任何TableRows爲任何Buttons的事件創建一個單一OnCLickListener

private OnClickListener mListener = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     TableRow tr = (TableRow) v.getParent(); 
     // now you have a reference to the row where this Button that was 
     // clicked exists 
     // do whatever row updates you want. 
    } 
}; 

mListener將被設置爲每個TableRowButtons

  //... 
      Button start = new Button(DashBoard.this); 
      start.setOnClickListener(mListener); 
      start.setText("Start"); 
      start.setMaxWidth(10); 
      tr.addView(start); 

      Button complete = new Button(DashBoard.this); 
      complete.setOnClickListener(mListener); 
      complete.setText("Complete"); 
      complete.setMaxWidth(10); 
      tr.addView(complete); 

      Button delivered = new Button(DashBoard.this); 
      delivered.setOnClickListener(mListener); 
      delivered.setText("Delivered"); 
      delivered.setMaxWidth(10); 
      tr.addView(delivered); 
+0

謝謝你會嘗試,並取回給你... – Goofy

1

您需要爲行內的每個項目添加onclicklistners,而不是行本身。

Button start = new Button(DashBoard.this); 
start.setText("Start"); 
start.setMaxWidth(10); 
start.setOnClickListener(new OnClickListener() { 

    @Override 
     public void onClick(View pos) { 
         // update fields for start 
         // your code for update your status of the row 
     } 
     }); 
tr.addView(start); 

Button complete = new Button(DashBoard.this); 
complete.setText("Complete"); 
complete.setMaxWidth(10); 
complete.setOnClickListener(new OnClickListener() { 

    @Override 
     public void onClick(View pos) { 
         // update fields for complete 
         // your code for update your status of the row 
     } 
     }); 

tr.addView(complete); 


Button delivered = new Button(DashBoard.this); 
delivered.setText("Delivered"); 
delivered.setMaxWidth(10); 
delivered.setOnClickListener(new OnClickListener() { 

    @Override 
     public void onClick(View pos) { 
         // update fields for delivered 
         // your code for update your status of the row 
     } 
     }); 

tr.addView(delivered); 

編輯:添加了其他2個onclicklistners並增加了一些commets

+4

對不起,請您詳細說明一下。 – Goofy

+0

你想更新什麼狀態?你將需要添加所有的變量,你想要更新的交付按鈕onclick /開始/繼續 – user1841306