2016-11-16 5 views
1

だから私はカレンダーを最初から作成しようとしていますが、基本カレンダーのように見えます。 ArrayAdapterを実装した私自身のCustomAdapterを作成して、値を自動的に変更できるようにしました。これは、私がこれまでに得たもの:このCustomadapter.javagridviewでnotifyDataSetChanged()がリフレッシュされない

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 

    } 
} 

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; 

    } 
} 

もコードは正常に動作しますが、私はクリックしたときに私の前と次のボタン(onClick = "previous"onClick = "next"を使用しています)では何も起こりません。値は変更されません。誰でも知っている理由は?役立つかもしれない

注:

  • 私は私のArrayAdapter代わりのArrayListで文字列の配列を使用しています。

  • onCreatepreviousと私の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(); 
     } 

このような変更を加えて確認してください。

+0

それは働いた!どうもありがとうございます ! :D –

+0

その後UpVote ... !!! –

+0

申し訳ありませんが、私はまだスタックオーバーフローで新しいです..完了:) –

関連する問題