【问题标题】:Adding arraylist data to listview将arraylist数据添加到listview
【发布时间】:2015-03-22 08:26:35
【问题描述】:

我必须显示消息列表并使其可点击,我已阅读 ListView 并尝试使用它,但我有类似以下代码的数据,我将如何添加到适配器?从这个列表中,我将循环并获取消息说 list.get(i).getMessage();必须显示并且有多个消息。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.reminderlist);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,   R.layout.reminderlayout);
        setContentView(R.layout.reminderlayout);
        getWindow().getDecorView().setBackgroundColor(Color.WHITE);
        ArrayList<GetReminder> list = (ArrayList<GetReminder>) getIntent().getSerializableExtra("reminderList");
        System.out.println("size is >>>"+list.size());
        // Binding resources Array to ListAdapter
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    实现使用GetReminder 参数化的自定义适配器。以here 为例。你可以这样做:

    ArrayList<GetReminder> list = (ArrayList<GetReminder>) getIntent().getSerializableExtra("reminderList");
    ListView listView = findViewById(R.id.listview);
    MyCustomAdapter adapter = new MyCustomAdapter(this, 0);
    adapter.addAll(list)
    

    或者只是将您的列表作为第三个参数传递给 MyCustomAdapter 构造函数。

    【讨论】:

    • 我要从列表中获取消息,例如...list.get(i).getMessage();并且它们是要在列表中显示的“n”条消息,您的代码是否可行?
    • 请检查示例中的 getView 方法是我给出的。此方法的每次调用代表列表中的 1 行。在这里,您必须编写实现如何在 ListView 中显示列表中的信息。
    • 好的,我已经通过它,我什至在最后找到了完整的例子,但是如何实现onClick事件
    • 你找到了吗?我给了你一个完整示例的链接。 'listView.setOnItemClickListener' 以分配项目点击监听器。
    • 我关注你有没有说过stackoverflow.com/questions/8166497/… Louis 的最后一篇文章,仍在努力
    【解决方案2】:

    你可以的

    setListAdapter(new ArrayAdapter<GetReminder>(getApplicationContext(), R.layout.reminderlayout,(GetReminder[]) list.toArray()));
    

    考虑到此代码位于您的类的 onCreate() 方法中,该方法扩展了 ListActivity

    【讨论】:

    • 我要从列表中获取消息,例如...list.get(i).getMessage();并且它们是要在列表中显示的“n”条消息,您的代码是否可行?
    • 在这种情况下,遍历您的列表,获取存储在数组中的所有消息并将其传递给 Adapters 构造函数。
    • 你的意思是存储在字符串[]
    • 是的,如果这是 getMessage() 返回的内容。
    • 它会返回一个字符串
    猜你喜欢
    • 2020-05-16
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多