简体   繁体   English

如何从 ListView 获取项目并将这些项目转换为 ArrayList?

[英]How can I get the items from a ListView and convert these items to an ArrayList?

How can I get the items from a ListView and convert them to an ArrayList ?如何从 ListView 获取项目并将它们转换为ArrayList

https://i.stack.imgur.com/kWHzT.png

Screenshot: (in mConversationArrayAdapter must go the arraylist)截图:(在mConversationArrayAdapter中必须是 go 的arraylist)

You have to create a method in your adapter that gets the arrayList and then call that method where you need the data.您必须在适配器中创建一个获取 arrayList 的方法,然后在需要数据的地方调用该方法。

Something like this,像这样的东西,

 public ArrayList<String> getData(){ return this.arrayList; }

You have to create a method in the adapter to get the values like this:您必须在适配器中创建一个方法来获取如下值:

public Object getItem(int position) {
        return mConversationArrayAdapter.get(position);
    }

Or get theem directly in your class.或者直接在您的 class 中获取它们。

But if you want to display the list, you must use " RecyclerView " is better than ListView.但是如果要显示列表,就必须使用“ RecyclerView ”,效果比ListView好。

You should try like this.你应该这样尝试。

ListView listView = new ListView();

    listView.getItems().add("Item1;");
    listView.getItems().add("Item;2");
    listView.getItems().add("Item3;");

     for (int i=0;i<listView.getItems().size();i++){
         String currentLine= (String) listView.getItems().get(i);
         String[] cells=currentLine.split(";");
         System.out.println(currentLine);
         System.out.println(Arrays.toString(cells));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM