Android - Addview using LayoutInflater in a loop
Code như này:
LinearLayout parent = ((LinearLayout)findViewById(R.id.parent_view)); for (int i = 0; i < 5; i++) { View r = LayoutInflater.from(getBaseContext()).inflate(R.layout.viewitem, parent); ((TextView)r.findViewById(R.id.text1)).setText("item "+i); }
Định add 5 thằng view con trong đấy có 1 textview và setText cho từng textview đó. Khi chạy thì chỉ TextView đầu tiên có chữ, và chữ là "item 4" .
Solution:
for (int i = 0; i < 5; i++) { View r = LayoutInflater.from(getBaseContext()).inflate(R.layout.viewitem, parent, false); ((TextView)r.findViewById(R.id.text1)).setText("item "+i); parent.addView(r); }







