我已动态创建复选框,我正在使用服务器中的数据以表格布局的形式打印它。我添加了一个按钮,当用户按下按钮时,选中的复选框应显示其值(尚未实现)。
我的问题是,缺血方法不起作用。我已经标记了使用注释的单击事件的注释,以及复选框的初始化。
另外,请查看我的代码并告诉我,我是否正确地为复选框提供ID。
抱歉我的英文不好public class Availability extends Activity {
Users_availability q = new Users_availability();
Button book;
CheckBox chk;
ArrayList<String> productlist = new ArrayList<String>();
@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();
//////////////////////////////////////////////////called of button where///////////////////////////////////////////////
book.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(chk.isChecked()==true)
{
Toast.makeText(getApplicationContext(), "It works!!!", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
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.
// 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();
int j=0;
/** Create a TableRow dynamically **/
tr = new TableRow(this);
///////////////////////////////////////////////////////////initialization of checkbox//////////////////////////////////////////////////////////
chk = new CheckBox(this);
chk.setId(j);
j++;
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.setTag(p.getproduct_code());
productlist.add(p.getproduct_code());
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.
tr.addView((View)Ll);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
}
}
}