Vote count:
0
I use a connection to get the data in my database and print it my table layout with dynamic table row generated by the user's input. My problem is I don't know how to make this table row. I've try some codes and tutorials but nothing's work, Please help me.
tableLayout.removeAllViews();
phpScript = "get.php";
getData();
try
{
ArrayList<Users> users = new ArrayList<Users>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
user.setId(json_data.getInt("id"));
user.setName(json_data.getString("name"));
user.setPrice(json_data.getDouble("price"));
user.setQnty(json_data.getInt("qnty"));
user.setTotal(json_data.getDouble("total"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
if(result.isEmpty())
{
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
else
{
for (Iterator i = users.iterator(); i.hasNext();) {
Users p = (Users) i.next();
tableRow = new TableRow(this);
tableRow.setClickable(true);
LinearLayout Ll;
LinearLayout.LayoutParams params;
/// Creating a TextView to add to the row
idView = new TextView(this);
idView.setText(""+p.getId());
idView.setGravity(Gravity.CENTER);
idView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
idView.setBackgroundColor(Color.WHITE);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Ll.addView(idView,params);
tableRow.addView(Ll);
// Adding textView to tableRow.
nameView = new TextView(this);
nameView.setText(p.getName());
nameView.setGravity(Gravity.RIGHT);
nameView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
nameView.setBackgroundColor(Color.WHITE);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Ll.addView(nameView,params);
tableRow.addView(Ll);
// Adding textView to tableRow.
priceView = new TextView(this);
priceView.setText(""+p.getPrice());
priceView.setGravity(Gravity.CENTER);
priceView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
priceView.setBackgroundColor(Color.WHITE);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Ll.addView(priceView,params);
tableRow.addView(Ll);
// Adding textView to tableRow.
qntyView = new TextView(this);
qntyView.setText(""+NumberFormat.getInstance().format(p.getQnty()));
qntyView.setGravity(Gravity.RIGHT);
qntyView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
qntyView.setBackgroundColor(Color.WHITE);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Ll.addView(qntyView,params);
tableRow.addView(Ll);
// Adding textView to tableRow.
totalView = new TextView(this);
totalView.setText(""+NumberFormat.getInstance().format(p.getTotal()));
totalView.setGravity(Gravity.RIGHT);
totalView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
totalView.setBackgroundColor(Color.WHITE);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
Ll.addView(totalView,params);
tableRow.addView(Ll);
// Adding textView to tableRow.
// Add the TableRow to the TableLayout
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
asked 19 secs ago
Clickable dynamic tablerow in android
Aucun commentaire:
Enregistrer un commentaire