0

I have an ArrayList of ‘wholeYearRiskArray’, at each index of ArrayList there is another ArrayList of JSONObjects that contains the per month news/risks. Each index represents one month of news/risks. I want to show the displaydate and subject properties of each JSONObject in the ListView. Because each month data is separated by other month data by index.

I want to generate Fragments in ViewPager without any tabs. Each Fragment will hold each data of a month. The Fragments will dynamically generate according to the size of wholeYearRisksArray.

Here is the picture in the debug mode.

http://i.imgur.com/3RvqDMn.png

Here is the picture that i want to implement.

https://i.stack.imgur.com/ZcmV8.png

Here is my MainActivity.java code.

public class MainActivity extends AppCompatActivity {

    public static String TAG = MainActivity.class.getSimpleName();
    ArrayList<Risks> risksArrayList = new ArrayList<>();
    HashMap<String, String> firstObjectHashMap = new HashMap<>();
    HashMap<String, String> lastObjectHashMap = new HashMap<>();
    String latestDateStr;
    String oldestDateStr;
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    DateTime formatedLatestDate;
    DateTime formatedOldestDate;
    DateTime upDate;

    JSONArray filteredJSONArray = new JSONArray();
    ArrayList<ArrayList<JSONObject>> wholeYearRiskArray = new ArrayList<>();
    Risks risks = new Risks();
    private String JsonUrl = "https://api.myjson.com/bins/4goeq";  // https://api.myjson.com/bins/4goeq
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest req = new StringRequest(JsonUrl,

                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("data");

                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject jsonObjectFromArray = jsonArray.getJSONObject(i);

                                if (jsonObjectFromArray.getString("publishtype").equals("PUBLISHED")) {

                                    filteredJSONArray.put(jsonObjectFromArray);

                                    risks.setUID(jsonObjectFromArray.getString("UID"));
                                    risks.setSubject(jsonObjectFromArray.getString("subject"));
                                    risks.setArticletext(jsonObjectFromArray.getString("articletext"));
                                    risks.setDisplaydate(jsonObjectFromArray.getString("displaydate"));
                                    risks.setPublishdate(jsonObjectFromArray.getString("publishdate"));
                                    risks.setKeywords(jsonObjectFromArray.getString("keywords"));
                                    risks.setPublishdate(jsonObjectFromArray.getString("publishtype"));
                                    risks.setTimestamp(jsonObjectFromArray.getString("timestamp"));

                                    risksArrayList.add(risks);
                                }
                            }
                            risksArrayList.get(0);

                            JSONObject firstObjectFromJsonArray = filteredJSONArray.getJSONObject(0);
                            JSONObject lastObjectFromJsonArray = filteredJSONArray.getJSONObject(filteredJSONArray.length() - 1);

                            firstObjectHashMap.put("UID", firstObjectFromJsonArray.getString("UID"));
                            firstObjectHashMap.put("subject", firstObjectFromJsonArray.getString("subject"));
                            firstObjectHashMap.put("articletext", firstObjectFromJsonArray.getString("articletext"));
                            firstObjectHashMap.put("displaydate", firstObjectFromJsonArray.getString("displaydate"));
                            firstObjectHashMap.put("publishdate", firstObjectFromJsonArray.getString("publishdate"));
                            firstObjectHashMap.put("keywords", firstObjectFromJsonArray.getString("keywords"));
                            firstObjectHashMap.put("publishtype", firstObjectFromJsonArray.getString("publishtype"));
                            firstObjectHashMap.put("timestamp", firstObjectFromJsonArray.getString("timestamp"));

                            lastObjectHashMap.put("UID", lastObjectFromJsonArray.getString("UID"));
                            lastObjectHashMap.put("subject", lastObjectFromJsonArray.getString("subject"));
                            lastObjectHashMap.put("articletext", lastObjectFromJsonArray.getString("articletext"));
                            lastObjectHashMap.put("displaydate", lastObjectFromJsonArray.getString("displaydate"));
                            lastObjectHashMap.put("publishdate", lastObjectFromJsonArray.getString("publishdate"));
                            lastObjectHashMap.put("keywords", lastObjectFromJsonArray.getString("keywords"));
                            lastObjectHashMap.put("publishtype", lastObjectFromJsonArray.getString("publishtype"));
                            lastObjectHashMap.put("timestamp", lastObjectFromJsonArray.getString("timestamp"));

                            oldestDateStr = firstObjectHashMap.get("publishdate");
                            latestDateStr = lastObjectHashMap.get("publishdate");

                            oldestDateStr = oldestDateStr.substring(0, 10);
                            oldestDateStr = oldestDateStr + " 00:00:00";

                            formatedOldestDate = fmt.parseDateTime(oldestDateStr);
                            formatedLatestDate = fmt.parseDateTime(latestDateStr);

                            int daysInMonth;

                            ArrayList<JSONObject> perMonthRisksArray = new ArrayList<JSONObject>();

                            while (formatedOldestDate.isBefore(formatedLatestDate) || formatedOldestDate.isEqual(formatedLatestDate)) {

                                daysInMonth = formatedOldestDate.dayOfMonth().getMaximumValue();
                                upDate = formatedOldestDate.plusDays(daysInMonth - 1);

                                long formatedLatestDateMillis = formatedOldestDate.getMillis();
                                long upDateMillis = upDate.getMillis();

                                perMonthRisksArray = fetchObjectsByDate(filteredJSONArray, formatedLatestDateMillis, upDateMillis);

                                wholeYearRiskArray.add(perMonthRisksArray);
                                formatedOldestDate = upDate.plusDays(1);

                            }
                            wholeYearRiskArray.get(0);
                            Log.d(TAG, Integer.toString(wholeYearRiskArray.size()));
                            risksArrayList.get(0);

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        }
        );
        queue.add(req);
    }

    public ArrayList<JSONObject> fetchObjectsByDate(JSONArray array, long startDate, long endDate) {
        final ArrayList<JSONObject> list = new ArrayList<>();

        for (int i = 0; i < array.length(); i++) {
            final JSONObject object = array.optJSONObject(i);
            if (object != null) {
                final String dateStr = object.optString("publishdate");
                if (dateStr != null) {

                    final long date = fmt.parseDateTime(dateStr).getMillis();
                    if ((date >= startDate) && (date <= endDate)) {
                        list.add(object);
                    }
                }
            }
        }
        return list;
    }
}

Here is my Risks.Java code.

public class Risks {

    String UID;
    String subject;
    String articletext;
    String displaydate;
    String publishdate;
    String keywords;
    String publishtype;
    String timestamp;

    public String getUID() {
        return UID;
    }

    public void setUID(String UID) {
        this.UID = UID;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getDisplaydate() {
        return displaydate;
    }

    public void setDisplaydate(String displaydate) {
        this.displaydate = displaydate;
    }

    public String getArticletext() {
        return articletext;
    }

    public void setArticletext(String articletext) {
        this.articletext = articletext;
    }

    public String getPublishdate() {
        return publishdate;
    }

    public void setPublishdate(String publishdate) {
        this.publishdate = publishdate;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public String getPublishtype() {
        return publishtype;
    }

    public void setPublishtype(String publishtype) {
        this.publishtype = publishtype;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }
}

Should i is use a ListView or TableLayout or LayoutInflator?

How can i generate dynamically fragments without any tabs and show the data according to the above scenario?

3 Answers3

1

Ok first you need to add all required that into a List. then you have to make a custom List and make a seperate layout for your list. then put all the data from that list to custom list. This is the solution. Check this https://stackoverflow.com/a/8166802/5275639 it will help you.

Community
  • 1
  • 1
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
0

Try the Gson library. Key-value mappings from JSON to custom user models can be easily done, Hope it helps!

0

use http://www.jsonschema2pojo.org/ to create your POJO classes for your JSON response. Then use the Gson library to map THE response to POJO class. It'll be much easier to show it in ViewPager after this. Give a try

Create the fragments dynamically according to the size of the array. No need to have separate fragments. Create a sample viewpager fragment example from Android Studio then you'll come to know how to have a dynamic fragments on time. By default the default view pager implementation examples which you can find anywhere on internet would come without tab.

Akbarsha
  • 2,422
  • 23
  • 34