【问题标题】:Using ArrayAdapter to create a list in AndroidAndroid中使用ArrayAdapter创建列表
【发布时间】:2018-12-08 20:04:57
【问题描述】:

我是新手,解释一下事情的工作原理会非常有用。

所以我有这门课

        static class ListAdapter extends ArrayAdapter<String>{
            List<String> elements;
            Context context;

            public ListAdapter(Context context, List<String> elements) {
                super(context, 0, elements);
                this.context = context;
                this.elements = elements;
            }


            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.list_item_ex, null, false);
                }

                TextView tvTitle = convertView.findViewById(R.id.tv_title);
                TextView tvDescription = convertView.findViewById(R.id.tv_description);
                ImageView ivExample = convertView.findViewById(R.id.iv_image);

                ivExample.setImageResource(R.drawable.offer_1);
                tvTitle.setText(elements.get(position));
                tvDescription.setText(String.valueOf(position));

                return convertView;
            }
        }

我在 MainActivity 中向列表中添加了一些元素以查看其工作原理,但我很难理解如何添加元素,每个元素都有不同的图片、描述等。

【问题讨论】:

    标签: android list adapter


    【解决方案1】:

    为此,您必须使用自定义类创建 ArrayAdapter: 以下是您必须遵循的步骤:

    1. 创建自定义类

      public class Custom {
      int image;
      String title;
      String description;
      //constructeur 
      //getters and setters }
      
    2. 创建适配器扩展 ArrayAdapter

      public class CustomArrayAdapter extends ArrayAdapter<Custom> {
      
      ArrayList<Custom> list;
      
          public CustomArrayAdapter(Context context, ArrayList<Custom> list) {
          super(context, R.layout.list_item_ex, list);this.list = list;
      }
      
      
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
      
          Custom custom=getItem(position);
      
          if (convertView == null) {
              convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_ex, parent, false);
          }
      
          TextView tvTitle = convertView.findViewById(R.id.tv_title);
          TextView tvDescription = convertView.findViewById(R.id.tv_description);
          ImageView ivExample = convertView.findViewById(R.id.iv_image);
      
          ivExample.setImageResource(custom.getImage());
          tvTitle.setText(custom.getTitle());
          tvDescription.setText(custom.getDescription());
      
          return convertView;
      }
      

      }

    3. 填充一个 ArrayList 然后 listView.setAdapter(customArrayAdapter);

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          ListView listView=(ListView) findViewById(R.id.listView) ;
          ArrayList<Custom> list=new ArrayList<>();
          list.add(new Custom(R.mipmap.ic_launcher,"title","desc"));
      
          CustomArrayAdapter customArrayAdapter=new CustomArrayAdapter(this,listView);
      
          listView.setAdapter(customArrayAdapter);}
      

    【讨论】:

    猜你喜欢
    • 2011-10-05
    • 2012-07-10
    • 2013-10-05
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多