3

I have tried this, but I didn't understand how to make custom names for my variables. Here is the code that I want to make shorter:

    TextView viewmt1 = (TextView) v.findViewById(R.id.mt1);
    TextView viewmt2 = (TextView) v.findViewById(R.id.mt2);
    TextView viewmt3 = (TextView) v.findViewById(R.id.mt3);
    TextView viewmt4 = (TextView) v.findViewById(R.id.mt4);
    TextView viewmt5 = (TextView) v.findViewById(R.id.mt5);
    TextView viewmt6 = (TextView) v.findViewById(R.id.mt6);
    TextView viewmt7 = (TextView) v.findViewById(R.id.mt7);

    TextView viewtid1 = (TextView) v.findViewById(R.id.tid1);
    TextView viewtid2 = (TextView) v.findViewById(R.id.tid2);
    TextView viewtid3 = (TextView) v.findViewById(R.id.tid3);
    TextView viewtid4 = (TextView) v.findViewById(R.id.tid4);
    TextView viewtid5 = (TextView) v.findViewById(R.id.tid5);
    TextView viewtid6 = (TextView) v.findViewById(R.id.tid6);
    TextView viewtid7 = (TextView) v.findViewById(R.id.tid7);

    ImageView viewImage1 = (ImageView) v.findViewById(R.id.imageView1);
    ImageView viewImage2 = (ImageView) v.findViewById(R.id.imageView2);
    ImageView viewImage3 = (ImageView) v.findViewById(R.id.imageView3);
    ImageView viewImage4 = (ImageView) v.findViewById(R.id.imageView4);
    ImageView viewImage5 = (ImageView) v.findViewById(R.id.imageView5);
    ImageView viewImage6 = (ImageView) v.findViewById(R.id.imageView6);
    ImageView viewImage7 = (ImageView) v.findViewById(R.id.imageView7);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Bestem0r
  • 33
  • 9

3 Answers3

2

1.init TextViewImageView array

2.add for loop

3.get id of the view

4.findViewById in the code

Try this .

TextView[]  textViews1 = new TextView[7];
TextView[]  textViews2 = new TextView[7];
ImageView[]  imageViews = new ImageView[7];

for (int j = 0; j < 7; j++) {
    String viewmt = "mt" + (i + 1);
    String viewtid = "tid" + (i + 1);
    String viewImage = "imageView" + (i + 1);
    int resIDmt = getResources().getIdentifier(viewmt, "id", getPackageName());
    int resIDtid = getResources().getIdentifier(viewtid, "id", getPackageName());
    int resIDImage = getResources().getIdentifier(viewImage, "id", getPackageName());

    textViews1[j] = ((TextView) v.findViewById(resIDmt));
    textViews2[j] = ((TextView) v.findViewById(resIDtid));
    imageViews[j] = ((ImageView) v.findViewById(resIDImage));
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

You can't really define variable names at compile time. The easiest solution will be to store all those views into an array or something like that. Here is an example:

class Component{
    TextView mt;
    TextView tid;
    ImageView img;
    public Component(TextView mt, TextView tid, ImageView img){
        this.mt = mt;
        this.tid = tid;
        this.id = id;
    }
}

.....
Component[] components = new Component[7];
for(int i = 0; i < components.length; i++){
   TextView mt = (TextView) v.findViewById(getResources().getIdentifier("mt" + (i+1), "id", getPackageName()));
   TextView tid = (TextView) v.findViewById(getResources().getIdentifier("tid" + (i+1), "id", getPackageName()));
   ImageView img = (ImageView) v.findViewById(getResources().getIdentifier("imageView" + (i+1), "id", getPackageName()));
   components[i] = new Component(mt, tid, img); 
}
....

And to access those you can use something like this

Component c2 = components[3];
c2.mt.setText(......);
Titus
  • 22,031
  • 1
  • 23
  • 33
0

If the TextView's are listed in the correct order in your XML layout you can do something like this, assuming they are contained in a RelativeLayout but it should work the same for other Layout Class.

  RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.relLayout1);
  ArrayList<TextView> mTextViews = new ArrayList<>();

  for (int i = 0; i < mLayout.getChildCount(); i++) {
      View view = mLayout.getChildAt(i);
      if (view instanceof TextView)
            mTextViews.add((TextView)view);
  }

To get later one of the TexView myTextView = mTextViews.get[index];

from56
  • 3,976
  • 2
  • 13
  • 23