Reputation: 1249
I know the question might sounds weird (feel free to edit the title)
, but what I want to accomplish basically is this:
Let's say that I have a list of 4 strings:
ArrayList<String> carList= new ArrayList<>;
carList.add("BMW");
carList.add("GMC");
carList.add("KIA");
carList.add("Honda");
I want now to print 3 of the list items into 3 textviews, the fourth item will be excluded for some reason and its position is known.
int excludedIndex = 2; //for example.
for (int i = 0; i < carList.size(); i++) {
if (i != excludedIndex) {
textView_1.setText(carList.get(i)); // here it will put (BMW) in tv1
textView_2.setText(carList.get(??)); // here it should put (GMC) in tv2
textView_3.setText(carList.get(??)); // here it should put (Honda) in tv3
}
}
Upvotes: 1
Views: 68
Reputation: 634
you don't have to use for loop
ArrayList<String> carList= new ArrayList<>();
carList.add("BMW");
carList.add("GMC");
carList.add("KIA");
carList.add("Honda");
textView_1.setText(carList.get(0)); // here it will put (BMW) in tv1
textView_2.setText(carList.get(1)); // here it should put (GMC) in tv2
textView_3.setText(carList.get(3)); // here it should put (Honda) in tv3
Upvotes: 1
Reputation: 140613
Basically you are asking: how do I map elements of a list (identified by their index) to a certain text field. And that sentence already contains one possible solution - by using a Map<Integer, TextView>
that knows which text view should be used for which index.
As in:
Map<Integer, TextView> viewsByIndex = ...
for (int i = ... ) {
if (viewsByIndex.containsKey(i)) {
viewsByIndex.get(i).setText(listItem(i));
The above code isn't compiled/checked - it is rather meant as inspiration/pseudo code showing how one could solve this in an elegant way.
Upvotes: 2