简体   繁体   中英

HashMap in ArrayList viewing on Listview Android

All my code so far:

I am putting the string that I want to show on the screen in List of hardlist .(getDestinationCity and getTotalPrice) However I see only getTotalPrice values on the screen. getDestinationCity field is empty. I guess I only show the Values (getTotalPrice) on screen except Keys (getDestinationCity).

How can I figure it out?

    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

I think you should do like this:

//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);

So you will have a list of maps, each map having two entries (so having two key and their associated values).
Also, read carefully the meaning of the SimpleAdapter constructor parameters:

Constructor
@param context The context where the View associated with this SimpleAdapter is running
@param data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
@param resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
@param from A list of column names that will be added to the Map associated with each item.
@param to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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