有人可以帮助使这个表可点击

时间:2015-01-29 02:05:20

标签: android

有人可以帮忙解决这个问题。我需要这个可点击并更改行背景并在单击时获取数据。我已经尝试了所有教程并在我的代码中实现它,但没有任何作用

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(), "Item was not found!",
                Toast.LENGTH_LONG).show();
    }
    else
    {
tableLayout.removeAllViews();
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
    //item code
    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.

    //name
    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.

    //price
    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.

    //quantity
    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.

    //total
    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());
}
}

我希望这可以点击并烘烤数据。

1 个答案:

答案 0 :(得分:0)

你在哪里onclicklistener?
你需要为每个表行设置setOnClickListener,就像这样

tableRow.setOnClickListener(new OnClickListener(){
    public void onClick() {
    }
});



更新

我认为就像这样

tableRow.addView(Ll);
    // Adding textView to tableRow.
tableRow.setOnClickListener(new OnClickListener(){
        public void onClick() {
           Toast.makeText(getApplicationContext(), "Show something", 
   Toast.LENGTH_LONG).show();
        }
    });
    // Add the TableRow to the TableLayout
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));