user6487002
user6487002

Reputation:

Programmatically get selected rows data

I am developing a sample application to understand how does looping data in a table should work.

The next challenges I am facing is how do I get the data upon tapping onto a particular rows.

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_profile"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.singatour.touristattraction.ProfileActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableLayout
            android:id="@+id/tlGuide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left">


        </TableLayout>
    </ScrollView>
</LinearLayout>

Simple program

package com.singatour.touristattraction;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class GuideActivity extends AppCompatActivity
{
    TextView tvGuideName = null;
    TableLayout tlGuide = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        tlGuide     = (TableLayout) findViewById(R.id.tlGuide);

        for(int i  = 0; i < 100; i++)
        {
            TableRow rows = new TableRow(this);
            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
            rows.setLayoutParams(lp);

            tvGuideName =  new TextView(this);
            tvGuideName.setId(i);
            tvGuideName.setText("I am in row " + i);
            rows.addView(tvGuideName);

            tlGuide.addView(rows, i);
        }
    }
}

I am able to loop a 100 rows of data ranging from

I am in row 0

I am in row 1

I am in row 2

..

..

I am in row 99

However, is there a way to get get the data that I had set upon tapping onto the row?

Upvotes: 1

Views: 1482

Answers (1)

SAM_U_RAI
SAM_U_RAI

Reputation: 65

I think you need something like this:

 tr = new TableRow(this);
    tr.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
          TableRow t = (TableRow) view;
          TextView firstTextView = (TextView) t.getChildAt(i);
          String firstText = firstTextView.getText().toString();
       }
    });

Sources of answer: How do I get values from a dynamically created android TableRow?

How to get selected row object from Table in Android?

If It will help you - vote for this answer. I need to increase my reputation on Stackoverflow :)

Upvotes: 1

Related Questions