SetAdapter不起作用

时间:2016-08-29 12:47:04

标签: java android sqlite android-sqlite

我有一些问题,如果,我真的很感激 你可以帮帮我 问题:我想从SQLite数据库中获取数据并将其显示在中 无论哪种方式,都可以使用Listview或Gridview。 我看了一个教程,试图遵循规则和背后的想法, 复制源代码并逐个更改代码 自己的目的。奇怪的是,代码我在本教程中遇到了问题 代码,也在我的项目的另一个类中,但拒绝在 最近的文件..

所以,这是工作文件的oncreate:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_member_list);

    mem_op = new Member_Operations(this);
    mem_op.open();

    List values = mem_op.getAllMembers();

    et = (EditText) findViewById(R.id.et1);
    et2 = (EditText) findViewById(R.id.et2);
    gv = (GridView) findViewById(R.id.gv);

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

    gv.setAdapter(adapter);


} // oncreate

这是相关的超类:

public class Member_Operations {

public DBHelper_Members db_helper;
private SQLiteDatabase database;
private String [] MEMBER_TABLE_COLUMNS = { db_helper.MEMBERS_COLUMN_ID,db_helper.MEMBERS_COLUMN_NAME,db_helper.MEMBERS_COLUMN_PERMISSION};

public Member_Operations(Context context)
{
    db_helper = new DBHelper_Members(context);
}

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

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

public Member addMember(String name, String permission){

    ContentValues contentValues = new ContentValues();

    contentValues.put(db_helper.MEMBERS_COLUMN_NAME, name);
    contentValues.put(db_helper.MEMBERS_COLUMN_PERMISSION,permission);

    long MemID =  database.insert(db_helper.MEMBER_TABLE, null, contentValues);

    Cursor cursor = database.query(db_helper.MEMBER_TABLE,
            MEMBER_TABLE_COLUMNS,db_helper.MEMBERS_COLUMN_ID + " = " +
                    + MemID, null,null,null,null);

    cursor.moveToFirst();

    Member newComment = parseMember(cursor);
    cursor.close();

    return newComment;

}


public void deleteMember(Member mem){

    long id = mem.getID();
    SQLiteDatabase db = db_helper.getWritableDatabase();
    db.delete(db_helper.MEMBER_TABLE, db_helper.MEMBERS_COLUMN_ID + " = " + id,
           null);

}


public List getAllMembers(){

    List members = new ArrayList();

    Cursor cursor = database.query(db_helper.MEMBER_TABLE,
            MEMBER_TABLE_COLUMNS,null,null,null,null,null);

    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        Member member = parseMember(cursor);
        members.add(member);
        cursor.moveToNext();
    }

    cursor.close();
    return members;

}

private Member parseMember(Cursor cursor){
    Member member = new Member();
    member.setID(cursor.getInt(0));
    member.setName(cursor.getString(1));
    member.setPermission(cursor.getString(2));
    return member;
}

这是一个拒绝工作的人:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    kal_op = new Kalender_Operations(this);
    kal_op.open();

    List values = kal_op.getAllDays();


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

//在这里,我尝试添加类型(日期),与第一个不同。 //但无论哪种方式都无法发挥作用。

   GridView gv = (GridView) findViewById(R.id.gv);


    ListActivity la = new ListActivity();
    la.setListAdapter(adapter);


}

它的超级班:

public class Kalender_Operations {

SQLiteDatabase database;
DBHelper db_helper;
String [] DATES_TABLE_COLUMNS = { db_helper.JANUARY_COLUMN_ID,
db_helper.JANUARY_COLUMN_DAY,db_helper.JANUARY_COLUMN_EVENT};

public Kalender_Operations(Context context)
{
    db_helper = new DBHelper(context);
}

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

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

public Date addDate (String day, String event){

    ContentValues contentValues = new ContentValues();

    contentValues.put(db_helper.JANUARY_COLUMN_DAY, day);
    contentValues.put(db_helper.JANUARY_COLUMN_EVENT, event);

    long DateID = database.insert(db_helper.JANUARY_TABLE,null,contentValues);

    Cursor cursor = database.query(db_helper.JANUARY_TABLE,
            DATES_TABLE_COLUMNS, db_helper.JANUARY_COLUMN_ID
                    + " = " + DateID,null,null,null,null);
    cursor.moveToFirst();

    Date newComment = parseDate(cursor);
    cursor.close();
    return newComment;


}

public void showDetails(int i, Context context){

    Intent intent = new Intent(context, Test_Intent.class);
    intent.putExtra("Position", i);
    intent.putExtra("Month", db_helper.JANUARY_TABLE);
    intent.putExtra("Year", db_helper.JANUARY_YEAR);
    context.startActivity(intent);
}

public List getAllDays(){

    List Dates = new ArrayList();

        Cursor cursor = database.query(db_helper.JANUARY_TABLE,
                DATES_TABLE_COLUMNS, null, null, null, null, null);


        cursor.moveToFirst();

    while(!cursor.isAfterLast()){
        Date date = parseDate(cursor);
        Dates.add(date);
        cursor.moveToNext();
    }
    cursor.close();



    return Dates;

}
     public Date parseDate(Cursor cursor){
    Date date = new Date();
        date.setID(cursor.getInt(0));
        date.setDay(cursor.getString(1));
        date.setEvent(cursor.getString(2));


    return date;

}
}

请帮帮我。我真的想继续学习,但我现在花了很多时间试图弄清楚为什么一个有效,另一个没有...

0 个答案:

没有答案