android - 尝试对我的任务列表使用自定义 CursorAdapter,但遇到问题

标签 android android-cursoradapter custom-cursor

我正在制作一个简单的任务列表应用程序,并且使用数据库来保存所有内容。我编写了一个自定义 CursorAdapter,扩展了 ResourceCursorAdapter 来处理数据。一切都显示得很好,但一旦你开始超越这个范围,事情就会变得粗略。首先,我的实现:

import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Dave Smith
 *
 */
public class TaskCursorAdapter extends ResourceCursorAdapter {

    /**
     * @param context
     * @param layout
     * @param c
     * @param from
     * @param to
     * @param flags
     */
    private Context context;
    private int layout, isCheckedFromDb, IdFromDb;
    private Cursor cursor;

    public TaskCursorAdapter(Context context, int layout, Cursor c,
            int flags) {
        super(context, layout, c, flags);
        this.context = context;
        this.layout = layout;
    }
    public TaskCursorAdapter(Context context, int layout){
        super(context, layout, null, 0);
        this.context = context;
        this.layout = layout;
    }
    public TaskCursorAdapter(Context context, int layout, Cursor c){
        super(context, layout, c, 0);
        this.context = context;
        this.layout = layout;
    }
    public TaskCursorAdapter(Context context, int layout, Cursor cursor, boolean autoRequery)
    {
        super(context, layout, cursor, autoRequery);
        this.context = context;
        this.layout = layout;
    }
    public View newView(Context context, Cursor cursor, ViewGroup parent){
        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);
        return v;
    }
    public void bindView(View v, Context context, Cursor c){
        CheckBox taskCheckBox = (CheckBox)v.findViewById(R.id.taskCheckBox);
        TextView taskText = (TextView)v.findViewById(R.id.taskTitle);
        TextView taskNote = (TextView)v.findViewById(R.id.taskNote);
        final long assignedID = v.getID();
        //v.setClickable(true);     
        //LinearLayout holder = (LinearLayout)v.findViewById(R.id.container);
        //holder.setClickable(true);
        //IdFromDb = c.getInt(c.getColumnIndex(NagTasksDatabaseHelper.ID));
        taskText.setText(c.getString(c.getColumnIndex(NagTasksDatabaseHelper.TASK)));
        taskNote.setText(c.getString(c.getColumnIndex(NagTasksDatabaseHelper.NOTE)));
        //isCheckedFromDb = c.getInt(c.getColumnIndex(NagTasksDatabaseHelper.CHECKED));
        taskCheckBox.setChecked(c.getInt(c.getColumnIndex(NagTasksDatabaseHelper.CHECKED))==1);
        //cursor = c;
        taskCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //Really Old method:
                /*NagTasksDatabaseHelper helper = new NagTasksDatabaseHelper(null);
                int idCol = cursor.getColumnIndex(NagTasksDatabaseHelper.ID); 
                int taskID = helper.getNewTaskId();
                if (idCol!=-1)
                {
                    taskID= cursor.getInt(idCol);
                    if (isChecked){
                        helper.checkOffTask(taskID);
                    } else {
                        helper.uncheckTask(taskID);
                    }
                }
                //TODO Solve how to use helper to change ISCHECKED in database
                helper.close();
                //Old method:
                NagTasksDatabaseHelper helper = new NagTasksDatabaseHelper(null);
                if (isChecked) {
                    helper.checkOffTask(IdFromDb);
                } else {
                    helper.uncheckTask(IdFromDb);
                }
                helper.close();*/
                //Current method:

                if (assignedID!=View.NO_ID) {
                    NagTasksDatabaseHelper helper = new NagTasksDatabaseHelper(buttonView.getContext());
                    if (isChecked) {
                        helper.checkOffTask(assignedID);
                    } else {
                        helper.uncheckTask(assignedID);
                    }
                    helper.close();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(buttonView.getContext());
                    builder.setMessage("No ID found. Try something else.");
                    builder.setCancelable(true);
                    builder.create().show();
                }
            } 
        });
    }

}

问题 1

编辑:问题已解决,但我不知道如何解决。

在当前状态下,当我点击运行时,Eclipse 控制台中出现错误。它根本不会影响它的运行效果,但在以前的实现中,我使用了只有一个复选框的布局,并且只更改了复选框的文本,没有出现此错误:

[2012-06-18 16:10:15 - ddms] null
java.lang.NullPointerException
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
    at com.android.ddmlib.Client.sendAndConsume(Client.java:575)
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:672)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

[2012-06-18 16:10:15 - ddms] null
java.lang.NullPointerException
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
    at com.android.ddmlib.Client.sendAndConsume(Client.java:575)
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:672)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

问题 2

使用此 CursorAdapter 的 ListFragment 未执行其 OnListItemClick() 方法。同样,我已经注册了 ListFragment 的 ListView 以使用 ContextMenu(使用 registerForContextMenu()),但长按不会调出上下文菜单。

问题 3

取消注释taskCheckBox.setOnCheckedChangeListener通常会导致在选中复选框时发生不好的事情。通常,它会抛出空指针异常并崩溃,因为它不知道针对什么 _id 调用 checkOffTask()uncheckTask(). (编辑:最近编辑中所做的更改...仍然没有达到我想要的效果,但至少它不会崩溃。也烦人的是 getItemId( ) 请求一个位置,但我不知道如何从光标中检索 ListView 中给定项目的位置,否则我会使用它。)


谁能帮我解决这些问题吗?尽管 asking for help on this subject before 我仍然不太清楚 CursorAdapters 是如何工作的.

编辑:我应该注意到,我的应用程序有另一个列表,该列表使用了经过轻微调整的ResourceCursorAdapter(请参阅链接的问题),并且这些问题都没有出现在那里。 (尽管问题 3 没有理由弹出,因为它只是一个简单的字符串列表。)

最佳答案

问题 1:

将跳过,因为它已解决。

问题 2:

this previous question 中所述,如果 View 有可聚焦对象,则由于 SDK 中的错误,将无法使整个 View 可点击。因此,我进入了膨胀的 XML 并向每个项目添加了属性 android:focusable="false"。列表项现在可点击且可长点击。

问题 3:

正如 a nice fellow on reddit 所指出的,我使用“旧方法”是正确的,但需要使用本地 final long 而不是参数。

关于android - 尝试对我的任务列表使用自定义 CursorAdapter,但遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11091382/

相关文章:

javascript - 使用 JavaScript 更改鼠标指针

javascript - 使用 JavaScript 更改鼠标指针

android - 查看可见性状态 GONE 占用屏幕空间

javascript - Android 从 Java 调用 Javascript 不工作

android - 为什么我的应用程序在启动照片 Intent 之后但在调用 onActivityResult() 之前崩溃?

android - NullPointerException 在 Activity 中获取 ActionBar

android - 简单游标适配器需要最低 api 11 问题

Android RecyclerView + CursorLoader + ContentProvider + "Load More"

html - 自定义光标不起作用?

java - ListView 第一项的值错误