1

I want to minimise the code by using for loop with TextView objects a,b,c,d,e as elements of an array. Both for findViewById and setOnClickListener implementations. Any actionable walkthrough for this particular coding is really appreciated!^__^

  • The following is how I usually do the TextView implementations. But I am tired of writing so many lines needlessly.

    TextView a,b,c,d,e;

        a=(TextView)findViewById(R.id.A);
        b=(TextView)findViewById(R.id.B);
        c=(TextView)findViewById(R.id.C);
        d=(TextView)findViewById(R.id.D);
        e=(TextView)findViewById(R.id.E);
    
        a.setOnClickListener(this);
        b.setOnClickListener(this);
        c.setOnClickListener(this);
        d.setOnClickListener(this);
        e.setOnClickListener(this);
    
  • My question is if I can use a loop to set all the already initialized TextView objects to call setOnClickListener() without any trouble as shown below:

    TextView a,b,c,d,e;
    
    a=(TextView)findViewById(R.id.A);
    b=(TextView)findViewById(R.id.B);
    c=(TextView)findViewById(R.id.C);
    d=(TextView)findViewById(R.id.D);
    e=(TextView)findViewById(R.id.E);
    

    TextView[] textViews = {a, b, c, d, e};

    for (int count = 0; count < textViews.length; count++) {
        textViews[count].setOnClickListener(this);
    }
    

**

  • All I want to know is how to initialize the TextView objects a,b,c,d,e in the same way, I showed to setOnClickListener for them? Something like this,

    TextView[] textViews = {a, b, c, d, e};
    
    int[] textViewIds = {R.id.a, R.id.b, R.id.c, R.id.d, R.id.e};
    
    for (int count = 0; count < textViews.length; count++) {
        textViews[count] = (TextView)findViewById(textViewIds[count]);
    }
    

**

2
  • 3
    Put the ids into a list/array and loop over them? Commented Oct 5, 2017 at 7:43
  • yup. I would agree with @bub Commented Oct 5, 2017 at 7:44

3 Answers 3

3

This is how I would write it (I dont have a compiler here so sorry for mistakes)

ArrayList<TextView> textViews = new ArrayList<TextView>();

int[] tvIds = {R.id.A,R.id.B,R.id.C,R.id.D,R.id.E};

for(int index= 0; index<tvIds.length/* or .count forgot sorry*/; index++){   

 TextView tv = (TextView)findViewById(tvIds[index]));
 tv.setOnClickListener(this);

 textViews.add(tv);

}

Or you could use what @Hank Moody answered, butterknife is pretty straightforward.

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

Comments

2

I'm using ButterKnife for views binding, and it have a great ways to do what you want.

First of all you can bind views in List

@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<TextView> nameViews;

Then you can apply diffrent operations to this list with "Action", like this:

static final ButterKnife.Action<View> SET_CLICK = new ButterKnife.Action<View>() {
@Override
public void apply(View view, int index) {
    view.setOnClickListener(....listener);
  }
};

And then apply this ACTION

ButterKnife.apply(nameViews, DISABLE);

See examples here

UPD: if you are using Kotlin, see Kotter Knife

Comments

2

//Declare view objects as global variables in the beginning of a class

View v_home, v_earning, v_rating, v_account;

//Create view array for views and int array for view IDs

View[] views ={v_home, v_earning, v_rating, v_account};
int[] viewsID ={R.id.v_home, R.id.v_earning, R.id.v_rating, R.id.v_account};

//Then findViewById and setOnClickListener inside for loop

for (int v=0;v<views.length;v++){
        views[v] = findViewById(viewsID[v]);
        views[v].setOnClickListener(this);
    }

Comments

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.