Android如何检测单击了哪个tablerow

Android如何检测单击了哪个tablerow,android,android-tablelayout,clickable,Android,Android Tablelayout,Clickable,在我的片段布局中,我有一个如下的表格布局: <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableLayout android:id="@+id/tableLayout1"

在我的片段布局中,我有一个如下的表格布局:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        </TableLayout>
    </LinearLayout>
for(int i = 0; i < 5;){
    tableLayout.addView(createTableRow((LocationObject)objectList.get(i)), i);
}
在我调用createTableRow方法几次并用行填充tablelayout之后,我想检测用户何时单击一行。 我如何给行赋予不同的id,比如第一行得到id 0,第二行得到id 1等等。。最后,当用户单击一行时,我如何检测

编辑: 我尝试了setOnClickListener,但当我单击视图时它不起作用,logcat中从未显示消息“is reachable”

private View createTableRow(LocationObject locObject) {

TableRow tr = new TableRow(getActivity());
View v = LayoutInflater.from(getActivity()).inflate(R.layout.view_layout, tr, false);

...
v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.e("output", "is reachable");
        }

    });

return v;
}

首先,不要忘记
i++
at
for(inti=0;i<5;i++)

您可以尝试以下方法:

private View createTableRow(int position) {

    //instead
    //TableRow tr = new TableRow(MainActivity.this);
    //View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, tr, false);

    //try
    View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, null, false);

    TextView textViewName = (TextView) v.findViewById(R.id.textView_name);
    textViewName.setText("row "+ position);

    v.setTag(position);
    v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            Log.e("output", "is reachable at position "+ position);
        }

    });

    return v;
}
点击时输出:

02-04 09:36:13.615    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 0
02-04 09:36:14.680    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 2
02-04 09:36:15.310    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 4

首先,不要忘记
i++
at
for(inti=0;i<5;i++)

您可以尝试以下方法:

private View createTableRow(int position) {

    //instead
    //TableRow tr = new TableRow(MainActivity.this);
    //View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, tr, false);

    //try
    View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, null, false);

    TextView textViewName = (TextView) v.findViewById(R.id.textView_name);
    textViewName.setText("row "+ position);

    v.setTag(position);
    v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            Log.e("output", "is reachable at position "+ position);
        }

    });

    return v;
}
点击时输出:

02-04 09:36:13.615    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 0
02-04 09:36:14.680    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 2
02-04 09:36:15.310    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 4

首先,不要忘记
i++
at
for(inti=0;i<5;i++)

您可以尝试以下方法:

private View createTableRow(int position) {

    //instead
    //TableRow tr = new TableRow(MainActivity.this);
    //View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, tr, false);

    //try
    View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, null, false);

    TextView textViewName = (TextView) v.findViewById(R.id.textView_name);
    textViewName.setText("row "+ position);

    v.setTag(position);
    v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            Log.e("output", "is reachable at position "+ position);
        }

    });

    return v;
}
点击时输出:

02-04 09:36:13.615    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 0
02-04 09:36:14.680    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 2
02-04 09:36:15.310    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 4

首先,不要忘记
i++
at
for(inti=0;i<5;i++)

您可以尝试以下方法:

private View createTableRow(int position) {

    //instead
    //TableRow tr = new TableRow(MainActivity.this);
    //View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, tr, false);

    //try
    View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, null, false);

    TextView textViewName = (TextView) v.findViewById(R.id.textView_name);
    textViewName.setText("row "+ position);

    v.setTag(position);
    v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            Log.e("output", "is reachable at position "+ position);
        }

    });

    return v;
}
点击时输出:

02-04 09:36:13.615    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 0
02-04 09:36:14.680    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 2
02-04 09:36:15.310    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 4

您是否尝试过在
createTableRow
中添加
onClickListener
?@Marcus是的,但不起作用。您是否尝试过在
createTableRow
中添加
onClickListener
?@Marcus是,但不起作用。您是否尝试过在
createTableRow
中添加
onClickListener
?@Marcus是,但它不起作用。您是否尝试过在
createTableRow
中添加
onClickListener
?@Marcus是的,但它不起作用。