2015-12-30 89 views
1

這是我的代碼:如何在TableRow中動態創建按鈕單擊事件?

public class GolfExpoDetails extends AppCompatActivity { 

RequestObject requestObject; 
public static final String URL_EXPODETAIL ="http://knowledgeops.in/halogolf/upmeapi/class-woocommerce.php?function=search_golf_expo_api"; 
TextView expotitle,expodescription,stdate,endate,city; 
ImageView imageView; 
List<StallBO> stallBOList; 
TableLayout tableLayout; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_golf_expo_details); 
    /* Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar);*/ 
    expotitle = (TextView)findViewById(R.id.expodetail_title); 
    expodescription = (TextView)findViewById(R.id.expodetail_description); 
    stdate = (TextView)findViewById(R.id.expodetail_sdate); 
    endate = (TextView)findViewById(R.id.expodetail_edate); 
    city=(TextView)findViewById(R.id.expo_loc); 
    tableLayout=(TableLayout)findViewById(R.id.main_table); 
    long stallId = getIntent().getExtras().getLong("stallId"); 

    try { 
     requestObject = ExceptionRequest.generateSingleRequest("id", stallId); 
     requestObject.setUrl(URL_EXPODETAIL); 
     new ExpoDetailList().execute(requestObject); 
    }catch (Exception e) { 
     e.printStackTrace(); 
    } 




     } 

private class ExpoDetailList extends AsyncTask<RequestObject, Void, JSONObject> { 


    @Override 
    protected JSONObject doInBackground(RequestObject... arg0) { 
     // Creating service handler class instance 
     ServiceHandler sh = new ServiceHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(arg0[0], ServiceHandler.POST); 

     // List<Products> result = new ArrayList<Products>(); 

     Log.d("Response: ", "> " + jsonStr); 

     JSONObject expo = new JSONObject(); 


     if (jsonStr != null) { 

      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       expo = jsonObj.getJSONObject("rsBody"); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 

     return expo; 
    } 

    @Override 
    protected void onPostExecute(JSONObject result) { 
     super.onPostExecute(result); 

     stallBOList= new ArrayList<StallBO>(); 
     try{ 
      String title = result.getString("eventName"); 
      String description = result.getString("description"); 
      // String price = result.getString("Price"); 
      String img = result.getString("photoUrl"); 
      long productId = result.getLong("id"); 
      String sdate = result.getString("eventDate"); 
      String edate = result.getString("eventLastDate"); 
      String loc = result.getString("eventLocation"); 
      // String quantity = result.getString("quantity"); 
      //Integer length = result.getInt("length"); 
      // Integer width = result.getInt("width"); 
      JSONArray products = new JSONArray(); 
      products = result.getJSONArray("stallInformation"); 
      for (int i = 0; i < products.length(); i++) { 
       stallBOList.add(convertDetails(products.getJSONObject(i))); 
      } 



      expotitle.setText(title); 
      expodescription.setText(description); 
      stdate.setText(sdate); 
      endate.setText(edate); 
      // productPrice.setText("Rs" +Double.toString(price)); 
      Picasso.Builder builder = new Picasso.Builder(GolfExpoDetails.this); 
      Picasso picasso = builder.build(); 
      picasso.load(img) 
        .placeholder(R.drawable.ic_plusone_tall_off_client) 
        .resize(100,100) 
        .into(imageView); 
      city.setText(loc); 

      for (StallBO stallBO:stallBOList) 
      { 
       View tableRow = LayoutInflater.from(GolfExpoDetails.this).inflate(R.layout.table_expo,null,false); 
       TextView stall_size = (TextView) tableRow.findViewById(R.id.expo_stall_size); 
       TextView price = (TextView) tableRow.findViewById(R.id.expo_stall_price); 
       TextView stall_Quantity = (TextView) tableRow.findViewById(R.id.expo_sqty); 
       EditText user_Quantity = (EditText) tableRow.findViewById(R.id.expo_qty); 
       Button add_cart = (Button) tableRow.findViewById(R.id.add_cart); 

       stall_size.setText(String.valueOf(stallBO.getWidth())+"*"+String.valueOf(stallBO.getHeight())); 

       price.setText(stallBO.getPrice()); 
       stall_Quantity.setText(String.valueOf(stallBO.getQuantity())); 

       tableLayout.addView(tableRow); 

      } 

     } 
     catch (JSONException e) { 
      e.printStackTrace(); 
     } 


     return; 

    } 

    private StallBO convertDetails(JSONObject obj) throws JSONException { 
     Integer width = obj.getInt("width"); 
     Integer height = obj.getInt("height"); 
     Integer quantity = obj.getInt("quantity"); 
     String price = obj.getString("price"); 


     return new StallBO(width, height, quantity, price); 
    } 

} 
} 

對我的TableRow .xml文件是:

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:background="#2877AC"> 

    <TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/expo_stall_size" 
    android:layout_weight="0.25" 
    android:gravity="center" 
    android:textColor="#ffffff" /> 

<TextView 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:id="@+id/expo_stall_price" 
    android:layout_weight="0.25" 
    android:gravity="center" 
    android:textColor="#ffffff" /> 

<TextView 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:id="@+id/expo_sqty" 
    android:layout_weight="0.25" 
    android:gravity="center" 
    android:textColor="#ffffff" /> 

<EditText 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:id="@+id/expo_qty" 
    android:layout_weight="0.25" 
    android:gravity="center" 
    android:textColor="#ffffff" /> 
<Button 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:id="@+id/add_cart" 
    android:layout_weight="0.25" 
    android:gravity="center" 
    android:textColor="#ffffff" /> 
</TableRow> 

我的問題是,在那裏我會寫一個onClickListener的按鈕,這是內部的for循環以及如何當點擊該按鈕時,我會將該行的數據發送到服務器嗎?

+1

您可以點擊監聽器設置爲add_cart按鈕。像下面一樣'add_cart.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ ...... });' – Tamal

+0

哪個動態按鈕是你正在考慮的? –

+0

add_cart button – Anand

回答

1

onClickListener將被創建在被添加到表中各行:

for (StallBO stallBO:stallBOList){ 
     View tableRow = LayoutInflater.from(GolfExpoDetails.this).inflate(R.layout.table_expo,null,false); 
     TextView stall_size = (TextView) tableRow.findViewById(R.id.expo_stall_size); 
     TextView price = (TextView) tableRow.findViewById(R.id.expo_stall_price); 
     TextView stall_Quantity = (TextView) tableRow.findViewById(R.id.expo_sqty); 
     EditText user_Quantity = (EditText) tableRow.findViewById(R.id.expo_qty); 
     Button add_cart = (Button) tableRow.findViewById(R.id.add_cart); 


     //Here is where you will set the onClickListener: 
     add_cart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       <the code of what needs to be sent to your "server"> 
      } 
     }); 

     stall_size.setText(String.valueOf(stallBO.getWidth())+"*"+String.valueOf(stallBO.getHeight())); 

     price.setText(stallBO.getPrice()); 
     stall_Quantity.setText(String.valueOf(stallBO.getQuantity())); 

     tableLayout.addView(tableRow); 
    } 
+1

感謝它爲我工作 – Anand

+0

但先生我的問題仍然存在,我怎樣才能得到tableRow數據裏面點擊該行按鈕的監聽器? – Anand

0

您可以通過以下方式執行此操作,dynamicallyCreatedButton.PerformClick()強制導致按鈕被點擊。

相關問題