如何更改自定义CursorAdapter以在给定片段的列表视图中仅显示特定行?

时间:2018-10-06 16:57:19

标签: android android-fragments android-cursoradapter

我的应用程序中的三个片段Fragment1,Fragment2和Fragment3使用单个自定义CursorAdapter类TaskCursorAdapter在列表视图中显示单个表的内容。这是课程:

public class TaskCursorAdapter extends CursorAdapter {
    public TaskCursorAdapter(Context context, Cursor c) {
        super(context, c, 0 /* flags */);
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.list_item_task, parent, false);
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView titleTextView = (TextView) view.findViewById(R.id.task_title);
        TextView detailsTextView = (TextView) view.findViewById(R.id.task_details);
        int titleColumnIndex = cursor.getColumnIndex(TaskEntry.COLUMN_TASK_TITLE);
        int detailsColumnIndex = cursor.getColumnIndex(TaskEntry.COLUMN_TASK_DETAILS);
        String taskTitle = cursor.getString(titleColumnIndex);
        String taskDetails = cursor.getString(detailsColumnIndex);
        if (TextUtils.isEmpty(taskDetails)) {
            taskDetails = context.getString(R.string.unknown_task);
        }
        titleTextView.setText(taskTitle);
        detailsTextView.setText(taskDetails);
    }
}

该表在Contract类中指定为TaskEntry。它还具有另一个名为TaskEntry.COLUMN_TASK_STATUS =“ status”的列。可能的值为0、1或2。当前,所有项目都显示在两个片段中。但是,我要这样做,以便在Fragment1中只显示status = 0的行,在Fragment2中只显示status = 1的行,在Fragment3中只显示status = 2的行。

我在bindView方法中尝试了以下方法:

int taskStatus = Integer.parseInt(cursor.getString(cursor.getColumnIndex(TaskEntry.COLUMN_TASK_STATUS)));
if(taskStatus==0) { //code in bindView }

这导致在所有片段中仅显示状态为= 0的项目,但它代替状态为0的项目留下了一个空的放大视图。 另外,我找不到传递信息以使其特定于Fragment1的方法。

我应该如何根据状态值和片段有条件地显示行?

编辑: 有效的方法:

我没有在TaskCursorAdapter中尝试此操作,而是在onCreateLoader方法中使用了条件查询,例如每个片段中的以下内容:

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String selectionArgs[] = {"<status>"};
    String[] projection = {
            TaskEntry._ID,
            TaskEntry.COLUMN_TASK_TITLE,
            TaskEntry.COLUMN_TASK_DETAILS};
    return new CursorLoader(this.getActivity(), TaskEntry.CONTENT_URI, projection,
            TaskEntry.COLUMN_TASK_STATUS + " = ?", selectionArgs, null);
}

1 个答案:

答案 0 :(得分:-1)

尝试一下:

public class TaskCursorAdapter extends CursorAdapter {
    private int statusCode;        

    public TaskCursorAdapter(Context context, Cursor c) {
        super(context, c, 0 /* flags */);
    }

    public setStatusCode(int statusCode){
        this.statusCode = statusCode;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int currentStatusCode = Integer.parseInt(cursor.getString(cursor.getColumnIndex(TaskEntry.COLUMN_TASK_STATUS)));
        if(statusCode == currentStatusCode){
            return LayoutInflater.from(context).inflate(R.layout.list_item_task, parent, false);
         } else return null;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        if(view != null){
            TextView titleTextView = (TextView) view.findViewById(R.id.task_title);
            TextView detailsTextView = (TextView) view.findViewById(R.id.task_details);
            int titleColumnIndex = cursor.getColumnIndex(TaskEntry.COLUMN_TASK_TITLE);
            int detailsColumnIndex = cursor.getColumnIndex(TaskEntry.COLUMN_TASK_DETAILS);
            String taskTitle = cursor.getString(titleColumnIndex);
            String taskDetails = cursor.getString(detailsColumnIndex);
            if (TextUtils.isEmpty(taskDetails)) {
                taskDetails = context.getString(R.string.unknown_task);
            }
            titleTextView.setText(taskTitle);
            detailsTextView.setText(taskDetails);
        }
    }
}

,并在每个片段中分别输入状态代码:

 yourAdapter = new TaskCursorAdapter(this, yourDataCursor);
 yourAdapter.setStatusCode(YOUR_STATUS_CODE);
 yourListView.setAdapter(yourAdapter);

编辑(事实证明,我们无法从CursorAdapter#newView()返回null

因此,我想您将必须在实例化新的TaskCursorAdapter之前在每个Fragment中过滤光标,并传入过滤后的光标而不是原始光标。您可以为此使用CursorWrapper类。这个答案可能会让您有个主意:https://stackoverflow.com/a/7343721/8354184