使用自定义适配器替换GridView按钮上的按钮文本?

时间:2015-12-03 17:04:35

标签: android gridview android-adapter android-viewholder

我有一个GridView,它是通过自定义GridViewAdapter创建的,它扩展了BaseAdapter以创建其视图。 GridView中的每个单元格都由一个按钮表示。

在View getView方法的内部,我使用ViewHolder来缓存按钮。

holder.gridPanel.setText(panel.getNumberPanel());

然后我尝试将OnClickListener应用于此按钮,但是即使Toast通知返回正确的值来设置按钮的新文本,按钮的文本似乎也没有改变。

holder.gridPanel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(context, panel.getAlphabetPanel(), Toast.LENGTH_SHORT).show();
            holder.gridPanel.setText(panel.getAlphabetPanel());

        }
    });

更改已存储在ViewHolder中的按钮文本的正确方法是什么?

代码:

static class ViewHolder {
    Button gridPanel;

}

public class GridViewAdapter extends BaseAdapter {

    private Context context;
    ArrayList<Panel> panelList;
    Panel panel;

public GridViewAdapter(Context context, ArrayList<Panel> panelList){
    this.context = context;
    this.panelList = panelList;
}

@Override
public int getCount() {
    return panelList.size();
}

@Override
public Object getItem(int position) {
    return panelList.get(position);
}

@Override
public long getItemId(int position) {
    return panelList.indexOf(getItem(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.game_grid_item_layout, parent, false);
        holder = new ViewHolder();
        holder.gridPanel = (Button) convertView.findViewById(R.id.grid_AlphabetPanel);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    panel = panelList.get(position);
    holder.gridPanel.setText(panel.getNumberPanel());


    holder.gridPanel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Toast.makeText(context, panel.getAlphabetPanel(), Toast.LENGTH_SHORT).show();
            holder.gridPanel.setText(panel.getAlphabetPanel());

        }
    });



    return convertView;
}


// below methods are in a different activity

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    ArrayList<Panel> panelList = new ArrayList<>();
    ArrayList<String> alphaList = new ArrayList<>();
    ArrayList<String> numberList = new ArrayList<>();
    alphaList.add("A");
    alphaList.add("B");
    alphaList.add("C");
    alphaList.add("D");
    alphaList.add("E");
    numberList.add(Integer.toString(1));
    numberList.add(Integer.toString(2));
    numberList.add(Integer.toString(3));
    numberList.add(Integer.toString(4));
    numberList.add(Integer.toString(5));

    for(int i = 0; i < 5; i++){
        String alp = alphaList.get(i);
        String numb = numberList.get(i);
        Panel panelObject = new Panel(alp, numb);
        panelList.add(panelObject);

    }

    GridView panelGrid = (GridView) findViewById(R.id.gridView_Panel);
    GridViewAdapter gridViewAdapter = new GridViewAdapter(this, panelList);
    panelGrid.setAdapter(gridViewAdapter);

}

1 个答案:

答案 0 :(得分:0)

如果有人在想,我会回答。

初始化ViewHolder时调用setOnClickListener可以正常工作,而在循环外执行则不允许更改数据。

在代码中:

if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.game_grid_item_layout, parent, false);
        holder = new ViewHolder();
        holder.gridPanel = (Button) convertView.findViewById(R.id.grid_AlphabetPanel);
        holder.gridPanel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                holder.gridPanel.setText(panel.getAlphabetPanel());
            }
        });

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }