将SQLite数据插入ListView

时间:2016-08-19 15:01:30

标签: android listview android-sqlite

我试图从我的SQLite数据库中获取一些数据到ListView,但它不会出现在那里。连接到SQLite工作正常,我用一些字符串测试它们,然后出现。所以我假设我在创建数据时犯了一个错误。

这是我的 OverviewActivity     公共类OverviewActivity扩展ListActivity实现View.OnClickListener {

private DataSource dataSource;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.overview);

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

    String[] values2 = {"1", "2", "3"};
    List<ValuesGetSet> values = dataSource.getAllValues();

    ArrayAdapter<ValuesGetSet> adapter = new ArrayAdapter<ValuesGetSet>(this, android.R.layout.simple_list_item_1, values);

    //When i change values to values 2 it works perfectly


    setListAdapter(adapter);
}
@Override
public void onClick(View v) {
    ArrayAdapter<ValuesGetSet> adapter = (ArrayAdapter<ValuesGetSet>) getListAdapter();
    ValuesGetSet valuesGetSet = null;
    switch (v.getId()){
        case R.id.add:
            valuesGetSet = dataSource.createValues(4, "Testname", "Testversion", "Testmethod", 200, false);
            adapter.add(valuesGetSet);
            Log.d("LOG_TAG", "" + dataSource.getAllValues());
            break;
        case R.id.remove:
            if (getListAdapter().getCount() > 0){
                valuesGetSet = (ValuesGetSet) getListAdapter().getItem(0);
                dataSource.deleteValues(valuesGetSet);
                adapter.remove(valuesGetSet);
            }
            break;
    }
    adapter.notifyDataSetChanged();
}

@Override
public void onResume(){
    dataSource.open();
    super.onResume();
}

@Override
public void onPause(){
    dataSource.close();
    super.onPause();
}
}

这是我的DataSource类:

public class DataSource {

private SQLiteDatabase database;
private FeedReaderDBHelper dbHelper;
private String [] allColumns = {
        FeedReaderDBHelper.COLUMN_NAME_ID, FeedReaderDBHelper.COLUMN_NAME_NUMBER,
        FeedReaderDBHelper.COLUMN_NAME_NAME, FeedReaderDBHelper.COLUMN_NAME_VERSION,
        FeedReaderDBHelper.COLUMN_NAME_METHOD, FeedReaderDBHelper.COLUMN_NAME_CHAIN_NUMBER,
        FeedReaderDBHelper.COLUMN_NAME_CAUGHT};

public DataSource(Context context){
    dbHelper = new FeedReaderDBHelper(context);
}

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

public void close(){
    dbHelper.close();
}

public ValuesGetSet createValues(int number, String name, String version, String method, int chainNumber, boolean caught){
    ContentValues values = new ContentValues();
    values.put(FeedReaderDBHelper.COLUMN_NAME_NUMBER, number);
    values.put(FeedReaderDBHelper.COLUMN_NAME_NAME, name);
    values.put(FeedReaderDBHelper.COLUMN_NAME_VERSION, version);
    values.put(FeedReaderDBHelper.COLUMN_NAME_METHOD, method);
    values.put(FeedReaderDBHelper.COLUMN_NAME_CHAIN_NUMBER, chainNumber);
    values.put(FeedReaderDBHelper.COLUMN_NAME_CAUGHT, caught);

    long insertID = database.insert(FeedReaderDBHelper.TABLE_NAME, null, values);

    Cursor cursor = database.query(
            FeedReaderDBHelper.TABLE_NAME,
            allColumns,
            FeedReaderDBHelper.COLUMN_NAME_ID + " = " + insertID,
            null, null, null, null);

    cursor.moveToFirst();
    ValuesGetSet newValuesGetSet = cursorToValues(cursor);
    cursor.close();
    return newValuesGetSet;

}

public void deleteValues(ValuesGetSet valuesGetSet){
    long id = valuesGetSet.getId();
    System.out.println("Values deleted with id: " + id);
    database.delete(
            FeedReaderDBHelper.TABLE_NAME,
            FeedReaderDBHelper.COLUMN_NAME_ID + " = " + id,
            null);
}

public List<ValuesGetSet> getAllValues(){
    List<ValuesGetSet> valuesGetSets = new ArrayList<ValuesGetSet>();

    Cursor cursor = database.query(FeedReaderDBHelper.TABLE_NAME, allColumns, null, null, null, null, null);
    cursor.moveToFirst();

    while (!cursor.isAfterLast()){
        ValuesGetSet valuesGetSet = cursorToValues(cursor);
        valuesGetSets.add(valuesGetSet);
        cursor.moveToNext();
    }

    cursor.close();
    return valuesGetSets;
}

private ValuesGetSet cursorToValues(Cursor cursor){
    ValuesGetSet valuesGetSet = new ValuesGetSet();
    valuesGetSet.setId(cursor.getLong(0));
    valuesGetSet.setNumber(cursor.getInt(1));
    return valuesGetSet;
}
}

这是我的ValuesGetSet类

public class ValuesGetSet {

private int number;
private int chainNumber;
private long id;
private String name;
private String version;
private String method;
private boolean caught;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}

public int getChainNumber() {
    return chainNumber;
}

public void setChainNumber(int chainNumber) {
    this.chainNumber = chainNumber;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getVersion() {
    return version;
}

public void setVersion(String version) {
    this.version = version;
}

public String getMethod() {
    return method;
}

public void setMethod(String method) {
    this.method = method;
}

public boolean isCaught() {
    return caught;
}

public void setCaught(boolean caught) {
    this.caught = caught;
}

@Override
public String toString(){
    return String.valueOf(this);
}
}

我假设我的错误在最后一部分 - public String to String() - 但我不知道如何解决这个问题。

我对android编程很新,所以任何帮助都会受到重视。

1 个答案:

答案 0 :(得分:0)

像这样修改ValuesGetSet类的toString()。

@Override
public String toString() {
    return "ValuesGetSet{" +
            "number=" + number +
            ", chainNumber=" + chainNumber +
            ", id=" + id +
            ", name='" + name + '\'' +
            ", version='" + version + '\'' +
            ", method='" + method + '\'' +
            ", caught=" + caught +
            '}';
}