Android how to detect which tablerow was clicked

南笙酒味 提交于 2019-12-12 10:21:01

问题


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

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?

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

回答1:


First of all don't forget i++ at for(int i = 0; i < 5; i++)

You can try somthing like:

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

output on click:

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


来源:https://stackoverflow.com/questions/28302360/android-how-to-detect-which-tablerow-was-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!