java - 动态改变ListView大小并加载图像

标签 java android

我想创建一个ListView,每行都包含图像和文本,动态更改其大小(例如,在开始时listView将不显示任何内容,然后,我可以向listView添加条目),我也希望listView 将可以加载位图图像列表,而不是可绘制图像。

我创建了这段代码,但是该代码仅从可绘制对象加载图像并创建一次(意味着我无法动态更改列表 - 添加或删除 listView 条目)

String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
       "Eight", "Nine", "Ten" };

     int[] image = { R.drawable.logo, R.drawable.logo, R.drawable.logo,
       R.drawable.logo, R.drawable.logo, R.drawable.logo, R.drawable.logo,
       R.drawable.logo, R.drawable.logo, R.drawable.logo };

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
 lv.setAdapter(new MyCustomAdapter(text, listImages));
          edittext= (EditText) findViewById(R.id.EditText01);

          edittext.addTextChangedListener(new TextWatcher()
          {

           public void afterTextChanged(Editable s)
           {

           }

           public void beforeTextChanged(CharSequence s, int start,
            int count, int after)
           {

           }

           public void onTextChanged(CharSequence s, int start,
            int before, int count)
           {

            textlength = edittext.getText().length();
            text_sort.clear();
            image_sort.clear();

            for (int i = 0; i < text.length; i++)
            {
             if (textlength <= text[i].length())
             {
              if (edittext.getText().toString().
           equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
              {
               text_sort.add(text[i]);
              // image_sort.add(image[i]);
              }
             }
            }

            lv.setAdapter(new MyCustomAdapter
             (text_sort, image_sort));

           }
          });
         }

最佳答案

检查 listview 的实现 http://www.java2s.com/Code/Android/UI/Demonstratestheusingalistviewintranscriptmode.htm

查看本教程以了解如何使用自定义 ListView 项 http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/

编辑: 使用此适配器类:

  class MyCustomAdapter extends BaseAdapter{
      public static  ArrayList text_array = new ArrayList();
      public static  ArrayList image_array = new ArrayList();
      public int getCount(){
           return text_array..size();
      }
      public long getItemId(int position){
           return position;
      }
      public String getItem(int position){
           return null;
      }
      public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflate = getLayoutInflater();
        View v = inflate.inflate(R.layout.listview, parent, false);
        final ImageView image = (ImageView) v.findViewById(R.id.ImageView01);

         if(listImages.get(position) != null) {
                     image.setImageBitmap(image_array.get(position));
                     image.setVisibility(View.VISIBLE);
          } else {
                     image.setVisibility(View.GONE);
                     image.setImageBitmap(null);
                      image.setVisibility(View.VISIBLE);
      }
     return v;

    }
     public void addObject(String text, Bitmap bitmap) {
    text_array.add(text);
        image_array.add(bitmap);
        notifyDataSetChanged();
     } 
}

从 Activity 类中调用 addObject 函数以在 ListView 中添加新项目

关于java - 动态改变ListView大小并加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356099/

相关文章:

java - 在 Java 中,如何让不同行中的两个类相互交互?

java - 如何获取网站内容并将其以我们想要的方式放入 android 应用程序布局中?

java - ConcurrentHashMap 操作是线程安全的

java - 帮助基本的 Android View ,用于 Next/Prev 导航,如电子邮件设置

android - 从移动浏览器分析页面加载时间的好方法

java - 访问 ListView 项目字符串的最简单方法是什么?

java - 为什么启用 native 加密 (ASO) 后,Oracle 中的每个错误 SQL 请求都会出现 "Checksum fail"?

android - ViewPager PagerObserver 未注册

android - 在 Android 中导出时转换为 Dalvik 格式失败并出现错误 1 ​​(adt 21)

android - 从 RemoteViews 获取 TextView 使其文本可滚动