从适配器刷新列表视图

时间:2016-12-30 14:21:08

标签: android listview android-adapter

我有CheckBox的2个列表。当用户选中CheckBox中的list_1时,列表项将转移到list_2。它工作正常,但我无法从适配器刷新列表。尝试notifyDataSetChanged()但没有任何反应。

在我的MainActivity

CustomListAdapter adapter = new CustomListAdapter(this, db.getAllTasks(MySQLiteHelper.TABLE_HOME, false), MySQLiteHelper.TABLE_HOME,false);
mylistView.setAdapter(adapter);    // Set the adapter for list_1

CustomListAdapter adapterDone = new CustomListAdapter(this, db.getAllTasks(MySQLiteHelper.TABLE_HOME, true),  MySQLiteHelper.TABLE_HOME,true);
listViewDone.setAdapter(adapterDone);    // Set the adapter for list_2

getAllTasks函数:

public List<Task> getAllTasks(String tableName, boolean done) {
    List<Task> tasks = new LinkedList<Task>();

    String op = done ? "1" : "0";
    String query = "SELECT  * FROM " + tableName + " WHERE done=" + op;
    Log.d(TAG, "query: " + query);
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    Task task = null;

    if (cursor.moveToFirst()) {
        do {
            task = new Task();
            task.setTaskText(cursor.getString(0));
            task.setDate(cursor.getString(1));
            task.setTime(cursor.getString(2));
            task.setDone(cursor.getInt(3) == 1);
            tasks.add(task);
        } while (cursor.moveToNext());
    }

    return tasks;
}

这是我的Adapter

public class CustomListAdapter extends ArrayAdapter<Task> {
    private static final String TAG = "DTAG";
    private final Activity context;
    private final List<Task> myTasks;
    private final MySQLiteHelper db;
    private final String tableName;
    private final boolean done;

    public CustomListAdapter(Activity context, List<Task> myTasks, String tableName, boolean done) {
        super(context, R.layout.list_row, myTasks);

        this.context = context;
        this.myTasks = myTasks;
        this.tableName = tableName;
        this.done = done;
        db = new MySQLiteHelper(context);
    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_row, null, true);

        final TextView txtTitle = (TextView) rowView.findViewById(R.id.listMainTextView);
        TextView txtSubTitle = (TextView) rowView.findViewById(R.id.listSubTextView);

        CheckBox ch = (CheckBox) rowView.findViewById(R.id.checkBox);
        ch.setChecked(done);

        ch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Log.d(TAG, "position: " + position + " Name: " + myTasks.get(position).getTaskText() + " Checked");
                    db.updateTask(myTasks.get(position).getTaskText(), tableName, true);
                } else {
                    Log.d(TAG, "position: " + position + " Name: " + myTasks.get(position).getTaskText() + " UnChecked");
                    db.updateTask(myTasks.get(position).getTaskText(), tableName, false);
                }

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        notifyDataSetChanged();
                    }
                }, 1000);
            }
        });

        txtTitle.setText(myTasks.get(position).getTaskText());
        txtSubTitle.setText(myTasks.get(position).getTime() + " " + myTasks.get(position).getDate());

        return rowView;
    };
}

2 个答案:

答案 0 :(得分:1)

更新数据库后,必须再次调用get task以获取新的任务列表,然后调用notifydatasetchange。
我在这种情况下经常使用cursoradapter,在更改后,重新查询我的光标并调用changecursor。
希望这能帮助你。

答案 1 :(得分:1)

Task中创建两个单独的MainActivity列表,如下所示。

List<Task> tasksPendingList = new LinkedList<Task>();
List<Task> tasksDoneList = new LinkedList<Task>();

现在,在初始化getAllTasks之前调用Adapter方法来完成任务。

tasksPendingList = db.getAllTasks(MySQLiteHelper.TABLE_HOME, false);
tasksDoneList = db.getAllTasks(MySQLiteHelper.TABLE_HOME, true);

现在像这样初始化适配器

CustomListAdapter adapter = new CustomListAdapter(this, tasksPendingList, MySQLiteHelper.TABLE_HOME, false);
mylistView.setAdapter(adapter);    // Set the adapter for list_1

CustomListAdapter adapterDone = new CustomListAdapter(this, tasksDoneList,  MySQLiteHelper.TABLE_HOME, true);
listViewDone.setAdapter(adapterDone);    // Set the adapter for list_2

现在,在您getView的{​​{1}}方法中,您需要重新填充列表以使Adapter生效。

notifyDatasetChanged

您可以考虑在 ch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { db.updateTask(myTasks.get(position).getTaskText(), tableName, true); } else { db.updateTask(myTasks.get(position).getTaskText(), tableName, false); } // Just call refreshBothList here refreshBothList(); } }); 中使用getView方法之外的列表刷新机制。

MainActivity

您可以考虑将public void refreshBothList() { // Repopulate the lists. tasksPendingList = db.getAllTasks(MySQLiteHelper.TABLE_HOME, false); tasksDoneList = db.getAllTasks(MySQLiteHelper.TABLE_HOME, true); adapter.notifyDataSetChanged(); adapterDone.notifyDataSetChanged(); } 作为内部类放在CustomListAdapter