【问题标题】:HashMap in ArrayList viewing on Listview Android在 Listview Android 上查看 ArrayList 中的 HashMap
【发布时间】:2016-10-01 10:10:14
【问题描述】:

到目前为止我的所有代码:

我将要在屏幕上显示的字符串放在 hardlist 列表中。(getDestinationCity 和 getTotalPrice) 但是我在屏幕上只看到 getTotalPrice 值。 getDestinationCity 字段为空。我想我只在屏幕上显示除键 (getDestinationCity) 之外的值 (getTotalPrice)。

我怎么知道?

    public class MainActivity extends AppCompatActivity { 

    List<HashMap<String, String>> hardlist = new ArrayList<HashMap<String, String>>();

            String getTotalPrice = null;
            String getDestinationCity = null;
            ListView lv;
            ListAdapter listAdapter;

        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                lv= (ListView)findViewById(R.id.listView1);

                deserilaze();

                String[] from = new String[]{getDestinationCity, getTotalPrice};
                int[] to = new int[]{R.id.name, R.id.price};

                listAdapter = new SimpleAdapter(this, hardlist,  R.layout.list_item, from, to);

                lv.setAdapter(listAdapter);
        }

           public void deserilaze(){

                Gson gson = new Gson();

                AirJson airJson = gson.fromJson(airFullJson, AirJson.class);

                for (Itinerary itini : airJson.getResult().getItineraries()) {
        //            System.out.println("\n");
                    String getSelectionID = itini.getSelectionId();
                    getTotalPrice = itini.getPriceInfo().getPriceBreakdown().getTotalPrice().toString();
                    String getCurrency = itini.getPriceInfo().getCurrency();

        //            System.out.println("Itinerary: " + "\n" + getSelectionID + "\n" + getTotalPrice + "\t" + getCurrency);


                    for (Trip tripin : itini.getTrips()) {
                        getDestinationCity = tripin.getDestinationCity();
                        String getOriginCity = tripin.getOriginCity();
                        String getValidatingCarrier = tripin.getValidatingCarrier();
                        String getDepartureTime = tripin.getDepartureTime();
                        String getArrivalTime = tripin.getArrivalTime();

        //                System.out.println("Trip: " + "\n" + getDestinationCity + "\n" + getOriginCity + "\n" + getValidatingCarrier + "\n" +
        //                        getDepartureTime + "\n" + getArrivalTime);

                        for (Segment segmenti : tripin.getSegments()){

                            String getSegmentOrigin = segmenti.getDepartureTime();
                            String getSegmentDestination = segmenti.getArrivalTime();

        //                    System.out.println("Segment: " + "\n" + getSegmentOrigin + "\n" + getSegmentDestination);

                        }

                    }

                    HashMap<String, String> list = new HashMap<String, String>();
                    list.put(getDestinationCity, getTotalPrice);

                    hardlist.add(list);

                }

            }
}

This is the debug result

【问题讨论】:

    标签: java android list listview hashmap


    【解决方案1】:

    我认为你应该这样做:

    //these are the names of the columns
    String[] from = new String[]{"DestinationCity", "TotalPrice"};
    ...
    //you associate to the key DestinationCity the value getDestinationCity
    list.put("DestinationCity", getDestinationCity);
    //you associate to the key TotalPrice the value getTotalPrice
    list.put("TotalPrice", getTotalPrice);
    

    因此您将获得一个映射列表,每个映射有两个条目(因此有两个键及其关联的值)。
    另外,请仔细阅读 SimpleAdapter 构造函数参数的含义:

    构造函数
    @param context 与此 SimpleAdapter 关联的 View 正在运行的上下文
    @param data 地图列表。列表中的每个条目对应于列表中的一行。地图包含每一行的数据,并应包括“来自”中指定的所有条目
    @param resource 定义此列表项的视图的视图布局的资源标识符。布局文件应至少包含“to”中定义的那些命名视图
    @param from 将添加到与每个项目关联的地图中的列名列表。
    @param to 应该在“from”参数中显示列的视图。这些都应该是 TextViews。此列表中的前 N ​​个视图被赋予 from 参数中前 N 列的值。

    【讨论】:

    • 但我将来会有数千个字段。我认为手动编写它们不是一个好主意。我通过 getDestinationCity、getTotalPrice 引用了字符串。
    • from 参数是A list of column names that will be added to the Map associated with each item。所以在这里你只放你的列的名字......你会有成千上万的列吗?我猜不是……
    猜你喜欢
    • 2016-05-07
    • 2015-07-08
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多