notifyDataSetChanged()未在gridview中刷新

时间:2016-11-16 04:49:27

标签: java android gridview calendar notifydatasetchanged

因此,我尝试从头开始创建自定义日历,它仍然看起来像一个基本的日历。 我创建了自己的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和我的adapters。大多数功能只是确定我的日期列表数组。

  • notifyDataSetChanged仅适用于previousnext功能,但不起作用。

我一直试图找到一个类似于我的问题,但我发现它确实无效。 谢谢

1 个答案:

答案 0 :(得分:0)

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

 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();
        }

制作这样的字符并检查。