使用ArrayAdapter意味着不再使用循环了吗?

时间:2020-05-12 04:11:07

标签: arrays for-loop listview arraylist android-arrayadapter

所以我最近研究了ArrayAdapter和ListView,但是当我不同时使用ArrayAdapter和ListView时,我放置了一个for循环,但是当我使用ListView时,代码却完全不同,看起来我不再使用for循环了。我想知道ListView是否不需要循环?数据是否存储在Arraylist中?

案例1:使用LinearLayout

ArrayList<String> words= new ArrayList<String>();

words.add(0, "one");
words.add(1, "two");
words.add(2, "three");
words.add(3, "four")


LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);


for (int index= 0; index < 3; index++) {
                /*TextView wordView = new TextView(this);
                wordView.setText(words.get(index));
                rootView.addView(wordView);


Case 2: When using  listview + arrayadapter 

ArrayList<String> words= new ArrayList<String>();

words.add(0, "one");
words.add(1, "two");
words.add(2, "three");
words.add(3, "four");


ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, words);

ListView listView = (ListView) findViewById(R.id.list);

listView.setAdapter(itemsAdapter);

所以我想知道为什么ArrayAdapter和ListView中没有循环语句。

1 个答案:

答案 0 :(得分:0)

(ListView的)适配器从数据对象(在本例中为ArrayList)提取正确的数据,并将此数据分配给SELECT relname, idx_tup_fetch + seq_tup_read as TotalReads, n_tup_ins + n_tup_upd + n_tup_del as TotalWrites, -- (n_tup_ins + n_tup_upd + n_tup_del) / magical_seconds_column_that_doesnt_exist as WritesPerSecond * from pg_stat_all_tables order by totalwrites desc

行中的视图

适配器管理数据模型,并使它适应窗口小部件中的各个条目。适配器扩展了BaseAdapter类。

有关更多信息,您可以在这里查看:https://www.vogella.com/tutorials/AndroidListView/article.html 或官方的Android文档:https://developer.android.com/reference/android/widget/ListView