如何根据网格视图android中的索引排列图像?

时间:2014-12-22 10:57:50

标签: android

Mainactivity:

      public class MainActivity extends Activity {
    GridView grid;
    RelativeLayout r1;
    int maxrows, maxcol;
    ArrayList<LayoutData> data = new ArrayList<LayoutData>();
     ArrayList<HashMap<String, String>> sheetdetails = new ArrayList<HashMap<String, String>>();

              @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        grid=(GridView)findViewById(R.id.gridview);

        new GetRoute().execute("http://sb2.reloadit.in/TravelServices.asmx/Getlayout");

       }

     private     class GetRoute extends AsyncTask<String, Void, JSONObject> {
            /*String mJourneyDate;
            public GetData(String pJourneyDate) {
                this.mJourneyDate = pJourneyDate;
            }*/ProgressDialog pd = null;
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();

                    pd = ProgressDialog.show(MainActivity.this, "", "Loading...", true);
                }

                @Override
                protected JSONObject doInBackground(String... params) {
                    String response;

                    try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(params[0]);
                        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
                         nameValuePair.add(new BasicNameValuePair("RouteScheduleID","4798657"));

                         nameValuePair.add(new BasicNameValuePair("JourneyDate","2014-12-20"));


                         httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
                        HttpResponse responce = httpclient.execute(httppost);

                        HttpEntity httpEntity = responce.getEntity();

                        response = EntityUtils.toString(httpEntity);
                        Log.d("response is", response);

                        return new JSONObject(response);

                    } catch (Exception ex) {

                        ex.printStackTrace();

                    }

                    return null;
                }

                @Override
                protected void onPostExecute(JSONObject result) 
                {
                    super.onPostExecute(result);
                    //Log.v("TAG_RESULTadapter",""+result);

                    pd.dismiss();
                    if(result != null)
                    {
                        try
                        {


                            JSONObject jobj = result.getJSONObject("Response");
                            String message = jobj.getString("Message");
                            String issuceess = jobj.getString("IsSuccess");

                            if(issuceess.equals("true"))
                            {

                                JSONObject layout = result.getJSONObject("Layout");  
                                // Log.v("TAG_routearray",""+layoutarray);
                                  maxrows=layout.getInt("MaxRows");

                                  maxcol=layout.getInt("MaxColumns");
                                  grid.setNumColumns(maxcol);


                                    JSONArray routearray = layout.getJSONArray("SeatDetails");
                                    for (int i = 0; i < routearray.length(); i++) {

                                     String Row = routearray.getJSONObject(i).getString("Row");
                                 // Log.v("TAG_routearray",""+Row);
                                     String Col = routearray.getJSONObject(i).getString("Col");
                                     String Height = routearray.getJSONObject(i).getString("Height");
                                  String  Width = routearray.getJSONObject(i).getString("Width");
                                     String SeatNo = routearray.getJSONObject(i).getString("SeatNo");
                                  String Gender = routearray.getJSONObject(i).getString("Gender");

                                         String Deck = routearray.getJSONObject(i).getString("Deck");
                                         String IsAvailable = routearray.getJSONObject(i).getString("IsAvailable");
                                      String  Fare = routearray.getJSONObject(i).getString("Fare");



                                     Log.v("TAG_Maxfare",""+Fare);
                                     HashMap<String, String> map = new HashMap<String, String>();
                                     map.put("Row", Row);
                                     map.put("Col", Col);
                                     map.put("Height", Height);
                                     map.put("Width", Width);
                                     map.put("SeatNo", SeatNo);
                                     map.put("Deck", Deck);
                                     map.put("IsAvailable", IsAvailable);
                                     map.put("Gender", Gender);
                                     if(Height.equalsIgnoreCase("1")&& Width.equalsIgnoreCase("1")) {
                                     if(SeatNo.equalsIgnoreCase("")&& IsAvailable.equalsIgnoreCase("false")){
                                         map.put("seat_img", ""+R.drawable.noseat);
                                        }
                                     else{
                                          if(IsAvailable.equalsIgnoreCase("false")&&SeatNo!= ""){
                                              map.put("seat_img", ""+R.drawable.seatgreen);
                                          }
                                          if(IsAvailable.equalsIgnoreCase("false")&&SeatNo!= ""&& Gender.equalsIgnoreCase("F")){
                                              map.put("seat_img", ""+R.drawable.women);
                                          }
                                          if(SeatNo== ""&& Gender.equalsIgnoreCase("M")){
                                              map.put("seat_img", ""+R.drawable.noseat);
                                          }

                                         /* if(IsAvailable.equalsIgnoreCase("false")){
                                              map.put("seat_img", ""+R.drawable.seatgreen);
                                          }*/else if(IsAvailable.equalsIgnoreCase("true")){
                                              map.put("seat_img", ""+R.drawable.seat);
                                          }
                                        }
                                     }
                                     map.put("Fare", Fare);
                                     sheetdetails.add(map);

                                    }
                                    CustomGrid adapter = new CustomGrid(MainActivity.this, sheetdetails);
                                    grid.setAdapter(adapter);


                            }
                        }


                        catch (Exception e) 
                        {
                            e.printStackTrace();
                        }
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this, "Network Problem", Toast.LENGTH_LONG).show();
                    }
                }
}
}

适配器类:

public class CustomGrid extends BaseAdapter{
     private Context mContext;
    ArrayList<HashMap<String, String>> sheetdetails = new ArrayList<HashMap<String, String>>();
     public CustomGrid(Context c,ArrayList<HashMap<String, String>> sheetdetails) {
         mContext = c;
         this.sheetdetails= sheetdetails;
    }@Override
     public int getCount() {
         // TODO Auto-generated method stub
         return sheetdetails.size();
     }@Override
     public Object getItem(int position) {
         // TODO Auto-generated method stub
         return sheetdetails.get(position);
     }
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            grid = new View(mContext);
            grid = inflater.inflate(R.layout.row_grid, null);

            ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);

            HashMap<String, String> map = sheetdetails.get(position);
         String gg =map.get("Gender");
            Log.v("TAG_UUUUU",""+gg);
            imageView.setImageResource(Integer.valueOf(map.get("seat_img")));


        } else {
            grid = (View) convertView;
        }

        return grid;
    }
  }

Json:

    "Layout": {
        "ExtensionData": {},
        "MaxRows": 13,
        "MaxColumns": 5,
        "SeatDetails": [
            {
                "ExtensionData": {},
                "Row": 0,
                "Col": 1,
                "Height": 1,
                "Width": 1,
                "SeatNo": "48",
                "Gender": "",
                "IsAisle": false,
                "Deck": 1,
                "IsAC": true,
                "IsSleeper": false,
                "IsAvailable": true,
                "Fare": 1400,
                "ChildFare": 0,
                "InfantFare": 0
            },
            {
                "ExtensionData": {},
                "Row": 0,
                "Col": 2,
                "Height": 1,
                "Width": 1,
                "SeatNo": "",
                "Gender": "M",
                "IsAisle": true,
                "Deck": 1,
                "IsAC": true,
                "IsSleeper": false,
                "IsAvailable": false,
                "Fare": 0,
                "ChildFare": 0,
                "InfantFare": 0
            },

这是我的json。每次从json我得到max-col,max row。

从json创建一个动态地使用max-col和max-row的网格视图。

之后基于json的row和col索引,我需要在网格中放置一个图像。

例如,如果没有[0,0]索引,则保留该网格。如果[0,2]索引在那里将图像放入网格中。

从3天开始,我试图了解索引网格,但我没有得到任何东西。请帮帮我。

1 个答案:

答案 0 :(得分:0)

imageView.setImageResource(Integer.valueOf(map.get("seat_img")));

您应该从条件语句中执行此操作。即使转换视图不为空,它也会发生变化。