如何在右边的图片上做最近时间的时间表?对我来说不是,我是新手。请帮我。
--Fragment.java
public class DayFragment extends ListFragment {
String[] time = new String[] { "06:30","06:40","06:48","06:56","07:04","07:12","07:20","07:28","07:36", "07:44",
"07:52", "08:00", "08:08", "08:16", "08:22", "08:31", "08:40", "08:50", "09:00", "09:10", "09:20",
"09:30", "09:40", "09:50", "10:00", "10:10", "10:20", "10:30", "10:40", "10:50", "11:00", "11:10",
"11:20", "11:30", "11:40", "11:50", "12:00", "12:10", "12:20", "12:30", "12:40", "12:50", "13:00",
"13:10", "13:20", "13:30", "13:40", "13:50", "14:00", "14:10", "14:20", "14:30", "14:40", "14:50",
"15:00", "15:10", "15:20", "15:30", "15:40", "15:50", "16:00", "16:10", "16:20", "16:30", "16:40",
"16:50", "17:00", "17:08", "17:16", "17:24", "17:32", "17:40", "17:48", "17:56", "18:04", "18:12",
"18:20", "18:28", "18:36", "18:44", "18:51", "19:00", "19:10", "19:20", "19:30", "19:39", "19:50",
"20:03", "20:16", "20:30", "20:43", "20:56", "21:10", "21:23", "21:36", "21:50", "22:03", "22:16",
"22:30", "22:43", "22:56", "23:10", "23:23", "23:36"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dayfragment, container, false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, time);
setListAdapter(adapter);
Time now = new Time();
now.set(now);
return view;
}}
- fragment.xml之
public class DayFragment extends ListFragment {
String[] time = new String[] { "06:30","06:40","06:48","06:56","07:04","07:12","07:20","07:28","07:36", "07:44",
"07:52", "08:00", "08:08", "08:16", "08:22", "08:31", "08:40", "08:50", "09:00", "09:10", "09:20",
"09:30", "09:40", "09:50", "10:00", "10:10", "10:20", "10:30", "10:40", "10:50", "11:00", "11:10",
"11:20", "11:30", "11:40", "11:50", "12:00", "12:10", "12:20", "12:30", "12:40", "12:50", "13:00",
"13:10", "13:20", "13:30", "13:40", "13:50", "14:00", "14:10", "14:20", "14:30", "14:40", "14:50",
"15:00", "15:10", "15:20", "15:30", "15:40", "15:50", "16:00", "16:10", "16:20", "16:30", "16:40",
"16:50", "17:00", "17:08", "17:16", "17:24", "17:32", "17:40", "17:48", "17:56", "18:04", "18:12",
"18:20", "18:28", "18:36", "18:44", "18:51", "19:00", "19:10", "19:20", "19:30", "19:39", "19:50",
"20:03", "20:16", "20:30", "20:43", "20:56", "21:10", "21:23", "21:36", "21:50", "22:03", "22:16",
"22:30", "22:43", "22:56", "23:10", "23:23", "23:36"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dayfragment, container, false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, time);
setListAdapter(adapter);
Time now = new Time();
now.set(now);
return view;
}}
- text_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textColor="#040404"
android:layout_gravity="center"
android:id="@+id/time"/>
答案 0 :(得分:0)
这是一些新手的伪代码:
GET your current time -> Time CURRENT_TIME = new Time();
现在,您必须使用time[]
的值找到最近的时间。所以你正在寻找索引
//exact the same time
time[index] EQUAL CURRENT_TIME
OR
//your current time has to be between your time tables
time[index] is smaller to CURRENT_TIME && time[index+1] is bigger to CURRENT_TIME
在Time
对象中,您有after
,before
和compare
(Java:Time)
小心time[index+1]
和ArrayOutOfBoundsException
。此时,您必须检查time[index+1]
是否存在。如果没有,则最后一次是您要查找的值