这是我的代码:
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>
我的问题是,我会在哪里为for循环中的按钮编写onClickListener,以及在单击该按钮时如何将该行的数据发送到服务器。
答案 0 :(得分: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 :(得分:0)
您可以通过以下方式执行此操作:dynamicallyCreatedButton.PerformClick()
强制导致单击该按钮。