在我的Android应用程序中,它会显示带有选择框的月份列表。当您点击一个月时,它应该显示的值仅属于该月份。如果我选择1月份,则表示...但它显示其他月份的信息同样。
// initialize values and settings
private void initValues() {
events = new ArrayList<>();
eventPositions = new ArrayList<>();
progressDialog = new ProgressDialog(this);
ServerManager serverManager = new ServerManager(this);
progressDialog.show();
serverManager.getEvents(authKey, loginType, 0);
selectedMonth = -1;
String[] months = {"Select month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
List<String> monthList = new ArrayList<>(Arrays.asList(months));
System.out.println(monthList);
SelectionListAdapter selectionListAdapter = new SelectionListAdapter(this, monthList);
Spinner monthSpinner = (Spinner)findViewById(R.id.calender_month_spinner);
monthSpinner.setAdapter(selectionListAdapter);
monthSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
calenderListAdepter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
dates = new ArrayList<>();
hasEvent = new ArrayList<>();
events = new ArrayList<>();
calenderListAdepter = new CalenderListAdepter(this, events,hasEvent,dates);
// calenderListAdepter = new CalenderListAdepter(this, dates, hasEvent);
ListView gridView = (ListView) findViewById(R.id.calender_gridView);
gridView.setAdapter(calenderListAdepter);
}
CalenderListAdepter
public class CalenderListAdepter extends BaseAdapter {
private List<EventInfo> events;
private Context context;
private List<String> dates;
private List<Boolean> hasEvent;
public CalenderListAdepter(Calender context, List<EventInfo> events, List<Boolean> hasEvent, List<String> dates) {
this.context = context;
this.events = events;
this.dates=dates;
this.hasEvent=hasEvent;
}
@Override
public int getCount() {
return events.size();
}
@Override
public Object getItem(int position) {
return events.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.calender_list_item, parent, false);
}
TextView messageText = (TextView)convertView.findViewById(R.id.message_textView);
TextView dateText =(TextView)convertView.findViewById(R.id.date_textView);
TextView titleText =(TextView)convertView.findViewById(R.id.Tile_textView);
if (position == 0)
convertView.setBackgroundColor(Color.rgb(231, 231, 239));
else {
// int month = Integer.parseInt(events.get(position).getMonth());
// if (position==month){
convertView.setBackgroundColor(Color.TRANSPARENT); messageText.setText(events.get(position).getNoticeDescription());
dateText.setText(events.get(position).getEventDate().toString());
titleText.setText(events.get(position).getNoticeTitle().toString());
}
return convertView;
}
}
这是包含日期,hasevents的代码
// set up the month view after selecting a month
private void monthSelected() {
eventPositions = new ArrayList<>();
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, selectedMonth - 1);
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
calendar.set(Calendar.YEAR, currentYear);
int startDay = calendar.get(Calendar.DAY_OF_WEEK);
int numberOfDays = calendar.getActualMaximum(Calendar.DATE);
dates.clear();
for (int i=1;i<startDay;i++) {
dates.add("");
hasEvent.add(false);
eventPositions.add(-1);
}
for (int i=1;i<=numberOfDays;i++) {
Integer eventPosition = -1;
dates.add(String.valueOf(i));
Boolean eventFlag = false;
for (int j=0;j<events.size();j++) {
Calendar eventCalender = Calendar.getInstance();
eventCalender.setTime(events.get(j).getStampDate());
int day = eventCalender.get(Calendar.DATE);
int month = eventCalender.get(Calendar.MONTH);
int year = eventCalender.get(Calendar.YEAR);
if (day == i && month == (selectedMonth-1) && year == currentYear) {
eventFlag = true;
eventPosition = j;
}
}
hasEvent.add(eventFlag);
eventPositions.add(eventPosition);
}
int remaining = 42 - dates.size();
for (int i=0;i<remaining;i++) {
dates.add("");
hasEvent.add(false);
eventPositions.add(-1);
}
calenderListAdepter.notifyDataSetChanged();
}
// shows details of an event
public void showEventDetails(View view) {
int tag = (int)view.getTag();
int eventPosition = eventPositions.get(tag);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(events.get(eventPosition).getNoticeTitle());
alertDialogBuilder.setMessage(events.get(eventPosition).getNoticeDescription());
alertDialogBuilder.setPositiveButton("Ok", null);
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
// shows an alert
void showAlert(String alertMessage) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage(alertMessage);
alertDialogBuilder.setPositiveButton("Ok", null);
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
// server response methods
@Override
public void requestFinished(String response, int requestTag) {
if (progressDialog.isShowing())
progressDialog.dismiss();
boolean jsonError = false;
try {
JSONArray jsonArray = new JSONArray(response);
for (int i=0;i<jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String title = jsonObject.optString("notice_title");
String description = jsonObject.optString("notice");
String month = jsonObject.optString("month");
String timeStamp = jsonObject.optString("create_timestamp");
long timeStampValue = Long.parseLong(timeStamp)*1000;
Date date = new Date(timeStampValue);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String formattedDate = formatter.format(date);
String input = formattedDate;
String[] out = input.split("/");
int month1 = Integer.parseInt(out[1]);
// System.out.println("Month = " + out[1]);
events.add(new EventInfo(title, description, timeStamp, formattedDate,month1,date,month));
}
}
catch (JSONException e) {
jsonError = true;
}
if (jsonError) {
showAlert("An error occurred");
}
}
@Override
public void requestFailed(String errorMessage, int requestTag) {
}
@Override
public void imageDownloaded(Bitmap image, int requestTag) {
}
}
答案 0 :(得分:0)
您没有更新calenderListAdepter
onItemSelected
方法中的monthSpinner
,这是用户进行选择时触发的方法。在调用notifyDataSetChanged()
之前,您需要更新适配器中的数据才能生效。
答案 1 :(得分:0)
就像@degs所说,你应该通过传递相应月份的新数据集来更新适配器。
您可以尝试两种方法:
<强>第一强>
您只能在用户点击的一个月内从服务器获取事件数据。在那里你应该创建一个查询来过滤&#34;月&#34;你后端的价值观。
monthSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Here you can show progress dialog
// and call a method to start fetching data from server
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
然后使用requestFinished
方法:
@Override
public void requestFinished(String response, int requestTag) {
if (progressDialog.isShowing()) progressDialog.dismiss();
boolean jsonError = false;
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
/*
Your current codes
*/
events.add(new EventInfo(title, description, timeStamp, formattedDate, month1, date, month));
calenderListAdepter.notifyDataSetChanged();
}
} catch (JSONException e) {
jsonError = true;
}
if (jsonError) {
showAlert("An error occurred");
}
}
<强> SECOND 强>
您从服务器获取所有数据并映射数据。
@Override
public void requestFinished(String response, int requestTag) {
if (progressDialog.isShowing()) progressDialog.dismiss();
// using SparseArray to map data with Integer key
SparseArray<List<EventInfo>> allEventInfo = new SparseArray<>();
boolean jsonError = false;
try {
// jsonArray should contain all events from all months
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
/*
Your current codes
*/
// create a temp variable to store temporary list
List<EventInfo> temp;
// check whether allEventInfo already has a list with corresponding key. If yes, copy the list to temp otherwise initialize temp
if (allEventInfo.get(month1) != null) {
temp = allEventInfo.get(month1);
} else temp = new ArrayList<>();
// add temp with data from server
temp.add(new EventInfo(title, description, timeStamp, formattedDate, month1, date, month));
// put the list back to allEventInfo
// at the end of for loop, allEventInfo should contains all events from all months
allEventInfo.put(month1, temp);
}
} catch (JSONException e) {
jsonError = true;
}
if (jsonError) {
showAlert("An error occurred");
}
}
然后,当用户点击monthSpinner
中的项目时:
monthSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// get data for a month clicked only and add all data to events
// position - 1 because you used index 0 for "Select month"
events.addAll(allEventInfo.get(position - 1));
// notify the adapter
calenderListAdepter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});