2010-07-07 87 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