I struggled with this problem for two days and still can not find a solution, I think that my basic knowledge of OOP is bad.
Now I declared about twenty TextView and I want to know if there is a way to save TextView into an array and findViewById them?
I tried using an array, for example:
public class MainActivity extends Activity { private TextView name, address; LinkedHashMap<Integer, TextView> demo = new LinkedHashMap<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int temp; allTextview = new TextView[]{name, address}; for(int i=0; i<allTextview.length; i++){ temp = getResources().getIdentifier(allTextview[i], "id", getPackageName()); allTextview[i] = (TextView)findViewById(temp); } }}
This method calls "name" and "allTextview [0]" does not point to the same object. I also use this solution , but still the same.
I think the reason is that the "name" and "address" were just declared and do not point to any object, how can I solve it?
I want to use for loop in findViewById , and I can use both "name" and "allTextview [0]" to do something with TextView .
Thanks for the help, and please excuse my poor English.
source share