Question

Here is my code to add table rows dynamically,`

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);
}

and here is 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();

    }
};  

however i am not getting the row id by this approach, i am referring this question and this one also.

Was it helpful?

Solution

You need to use v.getId() to get ID of clicked view inside OnClickListner.

Replace this

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

By this one

Toast.makeText(getActivity(), "row selected --> " + v.getId() , 30).show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top