Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6243905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:13:35+00:00 2026-05-24T12:13:35+00:00

I have a TableLayout and some unknown number of TableRows that are generated based

  • 0

I have a TableLayout and some unknown number of TableRows that are generated based on what’s in the database.

Every row has an OnClick listener attached, however, once the click happens I can’t tell (meaningfully) from what row it came from. Is there a way that I can get the index of the tablerow in relation to the TableLayout or should I try something else?

Some code:

private void buildBudgetTable() {
    NumberFormat currencyFormatter = DecimalFormat.getCurrencyInstance();

    TableLayout budgetTable = (TableLayout) findViewById(R.id.budgetTable);
    budgetTable.removeAllViews();

    try {
        this.budgetItems = Budget.GetEntries(this);
    } catch (SQLException e) {
        Toast.makeText(getApplicationContext(), "Could not load budget", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        finish();
    }

    for(BudgetEntryItem entry : budgetItems){
        TableRow tr = new TableRow(this);
        tr.setPadding(1, 2, 1, 2);

        TextView txtLeft = new TextView(this);
        txtLeft.setText(entry.Memo);
        txtLeft.setGravity(Gravity.LEFT);
        txtLeft.setPadding(3,3,3,3);

        TextView txtRight = new TextView(this);
        txtRight.setText(currencyFormatter.format(entry.Amount));
        txtRight.setGravity(Gravity.RIGHT);
        txtRight.setPadding(3,3,3,3);

        if(entry.Amount > 0){
            txtRight.setBackgroundColor(Color.rgb(0x33, 0xEE, 0x33));
            txtLeft.setBackgroundColor(Color.rgb(0x33, 0xEE, 0x33));
        }else{
            txtRight.setBackgroundColor(Color.rgb(0xEE, 0x33, 0x33));
            txtLeft.setBackgroundColor(Color.rgb(0xEE, 0x33, 0x33));
        }

        tr.addView(txtLeft);
        tr.addView(txtRight);
        tr.setOnClickListener(this);

        budgetTable.addView(tr);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:13:37+00:00Added an answer on May 24, 2026 at 12:13 pm

    Use the TableRow’s tag:

    BudgetEntryItem entry = null;
    for(int i = 0; i < budgetItems; i++){
        entry = budgetItems[i]; // or budgetItems.get(i), etc
        TableRow tr = new TableRow(this);
    
        // Set the index as the tag
        tr.setTag(i); // or entry.setTag(entry.index()), etc
    
        // ...
    }
    

    Now to grab the index, just convert it back to an int in your onClick which provides you with the originating View.

    An aside
    Some people frown on using Tag’s in this manner on the principle that it couples the data layer with the UI layer. An alternative would be to use a Dictionary or Dictionary, etc to access the item indirectly without tying the UI element to the data object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that appends some rows to a table. One of the
I have a TableLayout with three columns that represents a form with requiredSymbol |
I have a TableLayout inside a ScrollView, because of some special needs, I need
I have a series of Views in a tablelayout. Some are EditTexts, some are
I have a TableLayout with dynamically added TableRows depending on the size of a
I have an xml file right now which has a TableLayout within a LinearLayout.
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
I want to design a TableLayout with some big buttons with icon and text
I have a form with some controls. (Actually, this is a sort of ongoing
I have an activity where a bunch of buttons are placed inside TableLayout, not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.