1

我为我ArrayAdapter的 for a定义了一个自定义视图 (xml) ListView,它添加了一个Button. 我想OnClickListenerButton每行创建一个,但不创建自定义适配器。这可能吗,还是 Android 会强迫我ArrayAdapter为我的ListView.

这是我正在做的一个片段:

glAdapter = new ArrayAdapter<Group>(getActivity(), R.layout.fragment_grouprow, R.id.groupRowText, ListOfGroups);
ListView groupListView = (ListView)mainView.findViewById(R.id.listViewGroupMain);
groupListView.setAdapter(glAdapter);
groupListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          // Call function
      }
});

现在我想在我的自定义视图中添加一个OnClickListenerButton

4

1 回答 1

1

您必须实现自己的适配器并在按钮上getView()设置方法。OnClickListener这里开始示例:

public class CustomAdapter extends ArrayAdapter<Integer> {
    private ListView listView;

    public CustomAdapter(Context context, int textViewResourceId, Integer[] objects, ListView listView) {
        super(context, textViewResourceId, objects);
        this.listView = listView;
    }


    static class ViewHolder {
        TextView text;
        Button btn;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Integer color = getItem(position);

        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
            rowView = inflater.inflate(R.layout.list_view_row, parent, false);
            ViewHolder h = new ViewHolder();
            h.text = (TextView) rowView.findViewById(R.id.item_text);
            h.btn = rowView.findViewById(R.id.btn);
            rowView.setTag(h);
        }

        ViewHolder h = (ViewHolder) rowView.getTag();

        h.text.setText(color);
        h.indicator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // DO what you want to recieve on btn click there.
            }
        });

        return rowView;
    }
}

ViewHolder 在这里进行优化。你可以在那里读到这个。

于 2013-07-09T10:03:15.113 回答