2015-02-19 2 views
0

Я использую пользовательский класс адаптера для gridview и имею в виду одну кнопку Button и один TextView. Я показываю каландерный месяц в gridview, когда я нажимаю кнопку «Я хочу показать текст кнопки (то есть текущую дату) в тосте. но он показывает 28 всегда, т. е. только значение последней кнопки gridview. Мой код для метода GetView() является:пользовательский адаптер для метода getView() gridview не возвращает правильный контент

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.screen_gridcell, parent, false); 
     } 
     if (sp != null) { 

     } 
     // Get a reference to the Day gridcell 
     gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell); 
     gridcell.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) {   // HERE I AM SHOWING TEXT OF BUTTON 
       // TODO Auto-generated method stub 
       Toast.makeText(getActivity(), ""+gridcell.getText().toString(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
     gridcell.setOnLongClickListener(this); 
     // ACCOUNT FOR SPACING 

     Log.v(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]; 
     String completeDate = theday + "-" + themonth + "-" + theyear; 
     if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) { 
      if (eventsPerMonthMap.containsKey(theday)) { 
       num_events_per_day = (TextView) row 
         .findViewById(R.id.num_events_per_day); 
       Integer numEvents = (Integer) eventsPerMonthMap.get(theday); 
       num_events_per_day.setText(numEvents.toString()); 
      } 
     } 
     if ((!eventsDaysPerMonthMap.isEmpty()) 
       && (eventsDaysPerMonthMap != null)) { 

      System.out.println("date in list is " + list.get(position)); 
      if (eventsDaysPerMonthMap.containsKey(completeDate)) { 
       // row.setBackgroundColor(Color.GREEN); 
       gridcell.setBackgroundDrawable(getResources().getDrawable(
         R.drawable.eventcalender)); 

       // you can also use setBackground 
      } 
     } 

     // Set the Day GridCell 
     gridcell.setText(theday); 
     gridcell.setTag(theday + "-" + themonth + "-" + theyear); 
     Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-" 
       + theyear); 

     if (day_color[1].equals("GREY")) { 
      gridcell.setTextColor(getResources() 
        .getColor(R.color.gray)); 
     } 
     if (day_color[1].equals("WHITE")) { 
      gridcell.setTextColor(getResources().getColor(
        R.color.Black)); 
     } 
     if (day_color[1].equals("BLUE")) { 
      gridcell.setTextColor(getResources().getColor(R.color.white)); 
     } 
     return row; 
    } 

ответ

2

но он показывает 28 всегда то есть только значение последней кнопки по GridView

Поскольку gridcell ссылка удержания, которая инициализируется при последнем вызове getView.

Используйте v параметр onClick для получения щелкнули текста кнопки:

gridcell.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     String strvalue=((Button)v).getText().toString() 
     Toast.makeText(getActivity(),strvalue, Toast.LENGTH_LONG).show(); 
    } 
}); 
+0

Сво работает нормально сейчас, спасибо –

Смежные вопросы