Reputation: 147
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!
Upvotes: 0
Views: 514
Reputation: 338
If all you have are TextViews, you can just do :
@override
public void onClick(View v) {
String clickedTextViewValue = ((TextView) v).getText().toString();
}
Upvotes: 0
Reputation: 6792
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;
}
}
Upvotes: 0
Reputation: 1996
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);
}
Upvotes: 0
Reputation: 10876
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();
}
Upvotes: 0
Reputation: 157487
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
Upvotes: 1