简体   繁体   中英

Adding OnClickListener to dynamically created Textviews

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!

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

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();

    }

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);
    }

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;
     }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM