sqlite游标设置错误的列

时间:2015-11-19 05:37:15

标签: android

为什么使用这个游标:

Objects object = new Objects(cursor.getString(0),cursor.getString(1),
                cursor.getString(2),cursor.getString(3));

我有这样的结果:

Objects{id=1, name='http://www.escmobile.com/projects/android/okhttp/saul.jpg', url='null', type='null'

但是选择的结果是这样的

TYPE=IMAGES NAME=Image1 URL=http://www.escmobile.com/projects/android/okhttp/saul.jpg

这样我插入数据

void addObjects(Objects object) {
    SQLiteDatabase db = this.getWritableDatabase();
    onCreate(db);
    ContentValues values = new ContentValues();

    //values.put(OBJECT_ID,object.getId() ); // OBJECT Name
    values.put(OBJECT_NAME, object.getName()); // OBJECT Name
    values.put(OBJECT_URL, object.getUrl()); // OBJECT URL
    values.put(OBJECT_TYPE, object.getType()); // Contact type
    System.out.println(values);

    // Inserting Row
    long id = db.insert(TABLE_OBJECTS, null, values);
    System.out.println( "id:"+id);
    db.insert(TABLE_OBJECTS, null, values);
    db.close(); // Closing database connection
}

//来自MAIn活动

DatabaseHandler db = new DatabaseHandler(getApplicationContext());
Log.d("Insert: ", "Inserting ..");
db.addObjects(new Objects("Image1", "http://www.escmobile.com/projects/android/okhttp/saul.jpg", "IMAGES", "Leb Funny"));
db.addObjects(new Objects("Image1", "http://www.escmobile.com/projects/android/okhttp/saul.jpg","IMAGES","Leb Funny"));
db.addObjects(new Objects("Image1", "http://www.escmobile.com/projects/android/okhttp/saul.jpg", "IMAGES", "Leb Funny"));


List<Objects> Objects = db.getAllObjects();
Log.d("hehe","tttttt");
System.out.println(Objects);
DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects);

这是Object类

    public Objects( String name, String url,String type,String category) {
        //this.id=id;
        this.name = name;
        this.url = url;
        this.type = type;
        this.category =category;
    }
    public Objects()
    {

    }
    public int getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public String getUrl()
    {
        return url;
    }
    public String getType()
    {
        return type;
    }

    public String getCategory()
    {
        return category;
    }
    public void setId(int id)
    {
        this.id=id;
    }

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

    public void setcategory(String category)
    {
        this.category=category;
    }

}

2 个答案:

答案 0 :(得分:2)

您为类型

添加了Setter方法
public void setType(String type)
{
    this.type =type ;
}

答案 1 :(得分:1)

检查Objects class

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

不应该

public void setUrl(String url){
    this.url=url;
}