0

so I'm trying to add OnClickListeners to dynamically created textviews inside of a horizontal scroll. I currently have dynamically created textviews, but of course, the manner I am setting the OnClickListener results in any click of one of the TextViews to send only the information of the last TextView which was created. For instance, I have 1000 textviews and all 1000 of them on click will give me the data of the 1000th textView. I'd like to pull the .text.toString() of whichever was clicked in order to send that data elsewhere. Here's what I've got:

protected void onPostExecute(JSONArray jArr) {

        try {
            for (int i = 0; i < jArr.length(); i++) {

                flightList.add(jArr.getString(i));
                aText = new TextView(getApplicationContext());
                aText.setText(jArr.getString(i));
                aText.setWidth(50);
                aText.setBackgroundColor(Color.BLACK);
                aText.setTextColor(Color.WHITE);
                aText.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v){
                        GetMillis myTimer = new GetMillis();
                        aURL = "http://somelink?startTs="
                                + myTimer.getThreeAMToday()
                                + "&endTs="
                                + myTimer.getFourAMTomorrow()
                                + "&datafield="
                                + aText.getText().toString();

                        new myScheduleTask().execute();
                    }
                });

Any Help is appreciated!

5 Answers 5

1

you can use aText.setTag() to store the String you want to send. When the view is clicked you can use the v parameter to get the String from the tag

Sign up to request clarification or add additional context in comments.

3 Comments

A similar option would be to use aText.setId(). Just a matter of whether you want to use a String or an int.
So I would essentially during TextView creation go aText.setId(jArr.getString(i)); and then in the url use v.getId().toString()???
@Elliott aText.setTag / v.getTag
0

If all you have are TextViews, you can just do :

@override
public void onClick(View v) {
      String clickedTextViewValue = ((TextView) v).getText().toString();
}

Comments

0

try like this

set aText.setOnClickListener(this);

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
     aURL = "http://somelink?startTs="
                            + myTimer.getThreeAMToday()
                            + "&endTs="
                            + myTimer.getFourAMTomorrow()
                            + "&datafield="
                            + v.getText().toString();

    }

Comments

0

may be this will help

/** This is used to create Answer Letters */
    for (int i = 0; i < length; i++) {

        mAnsImage[i] = new TextView(getActivity());         
        mAnsImage[i].setOnClickListener(onAnswerClickListener);
    }

Comments

0

Set unique Id's to each TextView using textView.setID() method and then in the onClickListener's onClick method, fetch for the TextView using it's ID and perform appropriate action.

public void onClick(View v){
     switch(v.getID()){
     case 1:
          //This is TextView with id=1
          break;
     }
}

3 Comments

so I have legitimately 1000 textviews being created, each with a different string as text. that's a lot of cases...
Also, try keeping your onClick method out of your loop. That will make the code look more cleaner.
Thats only if your are looking for a different implementation for all of them. If you only want their text simply do public void onClick(View v){ String text = v.getText().toString() }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.