Question

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...

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

Was it helpful?

Solution 3

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

OTHER TIPS

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

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.

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.

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
   }   
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top