2013-03-12 83 views
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)); 

      } 
      }); 
     } 

回答

0

检查这个实现的ListView 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: 使用此适配器类:

 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(); 
    } 
} 

通话ADDOBJECT功能从您的活动类的列表视图中添加新项

+0

您好,感谢的答案,但是这个环节没有帮助我,没有任何的添加或删除从ListView的项目如何动态地例子,如何将位图图像列表加载到列表视图...任何进一步的帮助?感谢 – 2013-03-12 09:02:05

+1

它和Android示例中的示例可以看到它导致ApiDemo应用程序,它是用于动态列表视图。对于在列表视图中使用图像,您必须扩展BaseAdapter/ArrayAdapter类为您的列表视图 – 2013-03-12 09:06:42

+0

我更新了我的代码(getView func),我有一个问题(postlogcat也是) – 2013-03-12 10:00:31