关于 arrayAdapter

发布于 2024-11-16 19:25:52 字数 1016 浏览 4 评论 0原文

我的应用程序中有一个带有适配器的 ListView,我用它来显示带有 checkbox 和 textView 的列表>

public class MyListAdapter extends ArrayAdapter<Model> {
    private LayoutInflater inflater;
    private int position;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public MyListAdapter (Context context, List<Model> listMeasurement){
        super(context, R.layout.simplerow, R.id.empty, listMeasurement);
        inflater= LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent){
        Model model= (Model)this.getItem(position);
        CheckBox checkBox;
        TextView textView;
    }
}

我的问题是:

我想在另一个 activity 中显示另一个 list,这将有一个 image、两个 textViews > 和一个按钮。图像取决于 textView 的值。

最好的方法是做其他ArrayAdapter?或使用其他东西?

提前致谢。

I have in my app a ListView with adapter, I use this to show a list with a chechbox and a textView

public class MyListAdapter extends ArrayAdapter<Model> {
    private LayoutInflater inflater;
    private int position;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public MyListAdapter (Context context, List<Model> listMeasurement){
        super(context, R.layout.simplerow, R.id.empty, listMeasurement);
        inflater= LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent){
        Model model= (Model)this.getItem(position);
        CheckBox checkBox;
        TextView textView;
    }
}

My question is:

I want to show another list in another activity, this will have an image, two textViews and a button. The image depends on the value of the textView.

The best way to do this is do other ArrayAdapter? or use other things?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扶醉桌前 2024-11-23 19:25:52

您需要扩展 BaseAdapter 并重写 getView() 方法。这是一个示例。

    private class CustomAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private ArrayList<Model> list;

        public CustomAdapter(Context context, ArrayList<Model> list) {
            this.inflater = LayoutInflater.from(context);
                        this.list = list;
        }

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

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

        @Override
        public long getItemId(int position) {
            return position;
        }

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

            // If the view is null inflate it from xml
            if (view == null)
                view = inflater.inflate(R.layout.list_row, null);

            // Bind xml to java
           ImageView icon = (ImageView) view
                .findViewById(R.id.image);
           TextView text = (TextView) view.findViewById(R.id.text);
                       text.setText(list.get(position).getText());
                       icon.setImageDrawable(list.get(position).getDrawable());

           return view;
       }

   }

You need to extend the BaseAdapter and override the getView() method. Here is a sample.

    private class CustomAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private ArrayList<Model> list;

        public CustomAdapter(Context context, ArrayList<Model> list) {
            this.inflater = LayoutInflater.from(context);
                        this.list = list;
        }

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

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

        @Override
        public long getItemId(int position) {
            return position;
        }

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

            // If the view is null inflate it from xml
            if (view == null)
                view = inflater.inflate(R.layout.list_row, null);

            // Bind xml to java
           ImageView icon = (ImageView) view
                .findViewById(R.id.image);
           TextView text = (TextView) view.findViewById(R.id.text);
                       text.setText(list.get(position).getText());
                       icon.setImageDrawable(list.get(position).getDrawable());

           return view;
       }

   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文