2013-03-12 53 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