The rows are coming in reverse order. It should be Monday then Tuesday then Wednesday我有一个gridview,可以动态获取当前周的数据(即星期一到今天)。我面临两个问题。 1)我的gridView元素排列不正确。 2)GridView显示那天数据不存在的数据。它只是复制所有以前的行值。我知道重排问题是因为resetPosition变量没有得到更新。但如果我更新它,我得到NullPointerException。我怎样才能摆脱这些问题?
public class ViewTimeTable extends AppCompatActivity {
private ArrayList<String> mondayStrength,tuesdayStrength,wednesdayStrength,thursdayStrength,fridayStrength;
String [] currentWeek;
int resetPosition;
private Date date;
private Map map;
private int position,pos;
private TextView classTextView,sectionTextView,dateTextView;
private String Class,section,today,day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_day_layout);
classTextView = (TextView)findViewById(R.id.class_textView);
sectionTextView = (TextView)findViewById(R.id.section_textView);
dateTextView = (TextView)findViewById(R.id.date_textView);
Intent intent = getIntent();
ParseAnalytics.trackAppOpenedInBackground(intent);
mondayStrength = new ArrayList<String>();
tuesdayStrength = new ArrayList<>();
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ignored) {
}
map = new HashMap();
String[] lectureTimings = {"Lec/Day","I","II","III","IV","V","VI"};
for(int i =0;i<lectureTimings.length;i++){
mondayStrength.add(lectureTimings[i]);
pos = i;
}
System.out.println(position);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Lectures");
GridView mondayRow = (GridView)findViewById(R.id.mon_grid_view);
final GridAdapterChirag mondayAdapter = new GridAdapterChirag(ViewTimeTable.this,R.layout.grid_element, mondayStrength);
mondayRow.setAdapter(mondayAdapter);
System.out.println(position);
currentWeek = getThisWeek();
Class = intent.getStringExtra("Class");
section = intent.getStringExtra("Section");
position = pos+1;
System.out.println(position);
classTextView.append(Class);
sectionTextView.append(section);
resetPosition=position;
String [] day = {"Mon","Tue","Wed","Thu","Fri"};
for(int i=0;i<currentWeek.length;i++) {
mondayStrength.add(position, day[i]);
System.out.print("ResetPosition before loop"+resetPosition);
query.whereEqualTo("Date", currentWeek[i]);
try {
List<ParseObject> objects = query.find();
for (ParseObject object : objects) {
String lid = object.getString("LectureID");
if (lid.substring(7, 9).equals(Class)) {
String m = String.valueOf(lid.charAt(10));
if (m.equals(section)) {
String periodNumber = lid.substring(4, 6);
if (periodNumber.equals("01"))
position += 1;
else if (periodNumber.equals("02"))
position += 2;
else if (periodNumber.equals("03"))
position += 3;
else if (periodNumber.equals("04"))
position += 4;
else if (periodNumber.equals("05"))
position += 5;
else if (periodNumber.equals("06"))
position += 6;
else
position = 100;
map.put(position, object.getString("Strength"));
}
}
System.out.println("ResetPosition"+resetPosition);
System.out.println("Position"+position);
position = resetPosition;
}
System.out.println(map);
for (int k = 1; k <= map.size(); k++) {
mondayStrength.add(resetPosition + k, map.get(resetPosition+k).toString());
}resetPosition+=7;
System.out.println(mondayStrength.size());
//position++;
//mondayAdapter.notifyDataSetChanged();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
//此方法只返回当前周的日期数组
private String[] getThisWeek() {
int factor;
ArrayList<String> week = new ArrayList<>();
long oneDay = 86400000;
Calendar calendar = Calendar.getInstance();
long todayInMillis = calendar.getTimeInMillis();
String date;
String todaysDay = new SimpleDateFormat("EEE").format(new Date());
if(todaysDay.equals("Mon"))
factor = 0;
else if(todaysDay.equals("Tue"))
factor = 1;
else if(todaysDay.equals("Wed"))
factor = 2;
else if(todaysDay.equals("Thu"))
factor = 3;
else if(todaysDay.equals("Fri"))
factor = 4;
else if(todaysDay.equals("Sat"))
factor = 5;
else
factor = 6;
for(;factor>=0;factor--){
calendar.setTimeInMillis(todayInMillis-factor*oneDay);
Date d = new Date(calendar.getTimeInMillis());
date = new SimpleDateFormat("dd-MM-yyyy").format(d);
week.add(date);
}
String [] runningWeek = new String[week.size()];
runningWeek = week.toArray(runningWeek);
return runningWeek;
}
logcat的:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tonystark.adminlogin/com.example.tonystark.adminlogin.ViewTimeTable}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tonystark.adminlogin.ViewTimeTable.onCreate(ViewTimeTable.java:126)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)