Musterknabe
Musterknabe

Reputation: 6081

Get text of dynamic generated TextView when clicking on it

I'm running through a for-loop and am generating TextViews that should be clickable because I want to start then an intent and pass the url as parameter as well as the source.

So, I've tried this

articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //System.out.println(articleURL[v.getId()].getText().toString());
        System.out.println(v.getId());
    }
});

The problem i encounter is that the v.getId() is always 0. And when i use the commented code

System.out.println(articleURL[v.getId()].getText().toString()); 

I get an exception that says

java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1

I just need the content of the TextView i clicked on. How exactly do i get it? articleURL[i] doesn't work because he doesn't know i then. How can v.getId() always be -1? No matter which one I click?

This here is the complete for-loop

TextView articleURL = new TextView[hashMapSize];
for (int i = 0; i < hashMapSize; i++) {
    articleURL[i] = new TextView(getActivity());
    articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    articleURL[i].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println(articleURL[v.getId()].getText().toString());
            //System.out.println(v.getId());
        }
    });
}

Upvotes: 2

Views: 1549

Answers (3)

Hariharan
Hariharan

Reputation: 24853

Try this..

use this as globel

TextView articleURL[];

and then initial the articleURL like below

articleURL = new TextView[hashMapSize];

and then if your extends fragement means use below

articleURL[i]   = new TextView(getActivity());

extends activity means

articleURL[i]   = new TextView(this);

and

System.out.println(((TextView)v).getText().toString());

Upvotes: 0

xapienz
xapienz

Reputation: 169

You may try the following:

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        System.out.println(((TextView)v).getText());
    }
};
// ... some loop
articleURL[i].setOnClickListener(listener);

If you also want to get index of item, try this:

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        System.out.println(v.getTag());
    }
};
// ... some loop
articleURL[i].setTag(i);
articleURL[i].setOnClickListener(listener);

Upvotes: 0

meredrica
meredrica

Reputation: 2563

You actually get the View in the parameter. Just cast it to TextView and call getText()

public void onClick(View v) {
  Log.d("text",((TextView) v).getText().toString());
}

Also don't use System.out.println. This is Android, not desktop Java, and coding android is a huge difference to normal Java. You should get a book on Android and read it, otherwise your apps will start crashing pretty soon and you won't have any chance to fix them.

Upvotes: 10

Related Questions