我正在创建在线购物应用程序,其中所有已检查的项目都将添加到购物车中。在屏幕截图中,您可以看到有一个以编程方式创建的复选框。整个表是使用来自php服务器的数据以编程方式创建的(我使用JSON来填充表的数据)。我的问题是如何获取选中复选框的数据并将其发送回服务器,然后按下按钮将其存储在另一个数据库中。
服务器准备就绪,问题出在客户端。
抱歉我的英语不好。
这是我的代码
public class Availability extends Activity implements View.OnClickListener {
String data = "";
TableLayout tl;
TextView tv;
TableRow tr;
TextView label;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
Users_availability q = new Users_availability();
Button book;
CheckBox chk;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_availability);
tl = (TableLayout) findViewById(R.id.main_table);
tv = (TextView)findViewById(R.id.tv);
book=(Button)findViewById(R.id.book);
final GetDatafromDB_Availability getdb = new GetDatafromDB_Availability();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<Users_availability> users = parseJSON(data);
addData(users);
}
});
}
}).start();
book.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(chk.isChecked())
{
}
}
});
}
public ArrayList<Users_availability> parseJSON(String result) {
ArrayList<Users_availability> users = new ArrayList<Users_availability>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users_availability user = new Users_availability();
user.setId(json_data.getInt("id"));
user.setProduct_code(json_data.getString("product_code"));
user.setShapes(json_data.getString("shaps"));
user.setPair(json_data.getString("pair"));
user.setCarats(json_data.getString("carats"));
user.setColor(json_data.getString("color"));
user.setClarity(json_data.getString("clarity"));
user.setService(json_data.getString("service"));
user.setPolish(json_data.getString("polish"));
user.setSymetric(json_data.getString("symetric"));
user.setTables(json_data.getString("tables"));
user.setMeasurements(json_data.getString("measurments"));
user.setFlourscne(json_data.getString("flourscne"));
user.setDescription(json_data.getString("description"));
user.setCerticated(json_data.getString("certificated"));
user.setCcode(json_data.getString("ccode"));
user.setCut(json_data.getString("cut"));
user.setTotal(json_data.getString("total"));
user.setFile(json_data.getString("file"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
TextView add = new TextView(this);
add.setText("Add");
add.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
add.setPadding(5, 5, 5, 5);
add.setBackgroundColor(Color.parseColor("#BDB76B"));
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(add,params);
tr.addView((View)Ll);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("Product code");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView shapes = new TextView(this);
shapes.setText("Shapes");
shapes.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
shapes.setPadding(5, 5, 5, 5);
shapes.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(shapes,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView pair = new TextView(this);
pair.setText("pair");
pair.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
pair.setPadding(5, 5, 5, 5);
pair.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(pair,params);
tr.addView((View)Ll); // Adding textview to tablerow.
TextView carats = new TextView(this);
carats.setText("Carats");
carats.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
carats.setPadding(5, 5, 5, 5);
carats.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(carats,params);
tr.addView((View)Ll); // Adding textview to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
@SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users_availability> users) {
addHeader();
for (Iterator i = users.iterator(); i.hasNext();) {
Users_availability p = (Users_availability) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
chk = new CheckBox(this);
// btn.setTextSize();
//btn.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
// btn.setTag(mLinks.get(index));
// btn.setOnClickListener(this);
//Ll = new LinearLayout(this);
// params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
// LayoutParams.WRAP_CONTENT);
// params.setMargins(5, 2, 2, 2);
// params.width=400;
// params.height=60;
// btn.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
// Ll.addView(chk,params);
tr.addView((View)chk);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getproduct_code());
label.setId(p.getId());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
////label.setBackgroundColor(Color.parseColor("#BDB76B"));
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText(p.getShapes());
place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
place.setPadding(5, 5, 5, 5);
// place.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place,params);
tr.addView((View)Ll); // Adding textview to tablerow.
label = new TextView(this);
label.setText(p.getpair());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
////label.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
label = new TextView(this);
label.setText(p.getCarats());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
////label.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
label = new TextView(this);
label.setText(p.getColor());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
////label.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
}
}
}