使用ViewBinder SimpleCursorAdapter显示自定义列表 [英] Customizing list shown from SimpleCursorAdapter using ViewBinder
问题描述
我建立一个的ListView
与任的TextView
s或的ImageView
s为基础的项目是否是一个文本项目或有与之关联的图像。我用使用VewBinder <从SimpleCursorAdapter显示自定义列表/ A>和它的工作很好,到一个点。
I'm building a ListView
with either TextView
s or ImageView
s based on whether or not the item is a text item or has an image associated with it. I used Customizing list shown from SimpleCursorAdapter using VewBinder and it worked great, up to a point.
我有一个 SQLite数据库
包含都有一个标题(某些文本)项目,但不是所有的人都有一个图像/视频作为一种资源;所以一些是纯文本项而另一些图像或视频。对于图像和视频我要加载资源,但是当该项目是一个纯文本的项目,我只是想显示图像的标题。
I have an SQLite database
containing items that all have a title (some text), but not all of them have an image/video as a resource; so some are pure text items while others are images or videos. For the images and videos I want to load a resource, but when the item is a plain text item, I just want to display the title of the image.
所以,我的 XML
包含两个元素 - 一个的TextView
来显示文本项的标题,和的ImageView
显示资源文件(视频我检索视频ID YouTube的默认图像)。
So my XML
contains two elements - a TextView
to display the title of the text items, and an ImageView
to display the resource file (for videos I retrieve the YouTube default image for the video id).
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp"
android:background="@android:color/black"
>
<TextView
android:id="@+id/tv_details_title"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="TextView"
android:textColor="@android:color/white"
android:visibility="visible"
android:background="@android:color/holo_blue_bright" />
<ImageView
android:id="@+id/iv_details_resource"
android:layout_width="200dp"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_gravity="center"
android:src="@drawable/logo"
android:visibility="visible"
android:background="@android:color/holo_green_light" />
</LinearLayout>
我不使用内容提供商
,因为我并不需要其他应用永远使用我的数据库。我用的是 SimpleCursorAdapter
并写了 CustomViewBinder
(内部)类我的数据绑定到我的查看
s为基础对是否有相关的项目资源。
I'm not using a Content Provider
because I don't need other apps to ever use my database. I use a SimpleCursorAdapter
and have written a CustomViewBinder
(inner) class to bind my data to my view
s based on whether or not there is a resource associated to the item.
问题是,的ImageView
的可见性总是被设置为 GONE
不管是什么。因此,显示每个项目的标题,因为这始终是绑定到的TextView
元素,但是当没有我的数据库中列出的资源(例如,空是在在资源
字段)的记录,那么的ImageView
也消失了。
The problem is that the visibility of the ImageView
is always set to GONE
no matter what. So the title of each item is displayed, because that is always bound to the TextView
element, but when there is no resource listed in my database (i.e. "null" is in the record in the resource
field), then the ImageView
also disappears.
下面是code为我的 CustomViewBinder
:
Here is the code for my CustomViewBinder
:
private class CustomViewBinder implements ViewBinder{
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if(columnIndex == cursor.getColumnIndex(DatabaseHelper.FIELD_RESOURCE)){
Log.d("CustomViewBinder", "columnIndex = " + columnIndex);
//If the column is resource, ten we use custom view.
String resource = cursor.getString(columnIndex);
Log.d("CustomViewBinder", "resource = " + resource);
if(resource.equalsIgnoreCase("null")){
Log.d("CustomViewBinder", "Inside if resource = " + resource);
Log.d("CustomViewBinder", "Set the image view to GONE");
view.setVisibility(View.GONE);
}else{
Log.d("CustomViewBinder", "Inside else resource = " + resource);
columnIndex = cursor.getColumnIndex(DatabaseHelper.FIELD_TITLE);
Log.d("CustomViewBinder", "columnIndex = " + columnIndex);
//If the column is resource, ten we use custom view.
String title = cursor.getString(columnIndex);
Log.d("CustomViewBinder", "title = " + title);
if(!resource.equalsIgnoreCase("null")){
Log.d("CustomViewBinder", "Set the title view to GONE");
view.setVisibility(View.GONE);
}
return true;
}
return true;
}
return false;
}
然后一些日志净度:
And then some logs for clarity:
07-26 17:05:36.343: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:36.343: D/CustomViewBinder(9735): resource = null
07-26 17:05:36.343: D/CustomViewBinder(9735): Inside if resource = null
07-26 17:05:36.343: D/CustomViewBinder(9735): Set the image view to GONE
07-26 17:05:36.353: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:36.353: D/CustomViewBinder(9735): resource = null
07-26 17:05:36.353: D/CustomViewBinder(9735): Inside if resource = null
07-26 17:05:36.353: D/CustomViewBinder(9735): Set the image view to GONE
07-26 17:05:36.363: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:36.363: D/CustomViewBinder(9735): resource = null
07-26 17:05:36.363: D/CustomViewBinder(9735): Inside if resource = null
07-26 17:05:36.363: D/CustomViewBinder(9735): Set the image view to GONE
07-26 17:05:53.770: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:53.770: D/CustomViewBinder(9735): resource = Notes_Box_2_Notebook_1_006.jpg
07-26 17:05:53.770: D/CustomViewBinder(9735): Inside else resource = Notes_Box_2_Notebook_1_006.jpg
07-26 17:05:53.770: D/CustomViewBinder(9735): columnIndex = 2
07-26 17:05:53.770: D/CustomViewBinder(9735): title = Notebook page - man and wife
07-26 17:05:53.770: D/CustomViewBinder(9735): Set the title view to GONE
07-26 17:05:54.310: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:54.310: D/CustomViewBinder(9735): resource = null
07-26 17:05:54.310: D/CustomViewBinder(9735): Inside if resource = null
07-26 17:05:54.310: D/CustomViewBinder(9735): Set the image view to GONE
问题是,在设置标题视图GONE
的图像视图
仍然设置到 GONE
这样的形象 Notes_Box_2_Notebook_1_006.jpg
不显示 - 这是标题
The problem is that at "Set the title view to GONE
" the "image view
" is still set to GONE
so the image "Notes_Box_2_Notebook_1_006.jpg
" is not displayed - it's title is.
这是becase的了视图
的 CustomViewBinder
似乎永远是 ImageView的
在我的 XML
,而不是以往任何时候都在的TextView
。
This is becase the "view
" of the CustomViewBinder
seems to always be the ImageView
in my XML
rather than ever being the TextView
.
如何访问另一个视图
在 CustomViewBinder
?
How do I access "another" view
in the CustomViewBinder
?
推荐答案
我用的 ViewHolder
格局。
I used the ViewHolder
pattern.
我通过具有光标
扩展类来实现它。在我的主要活动我从中查询数据库我的数据库辅助类获得光标。然后我联系我的 CustomCursorAdapter
到水平列表视图
持有三个项目 - 一个的TextView
和两个的ImageView
秒。然后,我有一个 OnItemClickListener
设置为我的水平列表视图
。然后,可以过滤的东西,但我只是在链接的更新主视图
我上面的水平列表视图
(很像教程)。
I implemented it by having a Cursor
extended class. In my main activity I get the cursor from my database helper class which queries the database. Then I link my CustomCursorAdapter
to a horizontal listview
which holds three items - a TextView
and two ImageView
s. I then have an OnItemClickListener
set to my horizontal listview
. You can then filter on something, but I just updated a main view
above my horizontal listview
(much like in the linked tutorial).
在我的 CustomCursorAdapter
的 getView
函数定义了一个 ViewHolder
,将光标移动到的位置(从getView的参数),和T母鸡做什么是在 getView
函数的几乎每一个例子来做的,所以在这里它是:
In my CustomCursorAdapter
's getView
function I define a ViewHolder
, move the cursor to the position (from getView's parameters), andt hen do what is done in almost every example of the getView
function, so here it is:
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
cursor.moveToPosition(position);
if(convertView == null){
convertView = inflater.inflate(R.layout.activity_media_items, null);
holder = new ViewHolder();
holder.textview = (TextView) convertView.findViewById(R.id.tv_details_title);
holder.imageview = (ImageView) convertView.findViewById(R.id.iv_details_resource_image);
holder.imageviewvideo = (ImageView) convertView.findViewById(R.id.iv_details_resource_video);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//////////////////////////TYPE
type = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_ITEM_TYPE));
itemId = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_MEDIA_ITEM_ID));
if(type.equalsIgnoreCase("text")){
//////////////////////////TEXT
String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_TRANSCRIPTION));
title = title.subSequence(0, 150) + "...";
holder.textview.setText(title);
holder.textview.setVisibility(View.VISIBLE);
holder.imageview.setVisibility(View.GONE);
holder.imageviewvideo.setVisibility(View.GONE);
}
if(type.equalsIgnoreCase("image")){
//////////////////////////IMAGE
String imageUri = "assets://";
Log.d(TAG, "URI = " + imageUri + fileName);
ImageLoader.getInstance().displayImage(imageUri+fileName, holder.imageview);
holder.textview.setVisibility(View.GONE);
holder.imageview.setVisibility(View.VISIBLE);
holder.imageviewvideo.setVisibility(View.GONE);
}
if(type.equalsIgnoreCase("video")){
//////////////////////////VIDEO
//get the file name
String fileName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_RESOURCE));
//get the image of a youtube video because it's a video
try {
Log.d(TAG,"Want to show video at -> http://img.youtube.com/vi/"+fileName+"/mqdefault.jpg");
ImageLoader.getInstance().displayImage("http://img.youtube.com/vi/"+fileName+"/mqdefault.jpg", holder.imageviewvideo);
} catch(Exception e) {
Log.d("YouTube photo", "Failed to load photo!!! "+e.toString());
}
holder.textview.setVisibility(View.GONE);
holder.imageview.setVisibility(View.GONE);
holder.imageviewvideo.setVisibility(View.VISIBLE);
}
return convertView;
}
这篇关于使用ViewBinder SimpleCursorAdapter显示自定义列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!