16677

GridView setOnItemClickListener does not respond to clicks

Question:

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)); } }); <hr />

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

Answer1:

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.

Answer2:

I guess your item view contains button or other view that got foucus. Try to set android:descendantFocusability="blocksDescendants" to item view like below:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" > <!-- item --> </RelativeLayout>

Recommend

  • JavaScript calculate years and days from start date
  • how does zoomRange() works?
  • separate compilaton in CUDA
  • How do I select multiple tables in LINQ?
  • Amazon API gateway — proxied requests are returning unreadable data
  • Get all the dates of the month from Day of week
  • pack (type erase) a random number generator
  • why sometime can get error 404 not found (soapclient- response has contents of the response), how to
  • __ldg causes slower execution time in certain situation
  • Extract all urls in a string with python3
  • How do I use the Dom File API from privileged code?
  • Tkinter Python Continuously update Label from serial data?
  • ArrayIndexOutOfBoundsException: 0 error
  • How to insert html into database using PHP
  • Cuda program does not give the correct output when using a CUDA compatible GPU
  • Angular 2 material datepicker highlight special days
  • Cuda char* variable assignment
  • CUDA constant memory value not correct [duplicate]
  • nvcc is picking wrong libcudart library
  • how to change the date control in day pilot scheduler using in angularjs
  • Why does CMake force the use of libcublas with separable compilation?
  • Using CImg: LNK1181: cannot open file “m.lib” on windows 7 x64
  • Creating an object in Swift using the Objective-C factory method gives a compiler error [duplicate]
  • How to get number of days from month and year
  • trouble compiling with custom tensorflow gpu op
  • How to focus on a particular portion of an another html page when click on a button or link
  • Cuda Mutex, why deadlock?
  • Internet explorer and google chrome frame can support webRTC?
  • How to add definitions for cuda source code in cmake
  • Can't make a resizable / draggable
  • getFinancials (quantmod) and tq_get (tidy quant) not working?
  • Python pandas melting data to multiple columns and coulmn names in another column
  • XSLT foreach repeating nodes to flat
  • How to access culture data in globalize.js V1.0.0
  • Invalid Date on validation Date of js
  • MySQL Order by column = x, column asc?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • print() is showing quotation marks in results
  • TFS: Get latest causes slow project reloading
  • Updated Ionic CLI but shows previous version (Windows)