gpt4 book ai didi

java - notificationDataSetChanged() 未在 gridview 中刷新

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:52 26 4
gpt4 key购买 nike

所以我尝试从头开始创建一个自定义日历,它看起来仍然像一个基本日历。我创建了自己的 CustomAdapter 来实现 ArrayAdapter,以便它可以自动更改值。这是我到目前为止得到的:

MainActivity.java

package com.example.aldos.calendar;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

/**
* Created by aldos on 11/13/2016.
*/
public class CustomAdapter extends ArrayAdapter<String> {
private Context c;
private String[] date ;
private LayoutInflater inflater;

public CustomAdapter(Context context,String[] objects) {
super(context, android.R.layout.simple_list_item_1, objects);
this.c = context;
date = objects;
inflater = LayoutInflater.from(context);
}


@Override
public int getCount() {
return date.length;
}


@Override
public long getItemId(int position) {
return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if(convertView == null){
convertView = inflater.inflate(R.layout.activity_main,parent,false);
textView = new TextView(c);
textView.setLayoutParams(new GridView.LayoutParams(120,120));
textView.setGravity(Gravity.CENTER);
textView.setPadding(5,5,5,5);
}else{
textView = (TextView) convertView;
}

textView.setText(date[position]);


return textView;

}
}

还有这个Customadapter.java

package com.example.aldos.calendar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridView;

import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

Calendar calen = Calendar.getInstance();
int month = Calendar.MONTH;
String[] DateList ;
CustomAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DateList = init();

GridView grid = (GridView) findViewById(R.id.gridview);
adapter = new CustomAdapter(this,DateList);
grid.setAdapter(adapter);
}

public void previous(View v){
month--;
DateList = changemonth(month);

for(int i = 0; i < DateList.length;i++){
Log.d("prev",""+ DateList[i]);
}
adapter.notifyDataSetChanged();
}

public void next(View v){
month--;
DateList = changemonth(month);

adapter.notifyDataSetChanged();
}

private String[] init(){
String[] DateList = new String[42];
calen.set(Calendar.DAY_OF_MONTH,1); // first day of the month

int date = calen.get(Calendar.DATE);
int month = calen.get(Calendar.MONTH);
int year = calen.get(Calendar.YEAR);
int day = calen.get(Calendar.DAY_OF_WEEK);


DateList = BeforeMonth(DateList,day,month);

int numdays = daysinamonth(month);
int endmonth = numdays+day -1;
int j = day-1;

while(j < (numdays+day-1) ){
DateList[j] = Integer.toString(calen.get(Calendar.DATE));
calen.add(Calendar.DAY_OF_MONTH,1);
j++;
}

int end = 1;
while(endmonth <42){
DateList[endmonth] = Integer.toString(end);
endmonth++;
end++;
}

return DateList;

}

private String[] changemonth(int month){
calen.set(Calendar.YEAR,month,Calendar.DATE);
return init();
}

//adds the days before the first day of the month to the string list
private String[] BeforeMonth(String[] d, int day, int month){
int LastMo = daysinamonth( month-1); // know how many days in the last month 31 10-1
//Log.d("bmon","the current month" + month + " last month days " + LastMo + " day " + day);
switch (day){
case 1: // sunday
break;
case 2: // monday
d[0] = Integer.toString(LastMo);
break;
case 3: // tuesday
d[0] = Integer.toString(LastMo-1);
d[1] = Integer.toString(LastMo);
break;
case 4: // wednesday
d[0] = Integer.toString(LastMo-2);
d[1] = Integer.toString(LastMo-1);
d[2] = Integer.toString(LastMo);
break;
case 5: // thursday
d[0] = Integer.toString(LastMo-3);
d[1] = Integer.toString(LastMo-2);
d[2] = Integer.toString(LastMo-1);
d[3] = Integer.toString(LastMo);
break;
case 6: //friday
d[0] = Integer.toString(LastMo-4);
d[1] = Integer.toString(LastMo-3);
d[2] = Integer.toString(LastMo-2);
d[3] = Integer.toString(LastMo-1);
d[4] = Integer.toString(LastMo);
break;
case 7: //saturday
d[0] = Integer.toString(LastMo-5);
d[1] = Integer.toString(LastMo-4);
d[2] = Integer.toString(LastMo-3);
d[3] = Integer.toString(LastMo-2);
d[4] = Integer.toString(LastMo-1);
d[5] = Integer.toString(LastMo);
break;
}

return d;
}

//check how many days are in each month
private int daysinamonth(int month){
switch (month){
case 0: // January
return 31;
case 1: // February
return 28;
case 2: // March
return 31;
case 3: // April
return 30;
case 4: // May
return 31;
case 5: // June
return 30;
case 6: // July
return 31;
case 7: // August
return 31;
case 8: // Sept
return 30;
case 9: // Oct
return 31;
case 10: // Nov
return 30;
case 11: // Dec
return 31;
default:
return 0;
} // switch stmt

}
}

代码可以正常工作,但是当我单击上一个和下一个按钮(使用 onClick = "previous"onClick = "next" )时,什么也不会发生。值不会改变。有谁知道为什么吗?

可能有帮助的注释:

  • 我在 ArrayAdapter 中使用字符串数组,而不是 ArrayList

  • 就本问题而言,您只需查看 onCreateprevious 和我的适配器。大多数功能只是确定我的日期列表数组。

  • notifyDataSetChanged 仅在 previousnext 函数中使用,但它们不起作用。

我一直在尝试找到与我的问题类似的问题,但我找不到真正有效的问题。谢谢

最佳答案

在您的代码中,您不会再次为适配器设置值,因此请使用新值设置适配器并通知。

 public void next(View v){
month--;
DateList = changemonth(month);
adapter = new CustomAdapter(this,DateList);
grid.setAdapter(adapter);
adapter.notifyDataSetChanged();
}



public void previous(View v){
month--;
DateList = changemonth(month);

for(int i = 0; i < DateList.length;i++){
Log.d("prev",""+ DateList[i]);
}
adapter = new CustomAdapter(this,DateList);
grid.setAdapter(adapter);
adapter.notifyDataSetChanged();
}

像这样进行更改并检查。

关于java - notificationDataSetChanged() 未在 gridview 中刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com