如何将ArrayList数据设置到ListView中?

发布于 2024-12-02 18:21:37 字数 339 浏览 0 评论 0原文

我在 ArrayList 中有数据,例如:

[android_metadata, test1, test5, test4, test2, test3, test10, test1002, abcd, zxy, re, test1001, testing, test21, test12]

现在我想将这些数据设置到 android 中的 ListView 中。 请在这件事上帮助我。 我见过很多例子。但我是否必须实现 getter setter 类来存储数据并将其显示到 ListView 中? 我只需将数据列表上方显示到列表视图中并设置其 ClickEvent。 请给我一些代码来完成这样的实现。

I have Data in ArrayList like:

[android_metadata, test1, test5, test4, test2, test3, test10, test1002, abcd, zxy, re, test1001, testing, test21, test12]

Now i want to Set such data in to the ListView in android.
Please help me in this matter.
I have seen many Example. but should i have to implement getter setter class to store data and display it into ListView ?
I have just to show Just above List of data in to List View and set its ClickEvent.
Please Give me Some code to done such implement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-12-09 18:21:37

试试这个代码

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Start extends Activity {

private String[] lv_arr = {};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // Get a handle to the list view
    ListView lv = (ListView) findViewById(R.id.ListView01);

    // Convert ArrayList to array
    lv_arr = (String[]) arrayList.toArray();
    lv.setAdapter(new ArrayAdapter<String>(Start.this,
            android.R.layout.simple_list_item_1, lv_arr));

}


}

try this code

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Start extends Activity {

private String[] lv_arr = {};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // Get a handle to the list view
    ListView lv = (ListView) findViewById(R.id.ListView01);

    // Convert ArrayList to array
    lv_arr = (String[]) arrayList.toArray();
    lv.setAdapter(new ArrayAdapter<String>(Start.this,
            android.R.layout.simple_list_item_1, lv_arr));

}


}
随波逐流 2024-12-09 18:21:37

这是例子

ArrayList yourlist = new ArrayList();

ArrayAdapter adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, yourlist);
yourlistview.setAdapter(adapter);

This is the example

ArrayList yourlist = new ArrayList();

ArrayAdapter adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, yourlist);
yourlistview.setAdapter(adapter);
疯到世界奔溃 2024-12-09 18:21:37

作为对 Balaji.K 答案的补充,您不需要 String[] 数组。
您可以直接使用 ArrayAdapter 将 ArrayList 附加到 ListView:

private ArrayList<String> arrayList = Arrays.asList("android_metadata", "test1", "test5", "test4", /*...*/ "test21", "test12");

ListView lv = (ListView) findViewById(R.id.list_view);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList));

As a complement to Balaji.K's answer, you don't need the String[] array.
You can attach an ArrayList to a ListView by using an ArrayAdapter directly:

private ArrayList<String> arrayList = Arrays.asList("android_metadata", "test1", "test5", "test4", /*...*/ "test21", "test12");

ListView lv = (ListView) findViewById(R.id.list_view);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文