Android如何检测单击了哪个表格行 [英] Android how to detect which tablerow was clicked

查看:96
本文介绍了Android如何检测单击了哪个表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的片段布局中,我有一个这样的表格布局:

In my fragment layout I've a tablelayout like this:

    <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>

在这个表格布局中,我以编程方式添加了多个表格行,并在其中添加了一个这样的视图:

In this tablelayout, I add programmatically multiple tablerows with a view inside like this:

for(int i = 0; i < 5;){
    tableLayout.addView(createTableRow((LocationObject)objectList.get(i)), i);
}

-

private View createTableRow(LocationObject locObject) {

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

    TextView textViewName = (TextView) v.findViewById(R.id.textView_name);
             textViewName.setText(locObject.getTitle());

    TextView textViewProvider = (TextView) v.findViewById(R.id.textView_provider);
             textViewProvider.setText(locObject.getSubTitle());

    return v;
}

在我多次调用createTableRow方法并用行填充表格布局之后,我想检测用户单击行的时间. 如何为行赋予不同的ID,例如第一个ID为0,第二个ID为ID,等等.最后如何检测用户单击行的时间?

After I called the createTableRow method a few times and filled the tablelayout with rows, I want to detect when the user clicks a row. How can I give the row different id's, like the first gets the id 0, the second 1 etc.. And last how can I detect when a user clicks a row?

我尝试了setOnClickListener,但是当我单击视图时它无法正常工作,在logcat中永远不会显示消息"isreachable".

I tried setOnClickListener but it dosen't work when I click on the view, the message "is reachable" is never shown in logcat.

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

推荐答案

首先不要忘记for(int i = 0; i < 5; i++)上的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

这篇关于Android如何检测单击了哪个表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆