春衣少年当酒歌,起舞四顾以笑和。这篇文章主要讲述从数据库获取事件并放置日历视图(android)相关的知识,希望能为你提供帮助。
我的android应用程序中有一个日历程序。现在我需要将事件存储在在线mysql
数据库中并将它们放在日历上。现在我有一个php
文件,创建一个json
对象。这个JSON
对象显示了来自数据库的所有事件
Calendar.php
<
?php
include 'DatabaseConfig.php';
// Create connection
//$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
$conn = mysqli_connect("localhost", "id2553265_admin", "admin", "id2553265_mobile_app");
if ($conn->
connect_error) { die("Connection failed: " . $conn->
connect_error);
} $sql = "SELECT * FROM calendar";
$result = $conn->
query($sql);
if ($result->
num_rows >
0) { while($row[] = $result->
fetch_assoc()) { $tem = $row;
$json = json_encode($tem);
}} else {
echo "No Results Found.";
}
echo $json;
$conn->
close();
?>
在此代码中,我插入1496134800000L以突出显示2017-05-30的日期。现在我有了一些突出显示的java代码,并用特定的日期将它们涂成蓝色。
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_calendar, container, false);
//calendar setup
compactCalendar = (CompactCalendarView) view.findViewById(R.id.comCalendarView);
compactCalendar.setUseThreeLetterAbbreviation(true);
calendarMonth = (TextView) view.findViewById(R.id.calMonth);
Event may30 = new Event(Color.BLUE, **1496134800000L**, "test");
compactCalendar.addEvent(may30);
Event aug17 = new Event(Color.BLUE, 1502960400000L, "test");
compactCalendar.addEvent(aug17);
compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
@Override
public void onDayClick(Date dateClicked){
if (dateFormatDay.format(dateClicked).toString().compareTo("**2017-05-30**") == 0){
Toast.makeText(CalendarFragment.this.getContext(), "There is an event", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getActivity(), ListMay07.class);
i.putExtra("Date", "Sample date");
startActivity(i);
}elseif (dateFormatDay.format(dateClicked).toString().compareTo("2017-08-17") == 0){
Toast.makeText(CalendarFragment.this.getContext(), "There is an event", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getActivity(), ListAug17.class);
i.putExtra("Date", "Sample date");
startActivity(i);
}else{
Toast.makeText(CalendarFragment.this.getContext(), "No Event", Toast.LENGTH_SHORT).show();
}
}@Override
public void onMonthScroll(Date firstDayOfNewMonth) {
calendarMonth.setText(dateFormatMonth.format(firstDayOfNewMonth));
}});
// Inflate the layout for this fragment
return view;
}
我的主要问题是如何从数据库中获取数据并将其插入到我的java代码中,以便所有事件都将输入数据库而不是实际代码中。
文章图片
我想通过使用
JSON
对象来做到这一点,但我不知道如何。[{
"ID":"1",
"Epoch_Time":"1496134800000L",
"Date":"2017-05-30"??,
"Description":"Cama??raderie Day"
},
{
"ID":"2",
"Epoch_Time":"1502960400000L",
"Date":"2017-0??8-17",
"Description":??"Sample Date"
}]
答案【从数据库获取事件并放置日历视图(android)】首先为JSON中包含的数据创建POJO。
public class Event{
long id;
long epochTime;
String date;
Stringdesc;
public Event (long id, long epochTime, String date, Stringdesc){
this.id = id;
this.epochTime = epochTime;
this.date = date;
this.desc = desc;
}
}
然后,您可以解析JSON并将其存储在POJO中
String myJsonResponse // will contain the json response from the servertry {
JSONArray array = new JSONArray(myJsonResponse);
List<
Event>
events = new ArrayList<
>
(array.length());
for (int i = 0;
i <
array.length();
i++) {
JSONObject object = (JSONObject) array.get(1);
long id = Long.parseLong(object.getString("ID"));
long epoctTime = Long.parseLong(object.getString("Epoch_Time"));
String date = object.getString("Date");
String description = object.getString("Description");
Event event = new Event(id, epoctTime, date, description);
events.add(event);
}
} catch (JSONException e){
e.printStackTrace();
}
然后,您可以使用事件列表执行任何操作。
推荐阅读
- 如何在Android设备中获取设备日历事件列表()
- Android java以24小时格式获取当前时间
- 在Android中,每天在特定时间设置重复警报
- Android日历序列化与Java 6不兼容
- 解决方案(从C#app调用Crystal Report导致“数据库登录失败”或“加载报告失败”)
- Jackson ObjectMapper给出了递归数据类型的错误
- Android java测试浮动负值和正值不起作用吗()
- Settings.Secure.ANDROID_ID在构建APK和签名APK中为何不同()
- Struts 2 - 没有映射名称空间[/]的动作和与上下文路径[/ LoginApplication]相关联的动作名称[validateLogin]