2012-08-29 57 views
0

enter image description here 这里是图像,我想只显示当前的月份而不是下一天,而不是前几天。 这里是我的适配器代码,我管理和设置adapter.and和类文件中的日子我已经在gridview中设置适配器。你可以帮忙吗?如何在gridview中只显示当前月份的日期?

package com.ManageMyTimeTableAdapter;

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import android.content.Context; 
import android.graphics.Color; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 
import com.ManageMyTimeTable.CalendarView; 
import com.ManageMyTimeTable.DaliyScreenActivity; 
import com.ManageMyTimeTable.DateUtils; 
import com.ManageMyTimeTable.R; 

public class CalendarAdapter extends BaseAdapter{ 
private Date[] calendarGrid; 
private Context mContext; 
private LayoutInflater inflater; 
private static Calendar mCal; 
private String today; 
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy"); 

public CalendarAdapter(Context context){ 
    mContext = context; 
    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mCal = Calendar.getInstance(); 
    CalendarView.dateStore.clear(); 
    initCalendar(mCal);  
} 
private void initCalendar(Calendar cal){ 

    Calendar c = Calendar.getInstance(); 
    c.setTime(cal.getTime()); 
    c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1); 

    int startOfWeek = c.get(Calendar.DAY_OF_WEEK); 
    Log.e("fdfdfdf",""+startOfWeek); 
    Log.e("-1 vagar nu",""+c.get(Calendar.DAY_OF_WEEK)); 

    c.add(Calendar.DATE, -startOfWeek); 

    calendarGrid = new Date[6 * 7]; 
    int gridCount = 0; 
    for (int week = 0; week < 6; week++) { 
     for (int day = 0; day < 7; day++) { 
      Date dt = c.getTime(); 
      dt.setHours(0); 
      dt.setMinutes(0); 
      dt.setSeconds(0); 
      calendarGrid[gridCount++] = dt; 
      c.add(Calendar.DATE, 1); 
      } 
     } 
    } 

public void nextMonth() { 

    mCal.set(mCal.get(Calendar.YEAR), mCal.get(Calendar.MONTH) + 1, mCal.get(Calendar.DATE)); 
    initCalendar(mCal); 
    notifyDataSetChanged(); 
} 

public void prevMonth() { 

    mCal.set(mCal.get(Calendar.YEAR), mCal.get(Calendar.MONTH) - 1, mCal.get(Calendar.DATE)); 
    initCalendar(mCal); 
    notifyDataSetChanged(); 
} 

public Calendar getCurrentCalendar() { 
    return mCal; 
} 

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

@Override 
public Date getItem(int position) { 
    return calendarGrid[position]; 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 

    if (convertView == null) { 

     holder = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.raw_my_calendar, null); 
     holder.txtDate = (TextView) convertView.findViewById(R.id.txtCalDate); 
     holder.txtEventCount = (TextView) convertView.findViewById(R.id.txtEventCount); 
     convertView.setTag(holder); 

    }else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    Date date = getItem(position); 

    holder.txtDate.setText(String.format("%2d", date.getDate())); 
    holder.txtDate.setTag(format.format(date).toString()); 
    String keyDate = DateUtils.formatDate(DateUtils.DB_DATE_FORMAT, date); 
    setStyle(keyDate, position, holder.txtDate, convertView); 

    if(CalendarView.dateStore.size()>= position+1){ 

     Log.d(" Calender Adapter "," Clear arrayList **** "+CalendarView.dateStore.size()); 
     CalendarView.dateStore.clear();   
    }  
    CalendarView.dateStore.add(format.format(date).toString());  
    return convertView; 
} 

private void setStyle(String dateKey, int position, TextView txt, View convertView){ 

    if (dateKey.equals(today)) 
     convertView.setBackgroundColor(Color.parseColor("#88D23218")); 
    else 
     convertView.setBackgroundColor(Color.parseColor("#88CCCCCC")); 

    if (getItem(position).getMonth() != mCal.get(Calendar.MONTH)) 
     txt.setTextColor(Color.parseColor("#FF787878")); 
    else 

     txt.setTextColor(Color.parseColor("#FF000000")); 

     if(DaliyScreenActivity.month_member_id_arr.contains(txt.getText().toString())){    

      convertView.setBackgroundColor(Color.YELLOW); 
      convertView.setBackgroundColor(android.graphics.Color.rgb(230, 187, 60));    
     }  
    } 
static class ViewHolder { 
    TextView txtDate; 
    TextView txtEventCount; 
} 

}

回答

1

在你getView方法...更改最后if..else

此,

if (getItem(position).getMonth() != mCal.get(Calendar.MONTH)) 
{ 
     txt.setTextColor(Color.parseColor("#FF787878")); 
} 
else 
{ 
     txt.setTextColor(Color.parseColor("#FF000000")); 

      if(DaliyScreenActivity.month_member_id_arr.contains(txt.getText().toString())){    

      convertView.setBackgroundColor(Color.YELLOW); 
      convertView.setBackgroundColor(android.graphics.Color.rgb(230, 187, 60));    
     }  
} 

我的意思是说让你的代码来改变背景到else部分的黄色。

1

检查,凡不当月的细胞都将置自己灰色的文字颜色和两种文字颜色设置为相同的颜色作为背景,甚至更好设置空文本那些细胞。

if (getItem(position).getMonth() != mCal.get(Calendar.MONTH)) 
    txt.setTextColor(Color.parseColor("#88CCCCCC")); 

if (getItem(position).getMonth() != mCal.get(Calendar.MONTH)) 
    txt.setText(""); 
+0

嘿Ridcully感谢获取提示..它的工作,但不完全如此,我可以解决使用不同的审判。我现在可以在这里冒险,所以再次感谢 – Google

相关问题