我正在处理自定义日历,每件事情都正常工作,但问题是当点击网格(在我的情况下是按钮)时,我从模型中获取空值(Attendance_items)。
请帮帮我。
这是我的活动
public class My_Attendance extends BaseActivity implements OnClickListener {
private TextView currentMonth;
private ImageView prevMonth;
private ImageView nextMonth;
private GridView calendarView;
private GridCellAdapter adapter;
private Calendar _calendar;
private int month, year;
String ResponseResult;
String webMethName;
String currentMonthForAttendanceDetails;
String currentYearForAttendanceDetails;
private List<Attendance_items> itemsOfAttendance = new ArrayList<Attendance_items>();
private final DateFormat dateFormatter = new DateFormat();
private static final String dateTemplate = "MMMM yyyy";
private ProgressDialog progressDialog = null;
String userId;
String schoolId;
int thisYear;
int thisMonth;
int thisDay;
int totalDays = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_attendance);
SessionManager sessionManagerNgo = new SessionManager(My_Attendance.this);
HashMap<String, String> typeOfUser = sessionManagerNgo.getActiveUser();
userId = typeOfUser.get(SessionManager.KEY_USERID);
schoolId = typeOfUser.get(SessionManager.KEY_SCHOOLID);
mydb = new DataBaseHelper(this);
Calendar calendar = Calendar.getInstance();
thisYear = calendar.get(Calendar.YEAR);
thisMonth = calendar.get(Calendar.MONTH) + 1;
thisDay = calendar.get(Calendar.DAY_OF_MONTH);
prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
prevMonth.setOnClickListener(this);
nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
nextMonth.setOnClickListener(this);
calendarView = (GridView) this.findViewById(R.id.calendar);
_calendar = Calendar.getInstance(Locale.getDefault());
month = _calendar.get(Calendar.MONTH) + 1;
year = _calendar.get(Calendar.YEAR);
currentMonth = (TextView) this.findViewById(R.id.currentMonth);
currentMonth.setText(DateFormat.format(dateTemplate, _calendar.getTime()));
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please Wait.");
progressDialog.show();
getAttendanceDetails(month, year);
adapter = new GridCellAdapter(getApplicationContext(), R.id.calendar_day_gridcell, month, year, itemsOfAttendance);
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);
}
private void setGridCellAdapterToDate(int month, int year) {
totalDays = 0;
adapter = new GridCellAdapter(getApplicationContext(), R.id.calendar_day_gridcell, month, year, itemsOfAttendance);
_calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
currentMonth.setText(DateFormat.format(dateTemplate, _calendar.getTime()));
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);
}
@Override
public void onClick(View v) {
if (v == prevMonth) {
if (month <= 1) {
month = 12;
year--;
} else {
month--;
}
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please Wait.");
progressDialog.show();
getAttendanceDetails(month, year);
setGridCellAdapterToDate(month, year);
}
if (v == nextMonth) {
if (month > 11) {
month = 1;
year++;
} else {
month++;
}
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please Wait.");
progressDialog.show();
getAttendanceDetails(month, year);
setGridCellAdapterToDate(month, year);
}
}
private void getAttendanceDetails(int month, int year) {
currentMonthForAttendanceDetails = String.valueOf(month);
currentYearForAttendanceDetails = String.valueOf(year);
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
public class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
webMethName = "P_MonthWiseAttendanceDetails";
userId = "120032";
schoolId = "13";
int currentMonthForAttendanceDetails = month;
int currentYearForAttendanceDetails = year;
ResponseResult = Common_Webservice.ShowAttendanceDetailsForParents(userId, schoolId, currentMonthForAttendanceDetails, currentYearForAttendanceDetails, webMethName);
return null;
}
@Override
protected void onPostExecute(Void res) {
if (ResponseResult.equals("Details Not Found")) {
progressDialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(My_Attendance.this);
builder.setTitle("Result");
builder.setMessage("Attendance Details Not Available For this Month.");
AlertDialog alert1 = builder.create();
alert1.show();
} else if (ResponseResult.equals("No Network Found")) {
progressDialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(My_Attendance.this);
builder.setTitle("Result");
builder.setMessage("There is Some Network Issues Please Try Again Later.");
AlertDialog alert1 = builder.create();
alert1.show();
} else {
progressDialog.dismiss();
try {
JSONArray jsonArray = new JSONArray(ResponseResult);
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject obj = jsonArray.getJSONObject(i);
Attendance_items attendanceItems = new Attendance_items();
attendanceItems.setAttendanceId(obj.getString("Attendance_id"));
attendanceItems.setStudentId(obj.getString("Student_Id"));
attendanceItems.setSwipCardNo(obj.getString("SwipCard_No"));
attendanceItems.setSchoolId(obj.getString("School_Id"));
attendanceItems.setMachineId(obj.getString("Machine_Id"));
attendanceItems.setMachineNo(obj.getString("Machine_no"));
attendanceItems.setGroupId(obj.getString("Group_Id"));
attendanceItems.setAttType(obj.getString("Att_Type"));
attendanceItems.setAttDate(obj.getString("Att_Date"));
attendanceItems.setAttTime(obj.getString("Att_Time"));
attendanceItems.setAttStatus(obj.getString("Att_Status"));
attendanceItems.setTrkSms(obj.getString("Trk_Sms"));
attendanceItems.setPresentDate(obj.getString("PresentDate"));
attendanceItems.setPresentMonth(obj.getString("PresentMonth"));
attendanceItems.setPresentYear(obj.getString("PresentYear"));
itemsOfAttendance.add(attendanceItems);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
}
}
这是我的适配器类
public class GridCellAdapter extends BaseAdapter implements OnClickListener {
private static final String tag = "GridCellAdapter";
private final Context _context;
private final List<String> list;
private static final int DAY_OFFSET = 1;
private final String[] weekdays = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
private final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
private final int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private int daysInMonth;
private int currentDayOfMonth;
private int currentWeekDay;
private Button gridcell;
private TextView num_events_per_day;
private final HashMap<String, Integer> eventsPerMonthMap;
private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");
List<Attendance_items> itemsDetails;
// Days in Current Month
public GridCellAdapter(Context context, int textViewResourceId, int month, int year, List<Attendance_items> itemsOfAttendance) {
super();
this._context = context;
this.list = new ArrayList<String>();
this.itemsDetails = itemsOfAttendance;
Calendar calendar = Calendar.getInstance();
setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
// Print Month
printMonth(month, year);
// Find Number of Events
eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
}
private String getMonthAsString(int i) {
return months[i];
}
private String getWeekDayAsString(int i) {
return weekdays[i];
}
private int getNumberOfDaysOfMonth(int i) {
return daysOfMonth[i];
}
public String getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
private void printMonth(int mm, int yy) {
int trailingSpaces = 0;
int daysInPrevMonth = 0;
int prevMonth = 0;
int prevYear = 0;
int nextMonth = 0;
int nextYear = 0;
int currentMonth = mm - 1;
String currentMonthName = getMonthAsString(currentMonth);
daysInMonth = getNumberOfDaysOfMonth(currentMonth);
GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
if (currentMonth == 11) {
prevMonth = currentMonth - 1;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 0;
prevYear = yy;
nextYear = yy + 1;
} else if (currentMonth == 0) {
prevMonth = 11;
prevYear = yy - 1;
nextYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 1;
} else {
prevMonth = currentMonth - 1;
nextMonth = currentMonth + 1;
nextYear = yy;
prevYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
}
int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
trailingSpaces = currentWeekDay;
if (cal.isLeapYear(cal.get(Calendar.YEAR)))
if (mm == 2)
++daysInMonth;
else if (mm == 3)
++daysInPrevMonth;
// Trailing Month days
for (int i = 0; i < trailingSpaces; i++) {
list.add(String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" + getMonthAsString(prevMonth) + "-" + prevYear);
}
// Current Month Days
for (int i = 1; i <= daysInMonth; i++) {
String mydate = (String.valueOf(i) + " " + getMonthAsString(currentMonth) + " " + yy);
SimpleDateFormat inFormat = new SimpleDateFormat("d MMM yyyy");
Date date = null;
try {
date = inFormat.parse(mydate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
String currentDayName = outFormat.format(date);
if (i == getCurrentDayOfMonth()) {
list.add(String.valueOf(i) + "-BLUE" + "-" + getMonthAsString(currentMonth) + "-" + yy);
} else {
list.add(String.valueOf(i) + "-WHITE" + "-" + getMonthAsString(currentMonth) + "-" + yy);
}
}
// Leading Month days
for (int i = 0; i < list.size() % 7; i++) {
list.add(String.valueOf(i + 1) + "-GREY" + "-" + getMonthAsString(nextMonth) + "-" + nextYear);
}
}
private HashMap<String, Integer> findNumberOfEventsPerMonth(int year, int month) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
return map;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.my_attendance_gridcell, parent, false);
}
num_events_per_day = (TextView) row.findViewById(R.id.num_events_per_day);
// Get a reference to the Day gridcell
gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
gridcell.setOnClickListener(this);
// ACCOUNT FOR SPACING
String[] day_color = list.get(position).split("-");
String color = day_color[1];
String theday = day_color[0];
String themonth = day_color[2];
String theyear = day_color[3];
gridcell.setText(theday);
gridcell.setTag(theday + "-" + themonth + "-" + theyear);
String myCurrentDate = theday + " " + themonth + " " + theyear;
SimpleDateFormat inFormat = new SimpleDateFormat("d MMM yyyy");
Date date = null;
try {
date = inFormat.parse(myCurrentDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
String currentDayName = outFormat.format(date);
//for event
if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) {
if (eventsPerMonthMap.containsKey(theday)) {
num_events_per_day = (TextView) row.findViewById(R.id.num_events_per_day);
Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
num_events_per_day.setText(numEvents.toString());
}
}
// Set the Day GridCell
gridcell.setText(theday);
gridcell.setTag(theday + "-" + themonth + "-" + theyear);
//checking to add day for remaining space of month and add next n previous months date
if (day_color[1].equals("GREY")) {
gridcell.setTextColor(getResources().getColor(R.color.lightgray02));
gridcell.setBackgroundColor(getResources().getColor(R.color.colorwhite));
}
//checking for actual days of month
else if (day_color[1].equals("WHITE")) {
if (currentDayName.equals("Sunday")) {
gridcell.setTextColor(getResources().getColor(R.color.colorred500));
gridcell.setBackgroundColor(getResources().getColor(R.color.colorwhite));
}
}
//checking for current date
else if (day_color[1].equals("BLUE")) {
if (currentDayName.equals("Sunday")) {
gridcell.setTextColor(getResources().getColor(R.color.colorred500));
gridcell.setBackgroundColor(getResources().getColor(R.color.colorwhite));
}
}
return row;
}
@Override
public void onClick(View view) {
//add event for each date and show it{timing}
if (view.getId() == R.id.calendar_day_gridcell) {
Attendance_items listItems = new Attendance_items();
String inTime = "In Time : " + listItems.getAttTime();
String outTime = "Out Time : " + listItems.getAtt_OutTime();
String attendanceType = "Type : " + listItems.getAttType();
}
}
public int getCurrentDayOfMonth() {
return currentDayOfMonth;
}
private void setCurrentDayOfMonth(int currentDayOfMonth) {
this.currentDayOfMonth = currentDayOfMonth;
}
public void setCurrentWeekDay(int currentWeekDay) {
this.currentWeekDay = currentWeekDay;
}
}
这是我的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightgray"
android:orientation="vertical">
<LinearLayout
android:id="@+id/buttonlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:gravity="left|top"
android:orientation="horizontal">
<ImageView
android:id="@+id/prevMonth"
android:layout_width="@dimen/_20sdp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/_10sdp"
android:src="@drawable/calendar_left_arrow_selector" />
<TextView
android:id="@+id/currentMonth"
android:layout_width="fill_parent"
android:layout_height="@dimen/_40sdp"
android:layout_weight="0.6"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/nextMonth"
android:layout_width="@dimen/_20sdp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="@dimen/_10sdp"
android:src="@drawable/calendar_right_arrow_selector" />
</LinearLayout>
<RelativeLayout
android:id="@+id/daysLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonlayout"
android:layout_marginLeft="@dimen/_5sdp"
android:layout_marginRight="@dimen/_5sdp"
android:background="@drawable/calendar_days">
</RelativeLayout>
<GridView
android:id="@+id/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/daysLayout"
android:layout_marginLeft="@dimen/_5sdp"
android:layout_marginRight="@dimen/_5sdp"
android:numColumns="7">
</GridView><RelativeLayout>
这里是gridLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:orientation="vertical" >
<Button
android:id="@+id/calendar_day_gridcell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/_3sdp"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" >
</Button>
<TextView
android:id="@+id/num_events_per_day"
style="@style/calendar_event_style"
android:layout_width="@dimen/_10sdp"
android:layout_height="@dimen/_10sdp"
android:layout_gravity="right" >
</TextView>
在onclick方法中从Attendance_items获取空值。 我做错了什么?
谢谢。答案 0 :(得分:0)
Attendance_items listItems = new Attendance_items();
请试试这个。希望它有所帮助
final Button btn = (Button) vi.findViewById(R.id.btn);
这是空的。你没有在listItems中设置任何东西
现在最后的解决方案是在getView btn.setonclicklistner
中声明你的按钮。并在声明后添加onClick
,并在btn.setonclicklistner
中添加size:'4', color:'white'
代码。然后你很容易在那里找到位置。希望现在这可以帮助你
答案 1 :(得分:0)
//add event for each date and show it{timing}
if (view.getId() == R.id.calendar_day_gridcell) {
Attendance_items listItems = new Attendance_items();
String inTime = "In Time : " + listItems.getAttTime();
String outTime = "Out Time : " + listItems.getAtt_OutTime();
String attendanceType = "Type : " + listItems.getAttType();
}
你试图从一个新项目中获取价值......当然它是空的