适用于多个项目的ListView适配器

时间:2017-12-11 14:36:41

标签: android listview

我正在尝试为多个项目制作自定义适配器。它与一个项目完美搭配,simple_list_item_1布局,但它不会 在我制作自己的适配器时填满数据库。

这是我的数据库助手类: -

public class BazaPodataka extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "korisnici.db";
public static final String TABLE_NAME = "Korisnici";
public static final String COL1 = "ID";
public static final String COL2 = "IME";
public static final String COL3 = "PREZIME";
public static final String COL4 = "BROJTELEFONA";
public static final String COL5 = "KRECEMDO";
public static final String COL6 = "BROJSJEDISTA";
public static final String COL7 = "OPIS";



public BazaPodataka(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " IME TEXT, PREZIME TEXT, BROJTELEFONA TEXT, KRECEMDO TEXT, BROJSJEDISTA TEXT, OPIS TEXT)";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean addData( String ime, String prezime, String brojTelefona, String krecemDo, String brojSjedista, String opis) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, ime);
    contentValues.put(COL3, prezime);
    contentValues.put(COL4, brojTelefona);
    contentValues.put(COL5, krecemDo);
    contentValues.put(COL6, brojSjedista);
    contentValues.put(COL7, opis);

    long result = db.insert(TABLE_NAME, null, contentValues);

    //if date as inserted incorrectly it will return -1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

//query for 1 week repeats
public Cursor getListContents() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return data;
   }
 }

我的数据库项目的模型类是常规的,我不认为它有错误。

这是我输入项目的表单类:

public class BazaPodataka extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "korisnici.db";
public static final String TABLE_NAME = "Korisnici";
public static final String COL1 = "ID";
public static final String COL2 = "IME";
public static final String COL3 = "PREZIME";
public static final String COL4 = "BROJTELEFONA";
public static final String COL5 = "KRECEMDO";
public static final String COL6 = "BROJSJEDISTA";
public static final String COL7 = "OPIS";



public BazaPodataka(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " IME TEXT, PREZIME TEXT, BROJTELEFONA TEXT, KRECEMDO TEXT, BROJSJEDISTA TEXT, OPIS TEXT)";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean addData( String ime, String prezime, String brojTelefona, String krecemDo, String brojSjedista, String opis) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, ime);
    contentValues.put(COL3, prezime);
    contentValues.put(COL4, brojTelefona);
    contentValues.put(COL5, krecemDo);
    contentValues.put(COL6, brojSjedista);
    contentValues.put(COL7, opis);

    long result = db.insert(TABLE_NAME, null, contentValues);

    //if date as inserted incorrectly it will return -1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

//query for 1 week repeats
public Cursor getListContents() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return data;
  }
}

"告示板"应该输入所有这些的类:

public class BazaPodataka extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "korisnici.db";
public static final String TABLE_NAME = "Korisnici";
public static final String COL1 = "ID";
public static final String COL2 = "IME";
public static final String COL3 = "PREZIME";
public static final String COL4 = "BROJTELEFONA";
public static final String COL5 = "KRECEMDO";
public static final String COL6 = "BROJSJEDISTA";
public static final String COL7 = "OPIS";



public BazaPodataka(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " IME TEXT, PREZIME TEXT, BROJTELEFONA TEXT, KRECEMDO TEXT, BROJSJEDISTA TEXT, OPIS TEXT)";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean addData( String ime, String prezime, String brojTelefona, String krecemDo, String brojSjedista, String opis) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, ime);
    contentValues.put(COL3, prezime);
    contentValues.put(COL4, brojTelefona);
    contentValues.put(COL5, krecemDo);
    contentValues.put(COL6, brojSjedista);
    contentValues.put(COL7, opis);

    long result = db.insert(TABLE_NAME, null, contentValues);

    //if date as inserted incorrectly it will return -1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

//query for 1 week repeats
public Cursor getListContents() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return data;
    }
   }

问题是,当我启动应用程序并尝试按下主屏幕上的按钮时,它会崩溃 这是logcat: -

    E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.win.povezime, PID: 3588
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.win.povezime/com.example.win.povezime.oglasna_od_UDG}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                  at com.example.win.povezime.oglasna_od_UDG.onCreate(oglasna_od_UDG.java:46)
                  at android.app.Activity.performCreate(Activity.java:6975)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6541) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

1 个答案:

答案 0 :(得分:0)

解决。

我只需要修改我的代码以防止NullPointerException: -

listView1 = (ListView) findViewById(R.id.listView1);

            listView1.setAdapter(adapter1);

就像这样: -

listView1 = (ListView) findViewById(R.id.listView1);
        if (listView1 != null) {
            listView1.setAdapter(adapter1);
        }