Android 如何在TableRow中触发动态创建的按钮单击事件?

Android 如何在TableRow中触发动态创建的按钮单击事件?,android,android-layout,android-activity,Android,Android Layout,Android Activity,这是我的代码: 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 expotitl

这是我的代码:

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);
    }

}
}
公共类GolfExpoDetails扩展AppCompative活动{
请求对象请求对象;
公共静态最终字符串URL_expodail=”http://knowledgeops.in/halogolf/upmeapi/class-woocommerce.php?function=search_golf_expo_api";
TextView expotitle、expodescription、stdate、endate、city;
图像视图图像视图;
列表列表;
表布局表布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动、高尔夫、世博会详情);
/*Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏)*/
expotitle=(TextView)findViewById(R.id.expodetail_title);
expodescription=(TextView)findViewById(R.id.expodetail\u description);
stdate=(TextView)findViewById(R.id.expodail\u sdate);
endate=(TextView)findViewById(R.id.expodetail_edate);
城市=(文本视图)findViewById(R.id.expo_loc);
tableLayout=(tableLayout)findviewbyd(R.id.main_table);
long stallId=getIntent().getExtras().getLong(“stallId”);
试一试{
requestObject=ExceptionRequest.generateSingleRequest(“id”,stallId);
setUrl(URL\u expodail);
新建ExpoDetailList().execute(请求对象);
}捕获(例外e){
e、 printStackTrace();
}
}
私有类ExpoDetailList扩展了异步任务{
@凌驾
受保护的JSONObject doInBackground(请求对象…arg0){
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(arg0[0],ServiceHandler.POST);
//列表结果=新建ArrayList();
Log.d(“响应:”、“>”+jsonStr);
JSONObject expo=新的JSONObject();
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
expo=jsonObj.getJSONObject(“rsBody”);
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
回归博览会;
}
@凌驾
受保护的void onPostExecute(JSONObject结果){
super.onPostExecute(结果);
stallBOList=newarraylist();
试一试{
String title=result.getString(“eventName”);
String description=result.getString(“description”);
//字符串价格=result.getString(“价格”);
字符串img=result.getString(“photoUrl”);
long productId=result.getLong(“id”);
String sdate=result.getString(“eventDate”);
String edate=result.getString(“eventLastDate”);
String loc=result.getString(“eventLocation”);
//字符串数量=result.getString(“数量”);
//整数长度=result.getInt(“长度”);
//整数宽度=result.getInt(“宽度”);
JSONArray products=新的JSONArray();
products=result.getJSONArray(“失速信息”);
对于(int i=0;i
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>


我的问题是,我应该在哪里为for循环中的按钮编写onClickListener,以及在单击该按钮时如何将该行的数据发送到服务器。

可以通过以下方式执行,
dynamicallyCreatedButton.PerformClick()
强制单击按钮。

将为添加到表中的每一行创建
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);
    }
for(StallBO-StallBO:stallBOList){
View tableRow=LayoutInflater.from(GolfExpoDetails.this).充气(R.layout.table_expo,null,false);
TextView展位大小=(TextView)tableRow.findViewById(R.id.expo展位大小);