2010-07-07 90 views
2

爲我的公司做一些R & D.我們試圖讓listView包含一個imageview,並且爲listview中的每個條目設置兩個編輯框。將viewGroup添加到ListView?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:isScrollContainer="true" 
    android:focusableInTouchMode="false" 
    android:focusable="false"> 
    <ImageView android:id="@+id/imgView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:maxWidth="100dp" 
    android:maxHeight="100dp"/> 
    <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <EditText android:id="@+id/img_title" 
    android:hint="Title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minWidth="150dp"/> 
    <EditText android:id="@+id/img_desc" 
    android:hint="Description" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minWidth="150dp"/> 
    </LinearLayout> 
</LinearLayout> 

這是我嘗試用於列表視圖中的項目的佈局文件。在ImageAdapter的getView中(它擴展了ArrayAdapter), 我試圖使用LayoutInflater來擴充xml,然後將其存儲到ViewGroup中。我找到ViewByID()來獲取xml中的imageView,並設置我們想要的imageView的屬性。

如果我們繼續膨脹這個xml文件,所有的id都是一樣的。這是我們的問題。

  1. 如果我們刪除列表項與 上下文菜單,它實際上消除 不正確的一個。測試顯示 它主要是最後添加的, 但並非總是如此。
  2. EditText不響應鍵盤輸入。有時,他們存儲一些數據,通常總是「bbb」。

我們有更多的問題,但是我們會在解決這些更嚴重的錯誤後回覆。

public View getView(int position, View convertView, ViewGroup parent) 
    { 
     final ImageView imageView; 
     //InputStream is = null; 
     final Context mContext = super.getContext(); 
     final AdapterItem item = super.getItem(position); 
     final Uri imageUri = item.getUri(); 
     ViewGroup viewGroup = null; 

     try 
     { 
      //-- If the view has not been created yet. 
      if (convertView == null) 
      { 
       /* 
       * Build the ImageView from the URI with some custom 
       * view settings. 
       */ 

       viewGroup = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.mediauploadobject, null); 
       imageView = (ImageView) viewGroup.findViewById(R.id.imgView); 
       //imageView.setLayoutParams(new GridView.LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT)); 
       imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
       imageView.setPadding(IMAGE_PADDING_LEFT, IMAGE_PADDING_TOP, 
         IMAGE_PADDING_RIGHT, IMAGE_PADDING_BOTTOM); 
       imageView.setDrawingCacheEnabled(true); 
       imageView.setClickable(true); 
       imageView.setFocusable(true); 
       imageView.setFocusableInTouchMode(true); 
       imageView.setSaveEnabled(false); 
       //the following two lines are required for the menu to popUp 
       imageView.setOnLongClickListener(new LongClickListener()); 
       imageView.setOnCreateContextMenuListener(new LongClickMenu()); 
       imageView.setOnClickListener(new ShortClickListener()); 

       //the following two lines are required to put a boarder around the images 
       imageView.setOnTouchListener(new PictureOnTouchListener()); 
       imageView.setOnFocusChangeListener(new PictureOnFocusChangeListener()); 

       //-- Keep a reference to the ImageView by tagging it. 
       imageView.setTag(imageUri); 
      }else 
      { 
       //-- R-E-C-Y-C-L-E recycle! 
       viewGroup = (ViewGroup) convertView; 
       imageView = (ImageView) viewGroup.findViewById(R.id.imgView); 
      } 

      //-- Lazy load the images so the user doesn't have to wait for all of the querying non-sense 
      // that happens behind the scenes. 
      imageView.setImageResource(android.R.drawable.gallery_thumb); 
      imageView.post(new ImageLoader(imageUri, imageView));   

      //-- Be VERY careful when changing this code. Due to heap size issues, 
      // the size of the bitmap image MUST be modified with the 
      // provided BitmapFactory.Options or the program will 
      // crash often and frequent. 
      //post 
      //-- AJG 7/1/2010 added this assignment so we aren't always setting these preferences every 
      // iteration 
      convertView = viewGroup; 
     }catch(Throwable t) 
     { 
      Log.e(TAG, t.toString()); 
      return null;    
     }finally 
     { 
      //try{if(is != null)is.close();}catch(Exception squish){} 
     } 

     return viewGroup; 
    } 
+0

你可以發佈適配器的代碼嗎? – 2010-07-07 14:20:35

+0

如果問題在於刪除項目,則用於刪除項目的代碼可能有助於獲得答案。不應該因爲ID是相同的,因爲列表中的每個項目在列表中都有一個唯一的位置(這是您將用於移除特定項目的位置)。 – kiswa 2010-07-07 14:20:39

+0

我修復了刪除錯誤。適配器代碼約爲450行。我會假設你更願意看到代碼的getView部分。用代碼編輯我的原始帖子。 – DavidAndroidDev 2010-07-07 14:51:59

回答

2

默認情況下,子視圖在列表視圖中不可聚焦。這是爲了防止奇怪的軌跡球/非觸摸導航行爲。這可能是爲什麼你的編輯文本沒有響應輸入。確保你調用了ListView.setItemsCanFocus(true)方法。

+1

雖然這不是我的問題的解決方案,但這是我通常在其他地方找到的答案。謝謝我決定改爲將實例化自己的視圖組作爲「假」列表視圖中的一行,只需將視圖組添加到線性佈局即可。 – DavidAndroidDev 2010-07-09 17:20:12