如何仅显示 CursorAdapter 中的特定元素

发布于 2024-09-11 11:39:09 字数 1867 浏览 0 评论 0原文

我有一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2024-09-18 11:39:09

理想情况下,您只需过滤掉数据库查询中不需要的行即可。

如果出于某种原因这是不可能的,请创建一个包装适配器来过滤掉您不需要的行。这是一个 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.

情绪失控 2024-09-18 11:39:09

只需返回一个视图并将其可见性属性设置为 VIEW.GONE 即可

just return a View and set it's visibility property to VIEW.GONE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文