2016-11-16 70 views
1

所以我試圖從頭開始創建一個自定義日曆,它仍然看起來像一個基本的日曆。 我創建了我自己的CustomAdapter,它實現了ArrayAdapter,以便它可以自動更改值。這是我走到這一步:notifyDataSetChanged()不在gridview中刷新

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

  • 就本問題而言,您只需要查看onCreate,previous和我的adapters。大部分功能僅用於確定我的日期列表數組。

  • notifyDataSetChanged僅在previousnext函數中,它們不起作用。

我一直在試圖找到類似於我的問題,但我找不到實際的作品。 謝謝

+0

你確定你正在從changemonth()獲得init()調用中的新值。 – Pavya

+0

我認爲這是因爲你總是在'getItemId()'中返回0。至少嘗試返回位置。 –

+0

您每次都在爲DateList創建新對象,但在適配器中您引用了舊對象,這是在創建適配器時最初創建的對象。 –

回答

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

使這樣的chages和檢查。

+0

它工作!非常感謝 ! :D –

+0

然後UpVote ... !!! –

+0

哦,對不起,我仍然是堆棧溢出新..完成:) –