简体   繁体   中英

SimpleAdapter with two different colored custom row

I have used two different colored custom row. But it returns the same row which is black. I have a yellow row too, but it return only a black one. I couldnt understant it, thus I have shared my source codes below. Please, help me

json_orders = json.getJSONArray("orders");
                for(int i = 0; i < json_orders.length(); i++){
                JSONObject c = json_orders.getJSONObject(i);

                // Storing  JSON item in a Variable
                String orderDate = c.getString("orderDate");
                String orderTime = c.getString("orderTime");
                String orderId = c.getString("orderId");
                String driverStatusId = c.getString("driverStatusId");                  

                // Adding value HashMap key => value


                HashMap<String, String> map = new HashMap<String, String>();

                map.put("orderDate", orderDate);
                map.put("orderTime", orderTime);
                map.put("orderId", orderId);

                oslist.add(map);
                list=(ListView)view.findViewById(R.id.list);

                ListAdapter  adapter = new SimpleAdapter(getActivity(), oslist, i, null, null);

                if (!driverStatusId.equals("4"))
                {
                    adapter = new SimpleAdapter(getActivity(), oslist,
                            R.layout.list_row_new,
                            new String[] { "orderDate", "orderTime"}, new int[] {
                            R.id.orderDate, R.id.orderTime});
                }
                else {
                    adapter = new SimpleAdapter(getActivity(), oslist,
                            R.layout.list_row,
                            new String[] { "orderDate", "orderTime"}, new int[] {
                            R.id.orderDate, R.id.orderTime});
                }

                list.setAdapter(adapter);

                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        //Toast.makeText(getActivity(), "You Clicked at "+oslist.get(+position).get("orderId"), Toast.LENGTH_SHORT).show();     

                        Fragment fragment = null;
                        fragment = new OrderDetailsFragment();

                        final Bundle bundle = new Bundle();
                         bundle.putString("orderId", oslist.get(+position).get("orderId"));
                         Log.i("BUNDLE", bundle.toString());
                         fragment.setArguments(bundle);

                        FragmentManager fragmentManager = getFragmentManager();
                        fragmentManager.beginTransaction()
                                .replace(R.id.frame_container, fragment, "OrderDetailsFragment").commit();

                        //new UpdateDestinationInfo().execute(String.valueOf(db.getUserDetails().get("currentorderid")), oslist.get(+position).get("longlat"));
                       // Toast.makeText(getActivity(), "You Clicked at "+String.valueOf(db.getUserDetails().get("id")), Toast.LENGTH_SHORT).show();

                    }

If you have multiple views in a ListView you're going to need more than a SimpleAdapter because you need to override a few methods. You'll want to end up extending a BaseAdapter to make a custom adapter. The first method that you need when making your own adapter is getViewTypeCount() . This tells the adapter that there are a certain amount of view types and that it should account for that. The second method you need to override is getItemViewType() so that the ListView knows which views have what type so it can handle view recycling accordingly. Then in your getView() method, based on the position, you return a different type of view, based on what view type it is.

You can use multiple view types as the other answer suggested or you can just set the color of the list item programatically based on the position inside of getView.

Either way you'll need a custom adapter.

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