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 8990729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:33:52+00:00 2026-06-15T22:33:52+00:00

I create clickable textview dynamically depending on the records retrieved from the database just

  • 0

I create clickable textview dynamically depending on the records retrieved from the database just like this.

DBAdapter db = new DBAdapter(this);

db.open();
Cursor c = db.getAllProfiles();
if(c.moveToFirst())
{
    do{
        TextView tv = new TextView(this);
        tv.setText(c.getString(1));
        tv.setTextSize(18);
        tv.setTextColor(Color.BLACK);
        tv.setClickable(true);
        tv.setPadding(0, 10, 0, 0);
        tv.setGravity(Gravity.CENTER);
        mainLayout.addView(tv);

        tv.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        Toast.makeText(getBaseContext(), tv.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });

    }while(c.moveToNext());
}
else
    Toast.makeText(getBaseContext(), "No Profiles", Toast.LENGTH_LONG).show();
db.close();

How can I set click events on each texview independently? On the way I see it, if I create a click event on the loop, the click event might get triggered by all textviews.

*Update tried adding events in every loop but got this error:

“cannot refer to a non-final variable inside an inner class defined in a different method”.

  • 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-06-15T22:33:53+00:00Added an answer on June 15, 2026 at 10:33 pm

    change your code as to add OnClickListener to TextView

    TextView tv;
         if(c.moveToFirst())
                {
                    do{
                        tv = new TextView(this);
                        tv.setText(c.getString(1));
                        tv.setTextSize(18);
                        tv.setTextColor(Color.BLACK);
                        tv.setClickable(true);
                        tv.setPadding(0, 10, 0, 0);
                        tv.setGravity(Gravity.CENTER);
                        tv.setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
                            // do you work here
                         }
                        });
                        mainLayout.addView(tv);
    
                    }while(c.moveToNext());
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create some clickable blue text on my GUI, kind of
Like the title says I wan't to create a clickable list, in which each
How can I create an HTML checkbox with a label that is clickable (this
i have created a textview with underline text like this <TextView android:id=@+id/label android:layout_width=wrap_content android:layout_height=wrap_content
I wanted to create some clickable PowerShell scripts, and I found this answer that
I am trying to create a drop down that will be something like this:
I am trying to create a specific aspx page where I display clickable links
CREATE OR REPLACE FUNCTION layer2layerAttribute RETURN VARCHAR2 AS /** * This function properly joins
create table ImagenesUsuario { idImagen int primary key not null IDENTITY } This doesn't
Create a new appdomain, setup the assemblyResolve handler and you always get an exception

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.