1

我想在每一行中创建一个带有图像和文本的 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));

           }
          });
         }
4

1 回答 1

0

检查此列表视图的实现 http://www.java2s.com/Code/Android/UI/Demonstratestheusingalistviewintranscriptmode.htm

查看本教程以使用自定义列表视图项http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/

Eidt:使用这个适配器类:

  MyCustomAdapter 类扩展 BaseAdapter{
      公共静态 ArrayList text_array = new ArrayList();
      公共静态 ArrayList image_array = new ArrayList();
      公共 int getCount(){
           返回 text_array..size();
      }
      public long getItemId(int position){
           返回位置;
      }
      公共字符串getItem(int位置){
           返回空值;
      }
      public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater 膨胀 = getLayoutInflater();
        视图 v = inflate.inflate(R.layout.listview, parent, false);
        最终 ImageView 图像 = (ImageView) v.findViewById(R.id.ImageView01);

         如果(listImages.get(位置)!= null){
                     image.setImageBitmap(image_array.get(position));
                     image.setVisibility(View.VISIBLE);
          } 别的 {
                     image.setVisibility(View.GONE);
                     image.setImageBitmap(null);
                      image.setVisibility(View.VISIBLE);
      }
     返回 v;

    }
     公共无效addObject(字符串文本,位图位图){
    text_array.add(文本);
        image_array.add(位图);
        notifyDataSetChanged();
     }
}

从您的活动类调用 addObject 函数以在列表视图中添加新项目

于 2013-03-12T08:50:49.893 回答