如何仅显示 CursorAdapter 中的特定元素
我有一个自定义 CursorAdapter,它从数据库中获取项目并将其显示在列表视图中。如果可能的话,我想仅显示基于数据库元素中的某些布尔值的某些元素。这是与我想做的类似的事情:
package itp.uts.program;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
//Adapter tests if an element is read or unread, and bolds the items that are unread
public class BoldAdapter extends CursorAdapter{
private final LayoutInflater mInflater;
public BoldAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
mInflater = LayoutInflater.from(context);
}
public BoldAdapter(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//Creates a view to display the items in
final View view = mInflater.inflate(R.layout.notes_row, parent, false);
TextView textRequestNo = (TextView) view.findViewById(R.id.text1);
TextView textMessage = (TextView) view.findViewById(R.id.text2);
StringBuilder requestNo = new StringBuilder(cursor.getString(cursor
.getColumnIndex("requestNo")));
StringBuilder message = new StringBuilder(cursor.getString(cursor
.getColumnIndex("Message")));
//Sets the text fields as elements from the database
textRequestNo.setText(requestNo);
textMessage.setText(message);
if (cursor.getString(cursor.getColumnIndex("Read")).equals("true"))
{//Tests if the item is unread
**//DO NOT SHOW THE ELEMENT, HOW DO I DO THIS?**
}
return view;
}
}
I have a custom CursorAdapter that is taking items from a database and displaying them in a listview. If possible, I would like to display only certain elements based on some boolean value within a database element. Here is something similar to what I would like to do:
package itp.uts.program;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
//Adapter tests if an element is read or unread, and bolds the items that are unread
public class BoldAdapter extends CursorAdapter{
private final LayoutInflater mInflater;
public BoldAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
mInflater = LayoutInflater.from(context);
}
public BoldAdapter(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//Creates a view to display the items in
final View view = mInflater.inflate(R.layout.notes_row, parent, false);
TextView textRequestNo = (TextView) view.findViewById(R.id.text1);
TextView textMessage = (TextView) view.findViewById(R.id.text2);
StringBuilder requestNo = new StringBuilder(cursor.getString(cursor
.getColumnIndex("requestNo")));
StringBuilder message = new StringBuilder(cursor.getString(cursor
.getColumnIndex("Message")));
//Sets the text fields as elements from the database
textRequestNo.setText(requestNo);
textMessage.setText(message);
if (cursor.getString(cursor.getColumnIndex("Read")).equals("true"))
{//Tests if the item is unread
**//DO NOT SHOW THE ELEMENT, HOW DO I DO THIS?**
}
return view;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理想情况下,您只需过滤掉数据库查询中不需要的行即可。
如果出于某种原因这是不可能的,请创建一个包装适配器来过滤掉您不需要的行。这是一个
AdapterWrapper
基类,您可以使用它来提供帮助。Ideally, you just filter out the rows you don't want in your database query.
If, for whatever reason, that is not possible, create a wrapping adapter that filters out the rows you do not want. Here is an
AdapterWrapper
base class you can use to help.只需返回一个视图并将其可见性属性设置为 VIEW.GONE 即可
just return a View and set it's visibility property to
VIEW.GONE