【问题标题】:notifyDataSetChanged() not refreshing in gridviewnotifyDataSetChanged()未在gridview中刷新
【发布时间】:2016-11-16 04:49:26
【问题描述】:

所以我试图从头开始创建一个自定义日历,但它看起来仍然像一个基本日历。 我创建了自己的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。大多数功能只是确定我的 datelist 数组。

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

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

【问题讨论】:

  • 你确定你在从 changemonth() 调用的 init() 中获得了新值;
  • 我认为这是因为您总是在 getItemId() 中返回 0。至少尝试返回位置。
  • 您每次都在为 DateList 创建新对象,但在适配器中,您有旧对象的引用,这是您在创建适配器时最初创建的对象。

标签: java android gridview calendar notifydatasetchanged


【解决方案1】:

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

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

像这样进行更改并检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多