GridView控件setOnItemClickListener不响应点击 [英] GridView setOnItemClickListener does not respond to clicks
问题描述
我想获得在GridView所选的项目,但是当我点击的项目,没有任何反应。
我不从我的日志code得到任何日志: Log.i(现在的位置,ARG2 +);
下面是我的code:
calendarView =(GridView控件)this.findViewById(R.id.calendar);
calendarView.setOnItemClickListener(新OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
// TODO自动生成方法存根
Log.i(现在的位置,ARG2 +); 按钮TEMP =(按钮)calendarView.getAdapter()的getItem(ARG2)。 temp.setBackgroundColor(getResources()的getColor(R.color.LightGreen));
} });
我的适配器:
公共类GridCellAdapter延伸BaseAdapter {
私有静态最后的字符串标记=GridCellAdapter;
私人最终上下文_context;
私人最终名单<串GT;清单;
私有静态最终诠释DAY_OFFSET = 1;
私人最终的String [] =平日新的String [] {太阳报,星期一,星期二,
星期三,星期四,星期五,星期六};
私人最终的String []月= {一月,二月,三月,
四五,五一,六一,七五,八五,九五,
十月,十一月,十二月};
私人最终诠释[] daysOfMonth = {31,28,31,30,31,30,31,31,30,
31,30,31};
私人最终诠释月,年;
私人诠释daysInMonth,prevMonthDays;
私人诠释currentDayOfMonth;
私人诠释currentWeekDay;
私人按钮栅格单元; 当月//天
公共GridCellAdapter(上下文的背景下,INT textViewResourceId,
INT月,年整型){ 超();
this._context =背景;
this.list =新的ArrayList<串GT;();
this.month =月;
this.year =年; //Log.d(tag,==>传递日期为月:+月+
// +年:+年);
台历挂历= Calendar.getInstance(Locale.ENGLISH);
setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
//Log.d(tag,新的日历:=+ calendar.getTime()的toString());
//Log.d(tagCurrentDayOfWeek:+ getCurrentWeekDay());
//Log.d(tagCurrentDayOfMonth:+ getCurrentDayOfMonth()); //打印月
printMonth(月,年); } 私人字符串getMonthAsString(int i)以{ 回个月[I]
} 私人字符串getWeekDayAsString(int i)以{
返回平日[I]
} 私人诠释getNumberOfDaysOfMonth(int i)以{
返回daysOfMonth [I]
} 公共字符串的getItem(INT位置){
返回list.get(位置);
} 公众诠释的getCount(){
返回则为list.size();
} / **
*打印月
*
* @参数毫米
* @参数YY
* /
私人无效printMonth(INT毫米,诠释YY){
//Log.d(tag,==> printMonth:MM:+毫米++YY:+ YY);
//的天数,以留出空白
//这个月的开始。
INT trailingSpaces = 0;
INT daysIn prevMonth = 0;
INT prevMonth = 0;
INT prevYear = 0;
INT nextMonth = 0;
INT nextYear = 0; INT currentMonth =毫米 - 1;
字符串currentMonthName = getMonthAsString(currentMonth);
daysInMonth = getNumberOfDaysOfMonth(currentMonth); //Log.d(tag,当前月份:++ currentMonthName +具有
// + daysInMonth +的日子。); //公历:减1,设置为每月第一
GregorianCalendar的CAL =新的GregorianCalendar(YY,currentMonth,1);
//Log.d(tag,阳历:=+ cal.getTime()的toString()); 如果(currentMonth == 11){
prevMonth = currentMonth - 1;
daysIn prevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 0;
prevYear = YY;
nextYear = YY + 1;
//Log.d(tag,* - > prevYear:+ prevYear +prevMonth:
// + prevMonth +NextMonth:+ nextMonth
// +NextYear:+ nextYear);
}否则如果(currentMonth == 0){
prevMonth = 11;
prevYear = YY - 1;
nextYear = YY;
daysIn prevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 1;
//Log.d(tag,** - > prevYear:+ prevYear +prevMonth:
// + prevMonth +NextMonth:+ nextMonth
// +NextYear:+ nextYear);
}其他{
prevMonth = currentMonth - 1;
nextMonth = currentMonth + 1;
nextYear = YY;
prevYear = YY;
daysIn prevMonth = getNumberOfDaysOfMonth(prevMonth);
// Log.d(标签,*** - > prevYear:+ prevYear +prevMonth:
// + prevMonth +NextMonth:+ nextMonth
// +NextYear:+ nextYear);
} //计算多少的第一天之前前离开
//一个月。
// getDay()星期天返回0。
INT currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
trailingSpaces = currentWeekDay; //Log.d(tag,周日:+ currentWeekDay +就是
// + getWeekDayAsString(currentWeekDay));
//Log.d(tag号尾部的空格添加+ trailingSpaces);
//Log.d(tag,天在previous月号:+ daysIn prevMonth); 如果(cal.isLeapYear(cal.get(Calendar.YEAR))及&放大器;毫米== 2){
++ daysInMonth;
} //尾随月天
的for(int i = 0; I< trailingSpaces;我++){
//Log.d(tag,
//$ P $的pV月:=
// + prevMonth
// +=>中
// + getMonthAsString(prevMonth)
// +
// +将String.valueOf((daysIn prevMonth
// - trailingSpaces + DAY_OFFSET)
// + I));
list.add(字符串
.valueOf((daysIn prevMonth - trailingSpaces + DAY_OFFSET)
+ I)
+-grey
+ -
+ getMonthAsString(prevMonth)
+ -
+ prevYear);
} //当前月份的天
的for(int i = 1; I< = daysInMonth;我++){
//Log.d(currentMonthName,将String.valueOf(ⅰ)+
// + getMonthAsString(currentMonth)++ YY);
如果(ⅰ== getCurrentDayOfMonth()){
list.add(将String.valueOf(ⅰ)+蓝+ -
+ getMonthAsString(currentMonth)+ - + YY);
}其他{
list.add(将String.valueOf(ⅰ)+-WHITE+ -
+ getMonthAsString(currentMonth)+ - + YY);
}
} //领导月天
的for(int i = 0; I<则为list.size()%7,我++){
//Log.d(tag,下个月:=+ getMonthAsString(nextMonth));
list.add(将String.valueOf第(i + 1)+-grey+ -
+ getMonthAsString(nextMonth)+ - + nextYear);
}
} 众长getItemId(INT位置){
返回的位置;
} 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排= convertView;
如果(行== NULL){
LayoutInflater吹气=(LayoutInflater)_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
行= inflater.inflate(R.layout.calendar_day_gridcell,父母,
假);
} 显示显示=((窗口管理器)getSystemService(WINDOW_SERVICE))
.getDefaultDisplay(); INT高度= 0; 如果(android.os.Build.VERSION.SDK_INT> = 13){
点大小=新点();
display.getSize(大小);
高度= size.y; }其他
高度= display.getHeight(); 浮scaledDensity = getApplicationContext()。getResources()
。.getDisplayMetrics()scaledDensity; 身高=(身高/((int)的scaledDensity)); 如果(calendarView.getCount()→35)
身高=(身高 - 300)/ 7;
其他
身高=(身高 - 300)/ 6; RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,高度); //获取对日栅格单元的参考
栅格单元=(按钮)row.findViewById(R.id.calendar_day_gridcell);
gridcell.setLayoutParams(LP);
//gridcell.setOnClickListener(this); //占间距 //Log.d(tag,当前日:+ getCurrentDayOfMonth());
串[] day_color = list.get(位置).split( - );
串theday = day_color [0];
串themonth = day_color [2];
串的某些地方= day_color [3]; //设置日栅格单元
gridcell.setText(theday);
gridcell.setTag(theday + - + themonth + - +的某些地方); 如果(dateWanted.equals(theday + - + themonth + - +的某些地方))
gridcell.setBackgroundColor(getResources()的getColor(R.color.LightGreen)); //Log.d(tag,设置栅格单元+ theday + - + themonth + -
// +的某些地方); 如果(day_color [1] .equals(灰)){
gridcell.setTextColor(Color.LTGRAY);
}
如果(day_color [1] .equals(白)){
gridcell.setTextColor(Color.WHITE);
}
如果(day_color [1] .equals(蓝)){
gridcell.setTextColor(getResources()。的getColor(
R.color.static_text_color));
} 返回行;
} 公众诠释getCurrentDayOfMonth(){
返回currentDayOfMonth;
} 私人无效setCurrentDayOfMonth(INT currentDayOfMonth){
this.currentDayOfMonth = currentDayOfMonth;
} 公共无效setCurrentWeekDay(INT currentWeekDay){
this.currentWeekDay = currentWeekDay;
} 公众诠释getCurrentWeekDay(){
返回currentWeekDay;
}
}
我有一个类似的问题。我的方式解决它添加以下行到我的适配器getView()方法:
btn.setFocusable(假);
btn.setClickable(假);
凡BTN是参照按钮。
I am trying to get the selected item in a GridView, but when I click on item, nothing happens.
I don't get any logs from my log code: Log.i("postion", arg2+"");
Here is my code:
calendarView = (GridView) this.findViewById(R.id.calendar);
calendarView.setOnItemClickListener( new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("postion", arg2+"");
Button temp = (Button) calendarView.getAdapter().getItem(arg2);
temp.setBackgroundColor(getResources().getColor(R.color.LightGreen));
}
});
My adapter:
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;
private Button gridcell;
// 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 String getWeekDayAsString(int i) {
return weekdays[i];
}
private int getNumberOfDaysOfMonth(int i) {
return daysOfMonth[i];
}
public String getItem(int position) {
return list.get(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;
String currentMonthName = getMonthAsString(currentMonth);
daysInMonth = getNumberOfDaysOfMonth(currentMonth);
//Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
//+ daysInMonth + " days.");
// Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
//Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());
if (currentMonth == 11) {
prevMonth = currentMonth - 1;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 0;
prevYear = yy;
nextYear = yy + 1;
//Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
// + prevMonth + " NextMonth: " + nextMonth
// + " NextYear: " + nextYear);
} else if (currentMonth == 0) {
prevMonth = 11;
prevYear = yy - 1;
nextYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 1;
//Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
// + prevMonth + " NextMonth: " + nextMonth
// + " NextYear: " + nextYear);
} else {
prevMonth = currentMonth - 1;
nextMonth = currentMonth + 1;
nextYear = yy;
prevYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
// Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
// + prevMonth + " NextMonth: " + nextMonth
// + " NextYear: " + nextYear);
}
// Compute how much to leave before before the first day of the
// month.
// getDay() returns 0 for Sunday.
int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
trailingSpaces = currentWeekDay;
//Log.d(tag, "Week Day:" + currentWeekDay + " is "
// + getWeekDayAsString(currentWeekDay));
//Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
//Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);
if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 2) {
++daysInMonth;
}
// Trailing Month days
for (int i = 0; i < trailingSpaces; i++) {
//Log.d(tag,
// "PREV MONTH:= "
// + prevMonth
// + " => "
// + getMonthAsString(prevMonth)
// + " "
// + String.valueOf((daysInPrevMonth
// - trailingSpaces + DAY_OFFSET)
// + i));
list.add(String
.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
+ i)
+ "-GREY"
+ "-"
+ getMonthAsString(prevMonth)
+ "-"
+ prevYear);
}
// Current Month Days
for (int i = 1; i <= daysInMonth; i++) {
//Log.d(currentMonthName, String.valueOf(i) + " "
// + getMonthAsString(currentMonth) + " " + yy);
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++) {
//Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
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;
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 = (Button) row.findViewById(R.id.calendar_day_gridcell);
gridcell.setLayoutParams(lp);
//gridcell.setOnClickListener(this);
// ACCOUNT FOR SPACING
//Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
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));
//Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-"
// + theyear);
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;
}
public int getCurrentDayOfMonth() {
return currentDayOfMonth;
}
private void setCurrentDayOfMonth(int currentDayOfMonth) {
this.currentDayOfMonth = currentDayOfMonth;
}
public void setCurrentWeekDay(int currentWeekDay) {
this.currentWeekDay = currentWeekDay;
}
public int getCurrentWeekDay() {
return currentWeekDay;
}
}
I have had a similar problem. The way I solved it was adding the following lines to the getView() method in my adapter: btn.setFocusable(false); btn.setClickable(false); Where btn is the reference to the button.
这篇关于GridView控件setOnItemClickListener不响应点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!