Looping through ArrayList or ArrayDapter of ListView or the ListView itself (Android)?

huangapple go评论83阅读模式
英文:

Looping through ArrayList or ArrayDapter of ListView or the ListView itself (Android)?

问题

以下是翻译好的内容:

我想知道在Android Studio中访问列表视图项的最佳实践是哪种方式。
我是从数组列表、数组适配器还是列表视图本身访问它们?

例如:

我想循环遍历列表视图的项:

ListView listView = findViewById(R.id.listView);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("x");
arrayList.add("y");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);

方法1:

for(int i = 0; i < 2; i++)
{
        arrayList.get(i);
}

方法2:

for(int i = 0; i < 2; i++)
{
        arrayAdapter.getItem(i);
}

方法3:

for(int i = 0; i < 2; i++)
{
        listView.getItemAtPosition(i).toString();
}

注意:这段代码不是用于测试的。我只是问哪种方式是访问列表视图项的最佳实践(从复杂性的角度),因为我认为这三种方法都会产生相同的结果。

英文:

I'm wondering which way is best practice to access items of a list view in Android Studio.
Do I access them from the array list, array adapter or the listview itself?

e.g

I want to loop through the items of a list view:

ListView listView = findViewById(R.id.listView);
ArrayList &lt;String&gt; arrayList = new ArrayList&lt;String&gt;();
arrayList.add(&quot;x&quot;);
arrayList.add(&quot;y&quot;);
ArrayAdapter &lt;String&gt; arrayAdapter = new ArrayAdapter&lt;&gt;(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);

Method 1:

for(int i = 0; i &lt; 2; i++)
{
        arrayList.get(i);
}

Method 2:

for(int i = 0; i &lt; 2; i++)
{
        arrayAdapter.getItem(i);
}

Method 3:


for(int i = 0; i &lt; 2; i++)
{
        listView.getItemAtPosition(i).toString();
}

NOTE: THE CODE IS NOT WRITTEN FOR TESTING. I'M JUST ASKING WHICH WAY IS BEST PRACTICE (COMPLEXITY WISE) TO ACCESS THE LIST VIEW ITEMS SINCE I BELIEVE ALL 3 METHODS PRODUCE THE SAME RESULTS.

答案1

得分: 0

最佳实践是通过ListView获取项目(方法3

在方法1和方法2中,在将列表或适配器分配给ListView之后,必须保持引用。

在方法3中,ListView在内部调用适配器,并且在返回对象之前在方法中进行了所有必要的检查。

ListView源代码

 /**
 * 获取与列表中指定位置相关联的数据。
 *
 * @param position 要获取的数据位置
 * @return 与列表中指定位置相关联的数据
 */
public Object getItemAtPosition(int position) {
    T adapter = getAdapter();
    return (adapter == null || position < 0) ? null : adapter.getItem(position);
}
英文:

The best practise is to get the item through the listview(Method 3)

In Method 1 and Method 2, you have to hold the reference of the list or the adapter after assigning it to the ListView

In Method 3, the ListView calls the adapter internally and all the necessary checks has been made in the method before returning the object.

ListView Source Code

 /**
 * Gets the data associated with the specified position in the list.
 *
 * @param position Which data to get
 * @return The data associated with the specified position in the list
 */
public Object getItemAtPosition(int position) {
    T adapter = getAdapter();
    return (adapter == null || position &lt; 0) ? null : adapter.getItem(position);
}

huangapple
  • 本文由 发表于 2020年10月7日 02:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64231920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定