I'm trying to change the background color of dates using a JSON response I get. But I'm having some difficulty.
Here is my code:
<com.prolificinteractive.materialcalendarview.MaterialCalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp" />
MainActivty.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calbg();
materialCalendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
Calendar calendar = Calendar.getInstance();
materialCalendarView.setSelectedDate(calendar.getTime());
materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
get1 = sharedpreferences.getString(CLIENT, "");
materialCalendarView.setDateTextAppearance(getTitleColor());
materialCalendarView.setHeaderTextAppearance(R.style.AppTheme_Dark1);
}
private void calbg() {
// Volley's json array request object
StringRequest stringRequest = new StringRequest(Request.Method.POST, CALENDAR_DATA,
new Response.Listener < String > () {
@Override
public void onResponse(String response) {
JSONObject object = null;
try {
object = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonarray = null;
try {
jsonarray = object.getJSONArray("Table");
} catch (JSONException e) {
e.printStackTrace();
}
// SimpleDateFormatter formatter = new SimpleDateFormatter(); //TODO: update this line with the correct formatter
List<Event> events = new ArrayList<>();
for (int i = 0; i < jsonarray.length(); i++) {
try {
JSONObject obj = jsonarray.getJSONObject(i);
String str = obj.getString("eventdate").replaceAll("\D+","");
String upToNCharacters = str.substring(0, Math.min(str.length(), 13));
DateFormat timeZoneFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
timeZoneFormat.setTimeZone(TimeZone.getTimeZone("GMT-8"));
Date time = new java.util.Date(Long.parseLong(upToNCharacters));
// System.out.println(time);
// movie.setDate(String.valueOf(timeZoneFormat.format(time)));
// String str2 = String.valueOf(timeZoneFormat.format(time));
String str1 = obj.optString("eventcolor");
// Date date = formatter.parse(str2);
int color = Integer.parseInt(str1); //TODO: update this line with the correct code to parse your color
Event event = new Event(time, color);
events.add(event);
}
catch (JSONException e) {
e.printStackTrace();
}
}
for (Event event : events) {
//Here is the problem in parameter
EventDecorator eventDecorator = new EventDecorator(event.getDate(), event.getColor());
materialCalendarView.addDecorator(eventDecorator);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// VolleyLog.d(TAG, "Error: " + error.getMessage());
// hidePDialog();
}
}) {
@Override
protected Map < String, String > getParams() {
Map < String, String > params = new HashMap < String, String > ();
params.put(CLIENT, get1);
return params;
}
};
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(stringRequest);
}}
JSON response
{
"Table":[
{
"userid":4,
"eventname":"adi",
"eventdate":"/Date(1484121600000-0800)/",
"eventcolor":"2413AD",
"autoid":2005
},
{
"userid":4,
"eventname":"Mandeep",
"eventdate":"/Date(1480924800000-0800)/",
"eventcolor":"3A87AD",
"autoid":2002
},
{
"userid":4,
"eventname":"nefv",
"eventdate":"/Date(1477465200000-0700)/",
"eventcolor":"39AD37",
"autoid":2
},
]
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…