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

custom adapter for gridview's getView() method not returning correct content

我正在为 gridview 使用自定义适配器 class,其中有一个 Button 和一个 TextView。我在 gridview 中显示日历月份,当我单击 Button 时,我想在 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;
    }

but it shows 28 always i.e. only last button's value of gridview

因为 gridcell 持有在最后一次调用 getView 时初始化的引用。

使用 onClickv 参数获得点击按钮文本:

gridcell.setOnClickListener(new OnClickListener() {

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