我想在SimpleAdapter
使用ListView
时添加一些自定义操作。所以我写了一个MySimpleAdapter
来扩展SimpleAdapter
。如果图片网址为空,我需要隐藏ImageView
。但是,当视图回收工作时,具有可见ImageView
的视图在向上和向下滚动时也会隐藏。
代码如下:
public class MySimpleAdapter extends SimpleAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
vi=super.getView(position, convertView, parent);//other parts rather than the ImageView will behave normally
@SuppressWarnings("unchecked")
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
//below is the problematic part
if(data.get("url")!=null){
setImageWithUrl(data.get("url").toString(), (ImageView) vi.findViewById(R.id.my_image));//setImageWithUrl is a function containing my extra operations
}else{
((ImageView) vi.findViewById(R.id.my_image)).setVisibility(View.GONE);//hide the image as it has a border but no content
}
//in contrast, setting image drawable is always right
if(data.get("url2")!=null){
setImageWithUrl(data.get("url2").toString(), (ImageView) vi.findViewById(R.id.my_image_2));
}else{
((ImageView)vi.findViewById(R.id.my_image_2)).setImageDrawable(mContext.getResources().getDrawable(R.drawable.default_image));
}
}
}
问题就像(例如屏幕显示ListView
的 3 行:
[1=====] <a row with visible image
[2=====] <a row with visible image
[3=====] <a row with visible image
--------- below is out of screen
[4=====] <a row with hidden image
向下滚动到 4th 行,以便 1th 不在屏幕上。然后滚动回 1th 并且 4th 不在屏幕上。然后 1th 是隐藏图像@id/my_image
的行。我认为这是由于&#34; findViewById
&#34;。但是为什么用可绘制资源设置图像总是正确的(对于@id/my_image_2
)?
我对inflater和回收机制的细节不太熟悉。如何在此代码中避免这种情况?或者我需要使用其他适配器吗?
PS,
行xml结构很简单,如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/my_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@layout/border_style" />
<ImageView
android:id="@+id/my_image_2"
android:layout_width="100dp"
android:layout_height="100dp" />
...
</LinearLayout>
答案 0 :(得分:0)
ListView重用视图,因此您需要实现隐藏/恢复逻辑。将集合可见性添加到代码中可见。
$rcrdID = DB::table('dbo_studentrecord')
->join('dbo_students','dbo_studentrecord.StudentID' ,'=' ,'dbo_students.StudentID')
->lists(DB::raw('CONCAT("ID ", dbo_studentrecord.StudentID, " | " ,dbo_students.FirstName, " ", dbo_students.LastName)'),'StudentRecordID');