您好,我有一个不响应的 gridview 的客户适配器
setOnItemClickListener。
我已经查看了这个问题,但没有任何帮助:
https://code.google.com/p/android/issues/detail?id=3414
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
我在我的 logcat 上测试了我的响应,但是当我单击 gridview 中的项目时我没有得到任何响应。
这是我的适配器代码:
public class GridCellAdapter extends BaseAdapter{
private static final String tag = "GridCellAdapter";
private final Context _context;
private final List<String> list;
private static final int DAY_OFFSET = 1;
private final String[] weekdays = new String[] { "Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat" };
private final String[] months = { "January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" };
private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31 };
private final int month, year;
private int daysInMonth, prevMonthDays;
private int currentDayOfMonth;
private int currentWeekDay;
public String lastDateString;
// Days in Current Month
public GridCellAdapter(Context context, int textViewResourceId,
int month, int year) {
super();
this._context = context;
this.list = new ArrayList<String>();
this.month = month;
this.year = year;
//Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
// + "Year: " + year);
Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
//Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
//Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
//Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());
// Print Month
printMonth(month, year);
}
private String getMonthAsString(int i) {
return months[i];
}
private int getNumberOfDaysOfMonth(int i) {
return daysOfMonth[i];
}
public Object getItem(int position) {
return position;
}
public int getCount() {
return list.size();
}
/**
* Prints Month
*
* @param mm
* @param yy
*/
private void printMonth(int mm, int yy) {
//Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
// The number of days to leave blank at
// the start of this month.
int trailingSpaces = 0;
int daysInPrevMonth = 0;
int prevMonth = 0;
int prevYear = 0;
int nextMonth = 0;
int nextYear = 0;
int currentMonth = mm - 1;
daysInMonth = getNumberOfDaysOfMonth(currentMonth);
// Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
if (currentMonth == 11) {
prevMonth = currentMonth - 1;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 0;
prevYear = yy;
nextYear = yy + 1;
} else if (currentMonth == 0) {
prevMonth = 11;
prevYear = yy - 1;
nextYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 1;
} else {
prevMonth = currentMonth - 1;
nextMonth = currentMonth + 1;
nextYear = yy;
prevYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
}
int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
trailingSpaces = currentWeekDay;
if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 2) {
++daysInMonth;
}
// Trailing Month days
for (int i = 0; i < trailingSpaces; i++) {
list.add(String
.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
+ i)
+ "-GREY"
+ "-"
+ getMonthAsString(prevMonth)
+ "-"
+ prevYear);
}
// Current Month Days
for (int i = 1; i <= daysInMonth; i++) {
if (i == getCurrentDayOfMonth()) {
list.add(String.valueOf(i) + "-BLUE" + "-"
+ getMonthAsString(currentMonth) + "-" + yy);
} else {
list.add(String.valueOf(i) + "-WHITE" + "-"
+ getMonthAsString(currentMonth) + "-" + yy);
}
}
// Leading Month days
for (int i = 0; i < list.size() % 7; i++) {
list.add(String.valueOf(i + 1) + "-GREY" + "-"
+ getMonthAsString(nextMonth) + "-" + nextYear);
}
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TextView gridcell = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.calendar_day_gridcell, parent,
false);
}
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
int height = 0;
if (android.os.Build.VERSION.SDK_INT >= 13) {
Point size = new Point();
display.getSize(size);
height = size.y;
} else
height = display.getHeight();
float scaledDensity = getApplicationContext().getResources()
.getDisplayMetrics().scaledDensity;
height = (height / ((int) scaledDensity));
if (calendarView.getCount() > 35)
height = (height - 300) / 7;
else
height = (height - 300) / 6;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, height);
// Get a reference to the Day gridcell
gridcell = (TextView) row.findViewById(R.id.calendar_day_gridcell);
gridcell.setLayoutParams(lp);
//setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
String[] day_color = list.get(position).split("-");
String theday = day_color[0];
String themonth = day_color[2];
String theyear = day_color[3];
// Set the Day GridCell
gridcell.setText(theday);
gridcell.setTag(theday + "-" + themonth + "-" + theyear);
if(dateWanted.equals(theday + "-" + themonth + "-" + theyear))
gridcell.setBackgroundColor(getResources().getColor(R.color.LightGreen));
if (day_color[1].equals("GREY")) {
gridcell.setTextColor(Color.LTGRAY);
}
if (day_color[1].equals("WHITE")) {
gridcell.setTextColor(Color.WHITE);
}
if (day_color[1].equals("BLUE")) {
gridcell.setTextColor(getResources().getColor(
R.color.static_text_color));
}
return row;
}
}
我的 onCreate 中的代码:
calendarView = (GridView) this.findViewById(R.id.calendar);
adapter = new GridCellAdapter(getApplicationContext(),
R.id.calendar_day_gridcell, month, year);
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);
calendarView.setOnItemClickListener(this);
监听器:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("position", arg2+"");
}
我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginTop="15sp"
android:text="From which date to start"
android:textColor="@color/Black"
android:textSize="17sp" />
<RelativeLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="65sp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/nextMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/go_next" />
<ImageButton
android:id="@+id/prevMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:src="@drawable/go_back" />
<TextView
android:id="@+id/textMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"
android:textColor="@color/Black"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/linearLayout4"
android:layout_width="fill_parent"
android:layout_height="35sp"
android:layout_below="@+id/linearLayout1"
android:background="@color/LightBlue"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/textView1"
android:gravity="center_vertical"
android:text="Thu"
android:textColor="@color/Black"
android:textStyle="bold" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:gravity="center_vertical"
android:text="Sat"
android:textColor="@color/Black"
android:textStyle="bold" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:text="Sun"
android:textColor="@color/Black"
android:textStyle="bold" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_toRightOf="@+id/textView4"
android:gravity="center_vertical"
android:text="Fri"
android:textColor="@color/Black"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="31sp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_toRightOf="@+id/textView3"
android:gravity="center_vertical"
android:text="Mon"
android:textColor="@color/Black"
android:textStyle="bold" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_toRightOf="@+id/textView2"
android:gravity="center_vertical"
android:text="Tue"
android:textColor="@color/Black"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:text="Wed"
android:textColor="@color/Black"
android:textStyle="bold" />
</RelativeLayout>
<GridView
android:id="@+id/calendar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
android:numColumns="7"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>