无法在android的表格布局中获取动态添加行的行ID

时间:2014-04-01 06:35:32

标签: android tablelayout tablerow

这是我动态添加表行的代码,`

for (int j = 0; j < list.size(); j++) {           


TableRow tableRow = new TableRow(getActivity());
        table_layout.addView(tableRow);
        tableRow.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
        tableRow.setId(j);
        tableRow.setClickable(true);
        tableRow.setOnClickListener(tablerowOnClickListener);




TextView textView0 = new TextView(getActivity());
        textView0.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
        textView0.setText(currencySymbol.get(j));
        textView0.setGravity(Gravity.CENTER_HORIZONTAL
                | Gravity.CLIP_VERTICAL);
        tableRow.addView(textView0);


        TextView textView1 = new TextView(getActivity());
        textView1.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));

        textView1.setGravity(Gravity.RIGHT);
        tableRow.addView(textView1);
}

这里是onclicklistener

OnClickListener tablerowOnClickListener = new OnClickListener() {
    public void onClick(View v) {
        //GET TEXT HERE
        //String currenttext = ((TextView)v).getText().toString());
    Toast.makeText(getActivity(), "row selected" + View.getId , 30).show();

    }
};  

但是我没有通过这种方法获得行ID,我指的是this问题和this one also

1 个答案:

答案 0 :(得分:0)

您需要使用 v.getId() 来获取OnClickListner中点击视图的ID。

替换此

Toast.makeText(getActivity(), "row selected" + View,getId , 30).show();

通过这个

Toast.makeText(getActivity(), "row selected --> " + v.getId() , 30).show();