Table contains multiple rows each row has textview and button share, how to identify each row components?
<p>I'm creating android application that get texts from db ,and show them as a table
where number of rows depend on number of texts on that db
each text will be shown on row and a share button beside it </p>

<p>I created 2 layouts , 1 for my table and another for each row
now I fill each row with a function addtablerow 
but how can I identify each button for exact row ?</p>

<p>cause only last button works well and other button has no action??
I thought of creating array of buttons but always it crashes on creation !</p>

<p>for more details here is my code</p>

<p>activity_main.xml</p>

<pre><code>&lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" &gt;

android:layout_height="wrap_content"
android:layout_width="fill_parent" &gt;
&lt;TableLayout
    android:id="@+id/my_table"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:stretchColumns="1"
    android:shrinkColumns="1"/&gt;
&lt;/ScrollView&gt;
</code></pre>

<p>table_row_item.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:focusable="true"
android:focusableInTouchMode="false"
android:clickable="true"
android:background="@android:drawable/list_selector_background"&gt;

&lt;TextView
    android:id="@+id/cell"

/&gt;
 &lt;Button
    android:id="@+id/button1"
    android:layout_width="7dp"
    android:layout_height="77dp"
    android:text="Button" /&gt;
&lt;/TableRow&gt;
</code></pre>

<p>MainActivity.java</p>

<pre><code>package com.example.tabletest.test;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {
public int i;
public Button btn;
public TextView tv;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    i=1; //just for test , but later i will get position of data on db
    addTableRow();
    i=2;
    addTableRow();

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int j=btn.getId();
            Intent sharingIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "this is"+j;
            sharingIntent
                    .putExtra(android.content.Intent.EXTRA_TEXT,     shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via: "));
        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
private void addTableRow() {
    final TableLayout table = (TableLayout) findViewById(R.id.my_table);
    final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item, null);

    // fill cell txt
    tv = (TextView) tr.findViewById(R.id.cell);
    tv.setText("This is row"+i);
    table.addView(tr);

    btn=(Button) tr.findViewById(R.id.button1);
    btn.setId(i);


    registerForContextMenu(tr);
}


}
</code></pre>
