android changeCursor()没有生效

时间:2017-04-12 01:48:31

标签: android cursor

我正在学习android编程,我正在编写一个小的SQL列表应用程序,以便从DB中获取“注释”并使用自定义CursorAdapter将它们放在列表中。这一切似乎都是功能性但是添加/删除列表不会重新填充(删除似乎根本不起作用)。我正在调用changeCursor()但它看起来并不像我的列表更新。重新运行后,我可以看到上一次运行的结果。任何帮助是极大的赞赏。 这是我的代码:

CommentsDataSource.java:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class CommentsDataSource {

private MySQLiteHelper SQLhelper;
private SQLiteDatabase database;

public CommentsDataSource (Context context) {
    // Create a link to database:
    SQLhelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
    database = SQLhelper.getWritableDatabase();
}
public void close () {
    database.close();
}

public Comment createComment (String comment) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment);

    long id = database.insert(MySQLiteHelper.TABLE_COMMENTS, null, values);
    Cursor cursor = database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_COMMENTS + " WHERE " + MySQLiteHelper.COLUMN_ID + " = " + id, null);

    cursor.moveToFirst();
    Comment newComment  = new Comment();
    newComment.setId(cursor.getLong(0));
    newComment.setComment(cursor.getString(1));

    cursor.close();
    return newComment;
}

public void deleteComment (Comment comment) {
    long id = comment.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
            + " = " + id, null);

}

public List<Comment> getAllComments () {
    List<Comment> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_COMMENTS, null);
    cursor.moveToFirst();

    Comment newComment  = new Comment();

    while (! cursor.isAfterLast()) {
        newComment.setId(cursor.getLong(0));
        newComment.setComment(cursor.getString(1));
        list.add(newComment);
        cursor.moveToNext();
    }

    cursor.close();
    return list;
}

public Cursor getCursor() {
    List<Comment> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_COMMENTS, null);
    return cursor;
}
}

MainActivity:

public class MainActivity extends ListActivity {

private CommentsDataSource dataSource;
private Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.context= this;

    dataSource = new CommentsDataSource(this);
    dataSource.open();

    List<Comment> values = dataSource.getAllComments();

    /*ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_expandable_list_item_1, values);
    setListAdapter(adapter);*/

    Cursor cursor = dataSource.getCursor();
    ListView lvItems = (ListView) findViewById(android.R.id.list);

    CustomCursorAdapter adapter = new CustomCursorAdapter(this, cursor);
    lvItems.setAdapter(adapter);


}
public void onClick(View view) {
    @SuppressWarnings("unchecked")
    Cursor cursor = dataSource.getCursor();

    CustomCursorAdapter adapter = new CustomCursorAdapter(context, cursor);
    Comment comment = null;
    switch (view.getId()) {
        case R.id.add:
            String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
            int nextInt = new Random().nextInt(3);
            // save the new comment to the database
            comment = dataSource.createComment(comments[nextInt]);
            break;
        case R.id.delete:
            if (adapter.getCount() > 0) {
                Cursor d = (Cursor) adapter.getItem(0);
                comment = new Comment();
                comment.setComment(d.getColumnName(1));

                dataSource.deleteComment(comment);
            }
            break;
    }

    Cursor newcursor = dataSource.getCursor();
    adapter.changeCursor(newcursor);

}

}

customCursorAdapter:

public class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView type = (TextView) view.findViewById(R.id.setType_textView);
    TextView name = (TextView) view.findViewById(R.id.exerciseName_textView);

    name.setText(cursor.getString(1));
}
}

0 个答案:

没有答案