There are TextView and ImageView

I can not understand how you can display the value of the string and the picture. The values ​​in xml are written as stroka1 , stroka2 , stroka3 , and the pictures are named img1, img2 , img3 .

I have:

 int nomerstroki; // где значение для строки. 

How can I substitute a value for text output from String and Drawable ? I fight for two days, I found that there is a dynamic query, but I did not understand anything ...

The code is simple to horror, there are only two elements in the layout - text and image, and their values ​​are programmed in the activation:

 TextView mDateName = (TextView) findViewById(R.id.text_3); ImageView imgview = (ImageView) findViewById(R.id.img_3); 

specify for each value a lot of code. so HOW to specify TO

 int get_num = ActivityUtil.getNum(); TextView mDateName = (TextView) findViewById(R.id."text_"+get_num ); ImageView imgview = (ImageView) findViewById(R.id."img_"+get_num ); 

Of course, with this option, the text text_3 and img_3 , but it is necessary that the value be derived from the string

  • and what language? Add it to the question mark. - no news
  • You have something too smart. Describe the task more fully. You have three elements and writing 6 lines is difficult? Or do you have a list? and you invent a ListView (RecyclerView)? Btw, in the resources you can specify not only strings, but also arrays of strings ... And forget about the construction of identifiers, this is a crutch path. - Yura Ivanov
  • Indeed, line identifiers are a torment ... when there are a lot of them ... And when there are few, the same thing, because if there is an array, then it should be kept constantly in your head or you can create a separate file because you can constantly look at what is written in line 329 bothers you ... - Roman Zykov

2 answers 2

The fact is that R.id.text_3 (and the others) is essentially an int type constant and dynamically its name can be obtained only through Reflection (experts, correct if not so), which will be much more difficult, slow and potentially buggy, rather than just copying the code for all the necessary elements.

On the other hand, you can use tags. Here they are just a string and their names can be generated dynamically.

https://developer.android.com/reference/android/view/View.html#findViewWithTag%28java.lang.Object%29

  • Here is an example "somevalue" + intVariable.toString () for (Integer i = 1; i <3; i ++) {int id = getResources (). GetIdentifier ("somevalue" + i.toString (), "string", getPackageName ()); String value = (String) getResources (). GetText (id); } I don't understand how it works - Roman Zykov
  • getIdentifier works through Reflection. And it is always slow. - rjhdby
  • Not through reflection, but still slowly, yes. - Yura Ivanov

I found the output so far - it works quite quickly, however, from the aesthetic part - there are many duplicate lines that can be replaced with one, but have not yet found the way to do it.

  TextView text_kin_descr = (TextView) findViewById(R.id.date_name); ImageView img_kin = (ImageView) findViewById(R.id.img_kin); //получение значения int kin_n = Actyvity.getK(); //тут перебираем значения для 1 2 3 4 5 и тд if(kin_n == 1 ) {text_kin_descr.setText(string.kin1 ); img_kin.setImageResource(drawable.img_kin1 );} 

Have a great day!