我看不到ListView

时间:2017-04-09 20:08:05

标签: java android sqlite listview android-sqlite

空ListView的原因是什么?实际上,我已经用日期填充了数据库,但ListView是空的,尤其是活动为空。使用arrayAdapter创建ListView时,活动为空,但是当使用游标适配器创建时,应用程序会崩溃。

我写了一个应用程序,它使用ListView列出我的SQLite日期的一部分 但我看不到ListView。应用程序启动时没有错误,但活动为空。我看不出我的错误。我希望,其他人可以看到错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context="com.example.katjarummler.hundeschule_petra_bennemann.
GruppeAuslesenActivity"
android:focusableInTouchMode="true"
android:weightSum="1">

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:visibility="visible"
    />

 </LinearLayout>
 public class GruppeAuslesenActivity extends AppCompatActivity {


public static final String LOG_TAG = 
GruppeAuslesenActivity.class.getSimpleName();
ListView mKundenListView;
private HundeschuleMemoDataSource dataSource = new 
HundeschuleMemoDataSource(this);

private GoogleApiClient client;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gruppeauslesen);

   initializeHundeschuleKundenListView();

    HundeschuleMemoDataSource dataSource = new 
  HundeschuleMemoDataSource(this);


    client = new 
 GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

}

private void initializeHundeschuleKundenListView() {
   List<HundeschuleMemoKunden> listForInitialization = new 
ArrayList<HundeschuleMemoKunden>();

     mKundenListView = (ListView) findViewById(R.id.list);

    // Erstellen des ArrayAdapters für unseren ListView
    ArrayAdapter<HundeschuleMemoKunden> 
 hundeschuleMemoKundenArrayAdapter = new 
 ArrayAdapter<HundeschuleMemoKunden>(
            this,
            android.R.layout.simple_list_item_multiple_choice,
            listForInitialization) {

        // Wird immer dann aufgerufen, wenn der übergeordnete ListView 
 die Zeile neu zeichnen muss

        @NonNull
        public View getView(int position, View convertView, @NonNull 
 ViewGroup parent) {

            View view = super.getView(position, convertView, parent);
            TextView textView = (TextView) view;

            HundeschuleMemoKunden memo = (HundeschuleMemoKunden) 
 mKundenListView.getItemAtPosition(position);


            // Hier prüfen, ob Eintrag abgehakt ist. Falls ja, Text 
 durchstreichen
            if (memo.isChecked()) {
                textView.setPaintFlags(textView.getPaintFlags() | 
 Paint.STRIKE_THRU_TEXT_FLAG);
                textView.setTextColor(Color.rgb(175, 175, 175));
            } else {
                textView.setPaintFlags(textView.getPaintFlags() & 
 (~Paint.STRIKE_THRU_TEXT_FLAG));
                textView.setTextColor(Color.DKGRAY);
            }


            return view;
        }
    };

    mKundenListView.setAdapter(hundeschuleMemoKundenArrayAdapter);

    mKundenListView.setOnItemClickListener(new 
 AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapterView, View view, 
 int position, long id) {
            HundeschuleMemoKunden memo = (HundeschuleMemoKunden) 
 mKundenListView.getItemAtPosition(position);


           // Hier den checked-Wert des Memo-Objekts umkehren, bspw. 
 von true auf false
            // Dann ListView neu zeichnen mit showAllListEntries()
           HundeschuleMemoKunden updatedHundeschuleMemoKunden = 
 dataSource.updateHundeschuleMemoKunden(memo.getId(), memo.getgName(), 
 memo.getkName(),
                    memo.getkTelefon(), memo.gethName(), 
 memo.getbeginn(), (!memo.isChecked()));
            Log.d(LOG_TAG, "Checked-Status von Eintrag: " + 
 updatedHundeschuleMemoKunden.toString() + " ist: " + 
 updatedHundeschuleMemoKunden.isChecked());

            showAllListEntries();
        }
    });



}


private void showAllListEntries() {

    List<HundeschuleMemoKunden> hundeschuleMemoGruppenList = 
dataSource.getAllGruppen();


    ArrayAdapter<HundeschuleMemoKunden> adapter = 
(ArrayAdapter<HundeschuleMemoKunden>) mKundenListView.getAdapter();

    adapter.clear();
    adapter.addAll(hundeschuleMemoGruppenList);
    adapter.notifyDataSetChanged();
}


protected void onResume() {
    super.onResume();

    Log.d(LOG_TAG, "Die Datenquelle wird geöffnet.");
    dataSource.open();

    Log.d(LOG_TAG, "Folgende Einträge sind in der Datenbank 
  enthalten:");
    showAllListEntries();
  }

 protected void onPause() {
    super.onPause();

    Log.d(LOG_TAG, "Die Datenquelle wird geschlossen.");
    dataSource.close();
 }


public Action getIndexApiAction() {
    Thing object = new Thing.Builder()
            .setName("GruppeAuslesen Page") // TODO: Define a title for 
the content shown.
            // TODO: Make sure this auto-generated URL is correct.
            .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
            .build();
    return new Action.Builder(Action.TYPE_VIEW)
            .setObject(object)
            .setActionStatus(Action.STATUS_TYPE_COMPLETED)
            .build();
}

@Override
public void onStart() {
    super.onStart();

    client.connect();
    AppIndex.AppIndexApi.start(client, getIndexApiAction());
}

@Override
public void onStop() {
    super.onStop();


    AppIndex.AppIndexApi.end(client, getIndexApiAction());
    client.disconnect();
}
}
public class HundeschuleMemoDataSource {

private static final String LOG_TAG = 
HundeschuleMemoDataSource.class.getSimpleName();

private SQLiteDatabase database;
private HundeschuleMemoDBHelper dbHelper;

private String[] columns = {

        HundeschuleMemoDBHelper.COLUMN_ID,
        HundeschuleMemoDBHelper.COLUMN_KName,
        HundeschuleMemoDBHelper.COLUMN_GNAME,
        HundeschuleMemoDBHelper.COLUMN_KTELEFON,
        HundeschuleMemoDBHelper.COLUMN_HUND,
        HundeschuleMemoDBHelper.COLUMN_BEGINN,
        HundeschuleMemoDBHelper.COLUMN_CHECKED,
};


public HundeschuleMemoDataSource(Context context) {
    Log.d(LOG_TAG, "Unsere DataSource erzeugt jetzt den dbHelper. ");
    dbHelper = new HundeschuleMemoDBHelper(context, "gruppe", 
TABLE_KUNDEN_LIST);
}


public void open() {
    Log.d(LOG_TAG, "Eine Referenz auf die Datenbank wird jetzt 
angefragt.");
    database = dbHelper.getWritableDatabase();
    Log.d(LOG_TAG, "Datenbankreferenz erhalten. Pfad zur Datenbank: " + 
database.getPath());
}

public void close() {
    dbHelper.close();
    Log.d(LOG_TAG, "Datenbank mit Hilfe des DBHelpers geschlossen.");
}


public HundeschuleMemoKunden createHundeschuleMemoKunden(String kName, 
String gName, String kTelefon, String hName,
                                                         String beginn) 
{
    ContentValues values = new ContentValues();
    values.put(COLUMN_KName, kName);
    values.put(COLUMN_GNAME, gName);
    values.put(HundeschuleMemoDBHelper.COLUMN_KTELEFON, kTelefon);
    values.put(COLUMN_HUND, hName);
    values.put(HundeschuleMemoDBHelper.COLUMN_BEGINN, beginn);

    long insertId = database.insert(TABLE_KUNDEN_LIST, null, values);

    Cursor cursor = database.query(TABLE_KUNDEN_LIST,
            columns, HundeschuleMemoDBHelper.COLUMN_ID + "=" + 
   insertId,
            null, null, null, null);

    cursor.moveToFirst();
    HundeschuleMemoKunden hundeschuleMemoKunden = 
    cursorToHundeschuleMemoKunden(cursor);
    cursor.close();

    return hundeschuleMemoKunden;
}

public HundeschuleMemoKunden createHundeschuleMemoGruppen(String gName, 
String kName, String hName) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_GNAME, gName);
    values.put(COLUMN_KName, kName);
    values.put(COLUMN_HUND, hName);


    long insertId = database.insert(TABLE_KUNDEN_LIST, null, values);

    Cursor cursor = database.query(TABLE_KUNDEN_LIST,
            columns, HundeschuleMemoDBHelper.COLUMN_ID + "=" + 
 insertId,
            null, null, null, null);

    cursor.moveToFirst();
    HundeschuleMemoKunden hundeschuleMemoGruppen = 
  cursorToHundeschuleMemoKunden(cursor);
    cursor.close();

    return hundeschuleMemoGruppen;
}


public void deleteHundeschuleMemoKunden(HundeschuleMemoKunden 
hundeschuleMemoKunden) {
    long id = hundeschuleMemoKunden.getId();

    database.delete(TABLE_KUNDEN_LIST,
            HundeschuleMemoDBHelper.COLUMN_ID + "=" + id,
            null);

    Log.d(LOG_TAG, "Eintrag gelöscht! ID: " + id + " Inhalt: " + 
 hundeschuleMemoKunden.toString());
 }




public HundeschuleMemoKunden updateHundeschuleMemoKunden(long id, 
  String newkName, String newgName, String newkTelefon, String newHund,
                                                         String 
  newBeginn, boolean newChecked) {
    int intValueChecked = (newChecked) ? 1 : 0;
    ContentValues values = new ContentValues();
    values.put(COLUMN_KName, newkName);
    values.put(COLUMN_GNAME, newgName);
    values.put(HundeschuleMemoDBHelper.COLUMN_KTELEFON, newkTelefon);
    values.put(COLUMN_HUND, newHund);
    values.put(HundeschuleMemoDBHelper.COLUMN_BEGINN, newBeginn);
    values.put(HundeschuleMemoDBHelper.COLUMN_CHECKED, 
  intValueChecked);

    database.update(TABLE_KUNDEN_LIST,
            values,
            HundeschuleMemoDBHelper.COLUMN_ID + "=" + id,
            null);

    Cursor cursor = database.query(TABLE_KUNDEN_LIST,
            columns, HundeschuleMemoDBHelper.COLUMN_ID + "=" + id,
            null, null, null, null);

    cursor.moveToFirst();
    HundeschuleMemoKunden hundeschuleMemoKunden = 
  cursorToHundeschuleMemoKunden(cursor);
    cursor.close();

    return hundeschuleMemoKunden;
   }


  private HundeschuleMemoKunden cursorToHundeschuleMemoKunden(Cursor 
  cursor) {

    int idIndex = 
  cursor.getColumnIndex(HundeschuleMemoDBHelper.COLUMN_ID);
    int idKName = cursor.getColumnIndex(COLUMN_KName);
    int idGName = cursor.getColumnIndex(COLUMN_GNAME);
    int idKTelefon = 
 cursor.getColumnIndex(HundeschuleMemoDBHelper.COLUMN_KTELEFON);
    int idHund = cursor.getColumnIndex(COLUMN_HUND);
    int idBeginn = 
 cursor.getColumnIndex(HundeschuleMemoDBHelper.COLUMN_BEGINN);
    int idChecked = 
 cursor.getColumnIndex(HundeschuleMemoDBHelper.COLUMN_CHECKED);


    String kName = cursor.getString(idKName);
    String gName = cursor.getString(idGName);
    String kTelefon = cursor.getString(idKTelefon);
    String Hund = cursor.getString(idHund);
    String beginn = cursor.getString(idBeginn);
    long id = cursor.getLong(idIndex);
    int intValueChecked = cursor.getInt(idChecked);

    boolean isChecked = (intValueChecked != 0);


    HundeschuleMemoKunden hundeschuleMemoKunden = new 
   HundeschuleMemoKunden(kName, gName, kTelefon, Hund,
            beginn);

    return hundeschuleMemoKunden;

}



public List<HundeschuleMemoKunden> getAllHundeschuleMemos() {
    List<HundeschuleMemoKunden> hundeschuleMemoKundenList = new 
ArrayList<>();

    Cursor cursor = database.query(TABLE_KUNDEN_LIST,
            columns, null, null, null, null, null);

    cursor.moveToFirst();


    HundeschuleMemoKunden hundeschuleMemoKunden;

    while (!cursor.isAfterLast()) {
        hundeschuleMemoKunden = cursorToHundeschuleMemoKunden(cursor);
        hundeschuleMemoKundenList.add(hundeschuleMemoKunden);
        Log.d(LOG_TAG, "ID: " + hundeschuleMemoKunden.getId() + ", 
   Inhalt: " + hundeschuleMemoKunden.toString());
        cursor.moveToNext();
    }

    cursor.close();

    return hundeschuleMemoKundenList;
  }

 public List<HundeschuleMemoKunden> getAllGruppen() {
    List<HundeschuleMemoKunden> gruppenList = new ArrayList<>();
    SQLiteDatabase database = dbHelper.getWritableDatabase();


    String selectQuery = "SELECT " +
            HundeschuleMemoDBHelper.COLUMN_ID + " , " +
            HundeschuleMemoDBHelper.COLUMN_GNAME + " , " +
            HundeschuleMemoDBHelper.COLUMN_KName + " , " +
            HundeschuleMemoDBHelper.COLUMN_HUND + " , " +
            HundeschuleMemoDBHelper.COLUMN_CHECKED +
            " FROM " + HundeschuleMemoDBHelper.TABLE_KUNDEN_LIST + " 
  ORDER BY 2 ASC ";


    Cursor cursor = database.rawQuery(selectQuery, null);


    cursor.moveToFirst();
    cursor.close();
    database.close();
     return gruppenList;
    }

  }
 public class HundeschuleMemoDBHelper extends SQLiteOpenHelper {


private static final String LOG_TAG = 
HundeschuleMemoDBHelper.class.getSimpleName();



public static final String DB_NAME = "Kunden_list.db";
public static final int DB_VERSION = 7;

public static final String TABLE_KUNDEN_LIST = "Kunden_list";


public  static final String COLUMN_ID = "_id";
public  static final String COLUMN_KName = "Name";
public  static final String COLUMN_GNAME = "Gruppe";
public  static final String COLUMN_KTELEFON = "Telefon";
public  static final String COLUMN_HUND = "Hund";
public  static final String COLUMN_BEGINN = "Beginn";
public  static final String COLUMN_CHECKED = "checked";



public  static final String SQL_CREATE = "CREATE TABLE " + 
TABLE_KUNDEN_LIST +
        "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_KName + " TEXT NOT NULL, " +
        COLUMN_GNAME + " TEXT NOT NULL, " +
        COLUMN_KTELEFON + " TEXT NOT NULL, " +
        COLUMN_HUND + " TEXT NOT NULL, " +
        COLUMN_BEGINN + " TEXT NOT NULL, " +
        COLUMN_CHECKED + " BOOLEAN NOT NULL DEFAULT 0);";


public  static final String SQL_DROP = "DROP TABLE IF EXISTS " + 
TABLE_KUNDEN_LIST;



public  HundeschuleMemoDBHelper(Context context, String gruppe, String 
sql) {
    super(context, DB_NAME, null, DB_VERSION);
    Log.d(LOG_TAG, "DBHelper hat die Datenbank: " + getDatabaseName() + 
" erzeugt.");
}



//Die onCreate-Methode wird nur aufgerufen, falls die Datenbank noch 
 nicht existiert
public void onCreate(SQLiteDatabase db) {
    try{
        Log.d(LOG_TAG, "Die Tabelle wird mit SQL-Befehl: " + SQL_CREATE 
 + " angelegt.");
        db.execSQL(SQL_CREATE);


    }
    catch(Exception ex){
        Log.e(LOG_TAG, "Fehler beim Anlegen der Tabelle: " + 
 ex.getMessage());
    }

}


// Die onUpgrade-Methode wird aufgerufen, sobald die neue 
Versionsnummer höher
// als die alte Versionsnummer ist und somit ein Upgrade notwendig wird

public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
newVersion) {
    Log.d(LOG_TAG, "Die Tabelle mit Versionsnummer " + oldVersion + " 
wird entfernt.");
    db.execSQL(SQL_DROP);


    onCreate(db);
  }

}
public class HundeschuleMemoKunden {

private String kName;
private String gName;
private String kTelefon;
private String hName;
private String beginn;
private long id;
private boolean checked;


public HundeschuleMemoKunden(String kName, String gName, String 
kTelefon, String hName,
                             String beginn){
    this.id=id;
    this.kName=kName;
    this.gName=gName;
    this.kTelefon=kTelefon;
    this.hName=hName;
    this.beginn=beginn;
    this.checked = checked;
}



public String getkName(){
    return kName;
}
public void setkName(String kName){
    this.kName = kName;
}
public String getgName(){return gName;}
public void setgName(String gName){
    this.gName = gName;
}
public String getkTelefon(){
    return kTelefon;
}
public void setkTelefon(String kTelefon){this.kTelefon = kTelefon;}
public String gethName(){
    return hName;
}
public void sethName(String hName){
    this.hName = hName;
}
public String getbeginn(){
    return beginn;
}
public void setbeginn(String beginn){
    this.beginn = beginn;
}
public long getId(){return id;}
public void setId(long id){
    this.id = id;
}
public boolean isChecked(){return checked;}
public void setChecked(boolean checked){this.checked = checked;}





  public String toString(){
    String output = kName + " " + gName + " " + kTelefon + " " + hName 
 + " " + beginn;
    return output;
  }



 }

1 个答案:

答案 0 :(得分:0)

 android:layout_height="wrap_content"

您需要匹配父

如果您正在使用数据库,请尝试使用CursorAdapter而不是ArrayAdapter并确保dataSource.getAllGruppen()实际返回某些内容

此外,您有两个HundeschuleMemoDataSource dataSource个实例。由于Context尚未初始化,因此从字段中删除声明

private HundeschuleMemoDataSource dataSource;

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

    dataSource = new  HundeschuleMemoDataSource(this);

    initializeHundeschuleKundenListView();

如果您确实使用了CursorAdapter,那么这是您更正的方法

public Cursor getAllGruppen() {

   String selectQuery = "SELECT " +
        HundeschuleMemoDBHelper.COLUMN_ID + " , " +
        HundeschuleMemoDBHelper.COLUMN_GNAME + " , " +
        HundeschuleMemoDBHelper.COLUMN_KName + " , " +
        HundeschuleMemoDBHelper.COLUMN_HUND + " , " +
        HundeschuleMemoDBHelper.COLUMN_CHECKED +
        " FROM " + HundeschuleMemoDBHelper.TABLE_KUNDEN_LIST +  
        " ORDER BY 2 ASC ";

    return dbHelper.getReadableDatabase().rawQuery(selectQuery, null);
}