Skip to content

Instantly share code, notes, and snippets.

@Vagonn
Created July 13, 2018 05:31
Show Gist options
  • Save Vagonn/3c0cedad4cd0d4bb1b5df4b73a9aa43d to your computer and use it in GitHub Desktop.
Save Vagonn/3c0cedad4cd0d4bb1b5df4b73a9aa43d to your computer and use it in GitHub Desktop.
Setting recyclerview adapter
private void parseJSON(String url)
{
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
//System.out.println("RESPONSE: " + Res);
try
{
progressBar.setVisibility(View.GONE);
JSONArray jsonArray = response.getJSONArray("News");
for (int i = 0; i < jsonArray.length() - 1; i++)
{
JSONObject newsItem = jsonArray.getJSONObject(i);
JSONObject data = newsItem.getJSONObject("data");
imageUrl = "https://metbuat.az" + data.getString("image_t");
titleText = data.getString("title");
subTitleText = data.getString("sub_title");
watched = data.getString("views_stats");
body = data.getString("details_full");
lastNewsUNIX = String.valueOf(data.getLong("add_date"));
/*if (i == jsonArray.length() - 2)
{
System.out.println("unix of lastnews: " + lastNewsUNIX);
}*/
String sendUs = data.getString("sendus_text");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();
editor.putString("sendUs", sendUs);
editor.apply();
// region Time
addDateTimestamp = data.getLong("add_date") * 1000;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(addDateTimestamp);
int year = cal.get(Calendar.YEAR);
int monthNumber = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
if (minute < 10) minuteText = "0" + minute;
else minuteText = Integer.toString(minute);
if (hour < 10)
{
// hour = 0 olanda -1 eliyir
if (hour == -1) hourText = "23";
else hourText = "0" + Integer.toString(hour);
}
else hourText = Integer.toString(hour);
if (day < 10) dayText = "0" + day;
else dayText = Integer.toString(day);
titleText = titleText.replace("&quot;", "\"");
subTitleText = subTitleText.replace("&quot;", "\"");
if (subTitleText != null && !subTitleText.isEmpty())
{
if (!(titleText.substring(titleText.length() - 1)).equals(" ") && !(subTitleText.substring(0, 1)).equals(" "))
{
titleText = titleText + " ";
}
}
shareText = titleText + subTitleText;
shareUrl = data.getString("news_url");
time = hourText + ":" + minuteText;
date = dayText + " " + months[monthNumber] + " " + year;
//endregion
if (!data.getString("gallery").equals("false"))
{
JSONArray galleryJsonArray = data.getJSONArray("gallery");
jsonGallery = galleryJsonArray.toString();
}
else
{
jsonGallery = "";
}
// her xeberin melumatlarini bir-bir liste elave eliyir
feedNewsItemArrayList.add(new FeedNewsItem(imageUrl, titleText, subTitleText, time, date,
watched, body, addDateTimestamp, currentDate, jsonGallery, shareText, shareUrl));
}
feedAdapter = new FeedAdapter(MainActivity.this, feedNewsItemArrayList);
recyclerView.setAdapter(feedAdapter);
feedAdapter.setOnItemClickListener(MainActivity.this);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "İnternet bağlantısı yoxdur", Toast.LENGTH_LONG).show();
}
}
);
requestQueue.add(jsonObjectRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment