-2

I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...

I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )

The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(

So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...

https://i.stack.imgur.com/LRJGz.png

Charlie
  • 170
  • 12
  • 1
    Why do you need 200 text views? Can you show a diagram of your layout? It will probably help others suggest better options (ie if you can change to a list it will offer better performance and allow yo to do this with on OnItem listener) – Nick Cardoso Feb 14 '14 at 10:13
  • I think a ListView may be a good idea in this case, but am unsure?? – Swedish Architect Feb 14 '14 at 10:16
  • Hi there, iv attached a png of my xml layout and was thinking of using a ListView/GridView layout... probably best to now since i can see the benefits it gives. Especially when programming... i guess that what happens when im a beginner... not familiar with other strategies... thank you for your answer. – Charlie Feb 14 '14 at 18:20

5 Answers5

2

One of the ways to do what you want is to use the text view setTag() and getTag() methods.

On init of a text view use the setTag() to set some value to identify the view.

In the on click event use the getTag() on the view argument to know which view was clicked.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
chipopo
  • 326
  • 2
  • 9
  • Hey thanks... i was thinking of using a if statement to see which textview was clicked and then perform a getClicked or something that recognises which textview was clicked then apply the setText Method to that textview without having to state which textview was actually clicked... e.g.. do a method like this getTextSelected().setText("A"); compared to this textViewStudent1.setText("A"); as you can see if i went with the first one i can just get what ever textview was clicked and change it rather then code each indiviual id and then change it. Thanks for the advice will look into it =) – Charlie Feb 14 '14 at 18:29
2

I would advise you to use a grid view. You can see which textview was pressed like this:

gridView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View v, int position,
      long id) {

 //get id
      switch (v.getId()) {
          case R.id.textView1: ...
  }
});
Solenya
  • 694
  • 6
  • 21
  • Hi there, was thinking of using a gridview but didnt really comprehend how to code one... i think this is a good strategy and will try to code one on a smaller scale first and then go from there. Question can you make the columns and rows visible and invisible at will? Surely you can... if you have a look at my png pic you can see my layout and then you will understand why i went with that basic layout so i can alter tablerows and names etc. Hope this makes sense... thank you though and will look into it... – Charlie Feb 14 '14 at 18:24
  • Hey i looked into gridview today and realised that you cannot scroll horizontally. is this true? if so this is ideal for items that dont really need to follow a certain pattern but as you can see in the image i attached of the xml layout I'm coding i need it to scroll horizontally and vertically cause am looking at having up to 40 students so this strategy is probably not ideal.. unless iv missed something in making it scroll horizontally... – Charlie Feb 15 '14 at 10:33
0

Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.

Sush
  • 3,864
  • 2
  • 17
  • 35
0

I would suggest holding the textviews in an array, like so:

TextView[] textViewArray = new TextView[textViewCount];

Then using a for loop assign each one a tag of integer - it's position

textViewArray.setTag(i)

And add an onClickListener to each one, again using a for loop:

textviewArray[i].setOnClickListener(etc...)

Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:

textviewArray.setOnClickListener(new customOnClickListener())

Where your customOnClickListner is like this:

    private class customOnClickListener implements CompoundButton.{
    public void OnClick(View view){
        int position = (Integer) view.getTag()
        ///Do more code here - your processing
    }
}

Hope that makes sense :) For your for loops, you could use for(i = 0, i

Swedish Architect
  • 389
  • 3
  • 5
  • 23
  • Hey thanks for your answer... im a beginner at this but am willing to learn.. so will look into this method and see if i can use it. I like the array idea.. I have set a onClickLIstener to all my textViews and so when a user clicks one i know which textview has been click.. i think the problem is when it is clicked i have to programme a single dialog for each textview to be changed cause i dont know a getText Method where i can just identify which textview was clicked then assign a .setText("A") to that textView. Like is that what the view.getTag() does? i can just use this line of code? – Charlie Feb 14 '14 at 18:37
  • and then assign a setText(); method to it..and it will recognise which textView was clicked then change it... hope that makes sense... thank you though for the response... =) – Charlie Feb 14 '14 at 18:38
  • why was this downvoted, I wont understand. Helped me well. Thanks. – Kraken May 28 '17 at 08:02
0

You have to set "onClickListner" on all of of your textview. For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview, Like ,

    int i=0;
    ......


 textView1 = (TextView)findViewById(R.id.yourtextview1);
 textView2 = (TextView)findViewById(R.id.yourtextview2);
  ......
  ......
 // and so on, for your all textviews

   @Override
   public void onClick(View view) {

    if (view.equals(textView1)) {
         i = 1;
       CustomDialog(i);
     }
     //Similarly for all your textViews..
     .......... 

Make A function CustomDialog Like

  public void CustomDialog(int i){
   if(i==1){
    //Do something
   }   
  }
hemantsb
  • 2,049
  • 2
  • 20
  • 29
  • thanks for the response... is textView1 the id of the textview being created? or is it the text in the textview? like when i call view.equals(textView1) it looks to see if the id was clicked then perform some method(s). hope that makes sense... i was hoping to use just one dialog box for all textviews to use when clicked and inside the dialog there would be some method that would know which textview was being clicked then assign it a new string...thanks – Charlie Feb 14 '14 at 18:56