Android gridview的自定义适配器';s getView()方法未返回正确的内容

Android gridview的自定义适配器';s getView()方法未返回正确的内容,android,listview,gridview,Android,Listview,Gridview,我正在为gridview使用自定义适配器类,其中有一个按钮和一个TextView。我在gridview中显示日历月,当我点击按钮时,我想在Toast中显示按钮的文本(即当前日期)。但它始终显示28,即仅显示gridview最后一个按钮的值。 我的getView()方法代码是: public View getView(int position, View convertView, ViewGroup parent) { View row = convertView;

我正在为gridview使用自定义适配器类,其中有一个按钮和一个TextView。我在gridview中显示日历月,当我点击按钮时,我想在Toast中显示按钮的文本(即当前日期)。但它始终显示28,即仅显示gridview最后一个按钮的值。 我的getView()方法代码是:

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;
    }
但它始终显示28,即仅显示gridview最后一个按钮的值

因为
gridcell
保留在上次调用
getView
时初始化的引用

使用
onClick
v
参数获取单击的按钮文本:

gridcell.setOnClickListener(new OnClickListener() {

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