2016-05-03 5 views
1

Ich habe ein verschachteltes Array json, wo ich analysieren muss. Unten ist die jsonWie man Daten einstellt, die vom inneren Array des JSON geparst werden

{ 
    "result": { 

     "course": [{ 
        "course_id": "3", 
        "parent_course_id": "0", 
        "course_name": "Management of Portfolio (MoP)", 
        "course_price": "51000", 
        "course_price_doller": "510", 
        "course_price_discunt": "40000", 
        "course_price_discunt_doller": "400", 
        "course_discunt_date": "2016-08-09", 
        "course_description": "We are an expert training organization with our faculty having vast experience in consulting and training accredited by Peoplecert on behalf of Axelos.\r\nThis program is for 3 full days.\r\n\r\nXellentro has one of the best Project Portfolio Management Professionals with large experience of project management and consulting in the area across the globe for large organizations and governments.", 
        "course_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL5715320A694D7_COURSE_128x64.jpg", 
        "total_module": 3, 
        "module": [{ 
         "module_id": "14", 
         "video_price": "12", 
         "video_price_doller": "11", 
         "video_price_discunt": "123", 
         "video_price_discunt_doller": "12", 
         "video_discunt_date": "2016-02-03", 
         "video_type": "1", 
         "video_link": "arrisofttech.com\/2016\/xellentroapp\/video\/sample_2.mp4", 
         "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png", 
         "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
        }, { 
         "module_id": "12", 
         "video_price": "12", 
         "video_price_doller": "11", 
         "video_price_discunt": "123", 
         "video_price_discunt_doller": "12", 
         "video_discunt_date": "2016-02-03", 
         "video_type": "0", 
         "video_link": "https:\/\/www.youtube.com\/watch?v=QSaWoca3SjY&list=RDQSaWoca3SjY", 
         "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png", 
         "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
        }, { 
         "module_id": "13", 
         "video_price": "12", 
         "video_price_doller": "11", 
         "video_price_discunt": "123", 
         "video_price_discunt_doller": "12", 
         "video_discunt_date": "2016-02-03", 
         "video_type": "0", 
         "video_link": "https:\/\/www.youtube.com\/watch?v=QSaWoca3SjY&list=RDQSaWoca3SjY", 
         "video_image": "http:\/\/arrisofttech.com\/2016\/xellentroapp\/assets\/uploads\/course\/SL571531558C660_COURSE_228x80.png", 
         "video_description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
        }] 
}] 
      } 
} 

Es gibt zwei JsonArray: - genauer gesagt Array innerhalb eines Arrays nämlich KURS und MODUL Ich habe bereits die json erkannt, aber der Punkt ist es eine Listenansicht, wo werden die Kurse gezeigt werden. Beim Klick auf den Gegenstand öffnet sich eine Aktivität, die die gesamten Module in der Listenansicht zeigt. Ich habe den Kurs schon aufgelistet, aber auf dem Item oder dem Button Click kann ich die Module in der Listenansicht nicht anzeigen. Unten füge ich den Code für das gleiche an.

TrainningFragment.java

public class TrainingFragment extends Fragment { 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    private ImageView tag; 
    private static final String url = "http://arrisofttech.com/2016/xellentroapp/webservices/tranning/"; 
    private ProgressDialog pDialog; 
    private List<Movie> trainingList = new ArrayList<Movie>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 
    public static final String KEY_URL = "video_url"; 
    public TrainingFragment() { 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_news, container, false); 
     listView = (ListView) rootView.findViewById(R.id.list); 
     adapter = new CustomListAdapter(getActivity(), trainingList); 
     listView.setAdapter(adapter); 
     pDialog = new ProgressDialog(getActivity()); 
     pDialog.setMessage("Loading...Please Wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
     Volley.newRequestQueue(getActivity()).add(new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       hidePDialog(); 
       try { 
        JSONObject result = response.getJSONObject("result"); 
        JSONArray jsonArray = result.getJSONArray("course"); 
        System.out.println("Course: == >" + result.getJSONArray("course")); 
        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject trainingObj = jsonArray.getJSONObject(i); 
         Movie movie = new Movie(); 
         movie.setCourse_id(trainingObj.getString("course_id")); 
         movie.setParent_course_id(trainingObj.getString("parent_course_id")); 
         movie.setCourse_name(trainingObj.getString("course_name")); 
         movie.setCourse_price(trainingObj.getString("course_price")); 
         movie.setCourse_price_dollar(trainingObj.getString("course_price_doller")); 
         movie.setCourse_price_discount_dollar(trainingObj.getString("course_price_discunt_doller")); 
         movie.setCourse_discount_date(trainingObj.getString("course_discunt_date")); 
         movie.setCourse_description(trainingObj.getString("course_description")); 
         movie.setCourse_image(trainingObj.getString("course_image")); 
         JSONArray module = trainingObj.getJSONArray("module"); 
         for (int j = 0; j < module.length(); j++) { 
          JSONObject moduleObj = module.getJSONObject(j); 
          movie.setModule_id(moduleObj.getString("module_id")); 
          movie.setVideo_price(moduleObj.getString("video_price")); 
          movie.setVideo_price_doller(moduleObj.getString("video_price_doller")); 
          movie.setVideo_price_discunt(moduleObj.getString("video_price_discunt")); 
          movie.setVideo_price_discunt_doller(moduleObj.getString("video_price_discunt_doller")); 
          movie.setVideo_discunt_date(moduleObj.getString("video_discunt_date")); 
          movie.setVideo_type(moduleObj.getString("video_type")); 
          movie.setVideo_link(moduleObj.getString("video_link")); 
          movie.setVideo_image(moduleObj.getString("video_image")); 
          movie.setVideo_description(moduleObj.getString("video_description")); 
         } 
         trainingList.add(movie); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       new AlertDialog.Builder(getActivity()).setTitle("No Connectivity ").setMessage("Please check your internet connectivity!").setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
          } 
         }).setIcon(android.R.drawable.ic_dialog_alert).show(); 
       hidePDialog(); 
      } 
     })); 
     return rootView; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 
} 

MyCustomListAdapter

add_to_cart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       add_to_cart_list.add(m.getCourse_id().toString()); 
       add_to_cart_list.size(); 
       pDialog.setMessage("Added to the cart ..."); 
       showDialog(); 
       StringRequest strReq = new StringRequest(Request.Method.POST, 
         URL_ADD_TO_CART, new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         Log.d("Added to Cart", "Adding to the cart: " + response.toString()); 
         hideDialog(); 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Log.e("ERROR", "Adding to the cart: " + error.getMessage()); 
         Toast.makeText(activity, 
           error.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       }) { 
        @Override 
        protected Map<String, String> getParams() { 
         Map<String, String> params = new HashMap<String, String>(); 
         params.put("app_id", "12365478965874"); 
         params.put("product_type", "1"); 
         params.put("parent_course_id", m.getParent_course_id()); 
         params.put("course_id", m.getCourse_id()); 
         params.put("vaideo_id", "0"); 
         params.put("product_image", m.getCourse_image()); 
         params.put("product_name", m.getCourse_name()); 
         params.put("product_price", m.getCourse_price_dollar()); 
         params.put("product_discunt_price", m.getCourse_price_discount_dollar()); 
         params.put("product_quantity", "1"); 
         return params; 
        } 
       }; 
       AppController.getInstance().addToRequestQueue(strReq, "ADD TO CART"); 
      } 
      private void showDialog() { 
       if (!pDialog.isShowing()) 
        pDialog.show(); 
      } 

      private void hideDialog() { 
       if (pDialog.isShowing()) 
        pDialog.dismiss(); 
      } 
     }); 

     view_details.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
//    Toast.makeText(activity, "This functionality is under Development Phase", Toast.LENGTH_SHORT).show(); 

//    activity.startActivity(new Intent(activity, ViewDetails.class)); 


       Intent intent = new Intent(activity, ViewDetails.class); 
       intent.putExtra("course_image", m.getCourse_image()); 
       intent.putExtra("course_name", m.getCourse_name()); 
       intent.putExtra("course_price", m.getCourse_price_discount_dollar()); 
       intent.putExtra("course_price_discount", m.getCourse_price_dollar()); 
       intent.putExtra("course_description", m.getCourse_description()); 
       activity.startActivity(intent); 
      } 
     }); 

     btn_view_module.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent view_module = new Intent(activity, ViewModule.class); 

       Toast.makeText(activity, m.getCourse_id() + " " + m.getModule_id(), Toast.LENGTH_SHORT).show(); 


       activity.startActivity(view_module); 
      } 
     }); 

Hier gibt es 3 Tasten: -

AddToCart, viewmodule und Blick natürlich. Wenn Sie auf die Schaltfläche ViewModule klicken, wird eine Aktivität mit einer benutzerdefinierten Liste geöffnet, die die Module des Kurses angibt. In der obigen json die Gesamtzahl von Array-Elementen sind 3 SO 3 Listenansicht Elemente mit den oben angegebenen Komponenten gezeigt werden wird

ViewDetails.java

public class ViewDetails extends AppCompatActivity { 

    private boolean enableBackNavigation; 
    private ProgressDialog pDialog; 
    TextView tv_course_name, tv_course_actual_price, tv_course_discounted_price, tv_course_description; 
    private String course_image = "", course_name = "", course_actual_price = "", course_discounted_price = "", course_description = ""; 
    private NetworkImageView thumbNail; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view_details); 
     Intent intent = this.getIntent(); 
     if (intent != null) { 
      course_image = intent.getStringExtra("course_image"); 
      course_name = intent.getStringExtra("course_name"); 
      course_actual_price = intent.getStringExtra("course_price"); 
      course_discounted_price = intent.getStringExtra("course_price_discount"); 
      course_description = intent.getStringExtra("course_description"); 
     } 
     init(); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       onBackPressed(); 
      } 
     }); 
     if (enableBackNavigation) { 
      toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        onBackPressed(); 
       } 
      }); 
     } 
     if (imageLoader == null)imageLoader = AppController.getInstance().getImageLoader(); 
     thumbNail.setImageUrl(course_image, imageLoader); 
     tv_course_name.setText(course_name); 
     tv_course_actual_price.setText(course_actual_price); 
     tv_course_actual_price.setPaintFlags(tv_course_actual_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
     tv_course_discounted_price.setText(course_discounted_price); 
     tv_course_description.setText(course_description); 

    } 

    private void init() { 
     tv_course_name = (TextView) findViewById(R.id.course_name); 
     tv_course_description = (TextView) findViewById(R.id.course_description); 
     tv_course_actual_price = (TextView) findViewById(R.id.actual_cost); 
     tv_course_discounted_price = (TextView) findViewById(R.id.discounted_cost); 
     thumbNail = (NetworkImageView) findViewById(R.id.thumbnail); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_view_details, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

Movie.class

public class Movie { 
    private String course_id; 
    private String parent_course_id; 
    private String course_name; 
    private String course_price; 
    private String course_price_dollar; 
    private String course_price_discount_dollar; 
    private String course_discount_date; 
    private String course_description; 
    private String course_image; 
    private String total_module; 
    // Module 
    private String module_id; 
    private String video_price; 
    private String video_price_doller; 
    private String video_price_discunt; 
    private String video_price_discunt_doller; 
    private String video_discunt_date; 
    private String video_type; 
    private String video_link; 
    private String video_image; 
    private String video_description; 


    public Movie() { 
    } 

    public Movie(String course_id, String parent_course_id, String course_name, String course_price, 
       String course_price_dollar, String course_price_discount_dollar, String course_discount_date, 
       String course_description, String course_image, String total_module, String module_id, 
       String video_price, String video_price_doller, String video_price_discunt, 
       String video_price_discunt_doller, String video_discunt_date, String video_type, 
       String video_link, String video_image, String video_description) { 
     this.course_id = course_id; 
     this.parent_course_id = parent_course_id; 
     this.course_name = course_name; 
     this.course_price = course_price; 
     this.course_price_dollar = course_price_dollar; 
     this.course_price_discount_dollar = course_price_discount_dollar; 
     this.course_discount_date = course_discount_date; 
     this.course_description = course_description; 
     this.course_image = course_image; 
     this.total_module = total_module; 
     this.module_id = module_id; 
     this.video_price = video_price; 
     this.video_price_doller = video_price_doller; 
     this.video_price_discunt = video_price_discunt; 
     this.video_price_discunt_doller = video_price_discunt_doller; 
     this.video_discunt_date = video_discunt_date; 
     this.video_type = video_type; 
     this.video_link = video_link; 
     this.video_image = video_image; 
     this.video_description = video_description; 
    } 

    public String getCourse_id() { 
     return course_id; 
    } 

    public void setCourse_id(String course_id) { 
     this.course_id = course_id; 
    } 

    public String getParent_course_id() { 
     return parent_course_id; 
    } 

    public void setParent_course_id(String parent_course_id) { 
     this.parent_course_id = parent_course_id; 
    } 

    public String getCourse_name() { 
     return course_name; 
    } 

    public void setCourse_name(String course_name) { 
     this.course_name = course_name; 
    } 

    public String getCourse_price() { 
     return course_price; 
    } 

    public void setCourse_price(String course_price) { 
     this.course_price = course_price; 
    } 

    public String getCourse_price_dollar() { 
     return course_price_dollar; 
    } 

    public void setCourse_price_dollar(String course_price_dollar) { 
     this.course_price_dollar = course_price_dollar; 
    } 

    public String getCourse_price_discount_dollar() { 
     return course_price_discount_dollar; 
    } 

    public void setCourse_price_discount_dollar(String course_price_discount_dollar) { 
     this.course_price_discount_dollar = course_price_discount_dollar; 
    } 

    public String getCourse_discount_date() { 
     return course_discount_date; 
    } 

    public void setCourse_discount_date(String course_discount_date) { 
     this.course_discount_date = course_discount_date; 
    } 

    public String getCourse_description() { 
     return course_description; 
    } 

    public void setCourse_description(String course_description) { 
     this.course_description = course_description; 
    } 

    public String getCourse_image() { 
     return course_image; 
    } 

    public void setCourse_image(String course_image) { 
     this.course_image = course_image; 
    } 

    public String getTotal_module() { 
     return total_module; 
    } 

    public void setTotal_module(String total_module) { 
     this.total_module = total_module; 
    } 

    public String getModule_id() { 
     return module_id; 
    } 

    public void setModule_id(String module_id) { 
     this.module_id = module_id; 
    } 

    public String getVideo_price() { 
     return video_price; 
    } 

    public void setVideo_price(String video_price) { 
     this.video_price = video_price; 
    } 

    public String getVideo_price_doller() { 
     return video_price_doller; 
    } 

    public void setVideo_price_doller(String video_price_doller) { 
     this.video_price_doller = video_price_doller; 
    } 

    public String getVideo_price_discunt() { 
     return video_price_discunt; 
    } 

    public void setVideo_price_discunt(String video_price_discunt) { 
     this.video_price_discunt = video_price_discunt; 
    } 

    public String getVideo_price_discunt_doller() { 
     return video_price_discunt_doller; 
    } 

    public void setVideo_price_discunt_doller(String video_price_discunt_doller) { 
     this.video_price_discunt_doller = video_price_discunt_doller; 
    } 

    public String getVideo_discunt_date() { 
     return video_discunt_date; 
    } 

    public void setVideo_discunt_date(String video_discunt_date) { 
     this.video_discunt_date = video_discunt_date; 
    } 

    public String getVideo_type() { 
     return video_type; 
    } 

    public void setVideo_type(String video_type) { 
     this.video_type = video_type; 
    } 

    public String getVideo_link() { 
     return video_link; 
    } 

    public void setVideo_link(String video_link) { 
     this.video_link = video_link; 
    } 

    public String getVideo_image() { 
     return video_image; 
    } 

    public void setVideo_image(String video_image) { 
     this.video_image = video_image; 
    } 

    public String getVideo_description() { 
     return video_description; 
    } 

    public void setVideo_description(String video_description) { 
     this.video_description = video_description; 
    } 
} 

Bitte helfen. Ich bin hier steckengeblieben und kann nicht verstehen, was ich tun soll.

+0

Bitte senden Sie den Code bekommen im Zusammenhang mit ** I bereits den Kurs aufgeführt haben, aber auf den Punkt oder den Button klicken Ich kann die Module in der Listview nicht zeigen. ** –

+0

Ich habe den Code für das Trainning Fragment und auch den Code für das Click-Ereignis auf dem Adapter –

+0

Wie zeigen Sie die Details in "ViewDetails" Aktivität? Posten Sie diesen Code auch. –

Antwort

1

In Film-Klasse hinzufügen eine Arraylist

private String course_id; 
    private String parent_course_id; 
    private String course_name; 
    private String course_price; 
    private String course_price_dollar; 
    private String course_price_discount_dollar; 
    private String course_discount_date; 
    private String course_description; 
    private String course_image; 
    private String total_module; 
    // Module 
    private String module_id; 
    private String video_price; 
    private String video_price_doller; 
    private String video_price_discunt; 
    private String video_price_discunt_doller; 
    private String video_discunt_date; 
    private String video_type; 
    private String video_link; 
    private String video_image; 
    private String video_description; 
    private List<Module> modulelist; 

Erstellen einer Modulklasse mit den Feldern

private String module_id; 
    private String video_price; 
    private String video_price_doller; 
    private String video_price_discunt; 
    private String video_price_discunt_doller; 
    private String video_discunt_date; 
    private String video_type; 
    private String video_link; 
    private String video_image; 
    private String video_description; 

dann in trainig den Wert holen

JSONArray module = trainingObj.getJSONArray("module"); 
         for (int j = 0; j < module.length(); j++) { 
          JSONObject moduleObj = module.getJSONObject(j); 
          movie.setModule_id(moduleObj.getString("module_id")); 
          movie.setVideo_price(moduleObj.getString("video_price")); 
          movie.setVideo_price_doller(moduleObj.getString("video_price_doller")); 
          movie.setVideo_price_discunt(moduleObj.getString("video_price_discunt")); 
          movie.setVideo_price_discunt_doller(moduleObj.getString("video_price_discunt_doller")); 
          movie.setVideo_discunt_date(moduleObj.getString("video_discunt_date")); 
          movie.setVideo_type(moduleObj.getString("video_type")); 
          movie.setVideo_link(moduleObj.getString("video_link")); 
          movie.setVideo_image(moduleObj.getString("video_image")); 
          movie.setVideo_description(moduleObj.getString("video_description")); 
         } 

Nun ist diese

List<Module> mod1=new ArrayList<Module>(); 

    JSONArray module = trainingObj.getJSONArray("module"); 
    for (int j = 0; j < module.length(); j++) { 
     JSONObject moduleObj = module.getJSONObject(j); 
     Module modl=new Module(); 
     modl.setModule_id(moduleObj.getString("module_id")); 
     modl.setVideo_price(moduleObj.getString("video_price")); 
     modl.setVideo_price_doller(moduleObj.getString("video_price_doller")); 
     modl.setVideo_price_discunt(moduleObj.getString("video_price_discunt")); 
     modl.setVideo_price_discunt_doller(moduleObj.getString("video_price_discunt_doller")); 
     modl.setVideo_discunt_date(moduleObj.getString("video_discunt_date")); 
     modl.setVideo_type(moduleObj.getString("video_type")); 
     modl.setVideo_link(moduleObj.getString("video_link")); 
     modl.setVideo_image(moduleObj.getString("video_image")); 
     modl.setVideo_description(moduleObj.getString("video_description")); 
     mod1.add(j,modl); 

    } 
    movie.setModulelist(mod1); 

In movie.setModulelist ändern Sie Ihre Liste

Verwandte Themen