如何从像图像的json响应更新UI

时间:2017-01-05 18:24:26

标签: android json android-layout

您好我正在制作一个应用程序,需要在5列中显示listview中的席位。我正在使用aynctask来解析json中的值。基于我要显示的值。任何人请告诉我如何显示

seats in listview.Please anybody tell me any logics to display seats like this.

我需要什么: enter image description here 我得到了什么:

enter image description here     我的json是:

[
   {
      "row1":[
         {
            "seat_name ":"A1",
            "ticket_status":"booked",
            "seatIsLadies":"true"
         },
         {
            "seat_name ":"A2",
            "ticket_status":"Not booked",
            "seatIsLadies":"true"
         },
         {
            "seat_name ":"A3",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"A4",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"A5",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"A6",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"A7",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"A8",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"A9",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"A10",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"A11",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         }
      ],
      "row2":[
         {
            "seat_name ":"B1",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B2",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B3",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B4",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B5",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B6",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B7",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B8",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B9",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B10",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"B11",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         }
      ],
      "row3":[
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"NA",
            "ticket_status":"",
            "seatIsLadies":""
         },
         {
            "seat_name ":"C11",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         }
      ],
      "row5":[
         {
            "seat_name ":"D1",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D2",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D3",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D4",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D5",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D6",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D7",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D8",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D9",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D10",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         },
         {
            "seat_name ":"D11",
            "ticket_status":"Not booked",
            "seatIsLadies":"false"
         }
      ]
   }
]

在我的json中我有seat_name.if seat_name是NA然后我们想在listview中显示一个空格,否则我们想要显示座位图像。

我的代码:`

public class Seatparsing extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {

        String response;
        String jsonstr, seat_name, ticket_status, seatladies;
        ProgressDialog dialog = new ProgressDialog(Seatvisible.this);
        AndroidHttpClient maclient = AndroidHttpClient.newInstance("");

        @Override
        protected void onPreExecute() {
            dialog.setCancelable(false);
            dialog.setMessage("Please wait..");
            dialog.setIndeterminate(false);
            dialog.show();

        }

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(String... params) {


            HttpGet request = new HttpGet(url);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            try {
                response = maclient.execute(request, responseHandler);
            } catch (ClientProtocolException exception) {
                exception.printStackTrace();
                return null;
            } catch (IOException exception) {
                exception.printStackTrace();
                return null;
            }
            Log.i("Seats", "" + response);
            Log.i("seaturl", "" + url);

            jsonstr = response;
            busdetails1 = new ArrayList<>();

            if (jsonstr != null) {

                HashMap<String, String> map2 = new HashMap<>();
                try {
                    JSONArray array1 = new JSONArray(jsonstr);
                    for (int i = 0; i < array1.length(); i++) {
                        JSONObject objct1 = array1.getJSONObject(i);
                        JSONArray array2 = objct1.getJSONArray("row1");
                        for (int j = 0; j < array2.length(); j++) {
                            JSONObject objct2 = array2.getJSONObject(j);

                            seat_name = objct2.getString("seat_name ").toString();
                            ticket_status = objct2.getString("ticket_status");
                            seatladies = objct2.getString("seatIsLadies");

                            map2.put("seat_name", seat_name);
                            map2.put("ticket_status", ticket_status);
                            map2.put("seatIsLadies", seatladies);

                        }
                        busdetails1.add(map2);
                    }
                } catch (Exception e) {
                e.printStackTrace();
                }
            }

            return busdetails1;

        }

        protected void onPostExecute(ArrayList<HashMap<String, String>> busdetails1) {

            try {
                dialog.dismiss();
                customadapter adapter = new customadapter(getApplicationContext(), R.layout.seatlistview, busdetails1);
                lv.setAdapter(adapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class customadapter extends ArrayAdapter {

        private ArrayList<HashMap<String, String>> list;
        private int resource;
        private LayoutInflater inflater;

        public customadapter(Context context, int resource, ArrayList<HashMap<String, String>> busdetails1) {
            super(context, resource, busdetails1);

            list = busdetails1;
            this.resource = resource;
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);


        }

        @NonNull
        @Override
        public View getView(int i, View convertView, ViewGroup parent) {

            convertView = inflater.inflate(resource, null);

            ImageView imageView = (ImageView) convertView.findViewById(R.id.imageViewseat);

            String seat_name = list.get(i).get("seat_name");
            String ticket_status = list.get(i).get("ticket_status");
            String seatIsLadies = list.get(i).get("seatIsLadies");
            for(int l=0;l<busdetails1.size();l++) {
                if (!seat_name.equals(null)) {

                    if (ticket_status.equals("booked")) {
                        if (seatIsLadies.equals("true")) {
                            imageView.setImageResource(R.drawable.reservedladies);
                            imageView.setClickable(false);
                        } else {
                            imageView.setImageResource(R.drawable.bookedseat);
                            imageView.setClickable(false);
                        }

                    } else {
                        imageView.setImageResource(R.drawable.available_seat_img);


                    }

                } else {

                    imageView.setImageDrawable(null);
                    imageView.setClickable(false);


                }
            }
            return convertView;
        }
    }`

我的xml文件:`

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_seatvisible"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.notebook.runtowin.Activity.Seatvisible">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <TextView
            android:id="@+id/from_to"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimaryDark"
            android:text="Select your seat"
            android:padding="5dp"
            android:gravity="center_vertical"
            android:textStyle="bold"
            android:textColor="@color/colorwhite" />

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/from_to">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#FFEB3B"
            android:id="@+id/headinglayout">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/busname"
                    android:padding="5dp"
                    android:text="RUN TO WIN"
                    android:textStyle="bold"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_below="@+id/busname"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/seatnumber"
                    android:padding="5dp"
                    android:text="Seats :"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:text="25,24"
                    android:id="@+id/seatsnumber"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ticketprice"
                android:layout_alignParentRight="true"
                android:text="hsdshd"
                android:textStyle="bold"
                android:padding="5sp"
                android:textSize="30sp"
                android:layout_marginRight="10dp"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_below="@+id/headinglayout"
            android:layout_marginTop="3dp"
            android:paddingTop="10dp"
            android:paddingRight="25dp"
            android:paddingLeft="25dp">

            <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:background="@drawable/iconsteer"
                android:id="@+id/imageView2"
                android:layout_marginEnd="50dp"
                android:layout_alignParentEnd="true"/>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               android:layout_below="@+id/imageView2"
                android:layout_marginTop="20dp"
                android:numColumns="5"
                android:id="@+id/listviewseat">

            </ListView>


        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_alignParentEnd="true"
            android:gravity="center_horizontal"
            android:background="@color/colorPrimaryDark"
            >
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="BOOK TICKETS"
                android:padding="10dp"
                android:id="@+id/bt_book"
                android:textSize="20sp"/>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>
</ScrollView>

Listview的我的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8sp"
    android:gravity="top">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewseat"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试使用baseadpater而不是arrayadapter。

public class Studentloader extends BaseAdapter implements OnClickListener{

    private Context slcntx;
    private ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;
    private String url;

    public Studentloader() {
        this.data = new ArrayList<HashMap<String, String>>();
    }


    public Studentloader(Context ctx, ArrayList<HashMap<String, String>> d) {
        slcntx = ctx;
        data=d;

        inflater = (LayoutInflater)slcntx.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        imageLoader = new ImageLoader(slcntx);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public void clearData() { data.clear(); }

    /********* Create a holder Class to contain inflated xml file elements *********/
    public static class ViewHolder{

        public TextView studentTV;

        ImageView image;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        ViewHolder holder;
        HashMap<String, String> pers = new HashMap<String, String>();

        pers = data.get(position);

        if(convertView==null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            vi = inflater.inflate(R.layout.allfees, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.studentTV = (TextView) vi.findViewById(R.id.studentname);


            holder.image = (ImageView) vi.findViewById(R.id.profileimage);


            vi.setTag(holder);
        } else holder=(ViewHolder)vi.getTag();

        /************  Set holder with LayoutInflater ************/
        holder.studentTV.setText(pers.get("studentsname"));

        ImageView image = holder.image;

        url = pers.get("urls");      //Using LazyImageLoader for loading images using URLs cz in my case I needed to upload student picture, so passed the url data in the arraylist and received it here.
        System.out.println(url);

        imageLoader.DisplayImage(url, image);

        /******** Set Item Click Listner for LayoutInflater for each row ***********/
        //vi.setOnClickListener(new AdapterView.OnItemClickListener(position));
        return vi;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }

}