0

私はsqliteデータベースでリストビューを作成しようとしています。これまでのところ、データベースにアイテムを正しく作成/追加したと思います。私はそれらのアイテムを手に入れてリストビューに入れる方法がわかりません。ここでlistviewにsqlite dbを埋め込む

は私のデータベース作成(RecordsDatabase.java)である:私はボタンのクリック(MyMaxes.java)のボタンを追加する場所

public class RecordsDatabase extends SQLiteOpenHelper { 

public static final int DATABASE_VERSION = 1; 

public RecordsDatabase(Context context) { 
    super(context, RecordContract.RecordInfo.DATABASE_NAME, null, DATABASE_VERSION); 
    Log.d("Database operations", "Database created"); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE " + RecordContract.RecordInfo.TABLE_NAME + " (" 
        + RecordContract.RecordInfo.RECORDS_DATE + " TEXT NOT NULL," 
      + RecordContract.RecordInfo.RECORDS_SQUAT + " TEXT NOT NULL," 
      + RecordContract.RecordInfo.RECORDS_BENCH + " TEXT NOT NULL," 
      + RecordContract.RecordInfo.RECORDS_DEAD + " TEXT NOT NULL," 
      + RecordContract.RecordInfo.RECORDS_TOTAL + " TEXT NOT NULL)" 

    ); 
    Log.d("Database operations", "Table created"); 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

} 

public void putInformation(RecordsDatabase rdb, String date, String squat, String bench, String dead, String total) { 
    SQLiteDatabase SQ = rdb.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(RecordContract.RecordInfo.RECORDS_DATE, date); 
    contentValues.put(RecordContract.RecordInfo.RECORDS_SQUAT, squat); 
    contentValues.put(RecordContract.RecordInfo.RECORDS_BENCH, bench); 
    contentValues.put(RecordContract.RecordInfo.RECORDS_DEAD, dead); 
    contentValues.put(RecordContract.RecordInfo.RECORDS_TOTAL, total); 
    long k = SQ.insert(RecordContract.RecordInfo.TABLE_NAME, null, contentValues); 
    Log.d("Database operations", "Record added(1 row)"); 

} 

public Cursor getRecords(RecordsDatabase rdb) { 
    SQLiteDatabase SQ = rdb.getReadableDatabase(); 
    String[] columns = {RecordContract.RecordInfo.RECORDS_DATE, RecordContract.RecordInfo.RECORDS_SQUAT, RecordContract.RecordInfo.RECORDS_BENCH 
    , RecordContract.RecordInfo.RECORDS_DEAD, RecordContract.RecordInfo.RECORDS_TOTAL}; 
    Cursor CR = SQ.query(RecordContract.RecordInfo.TABLE_NAME, columns,null, null, null, null, null); 
    return CR; 

} 

これがある:私は私を持っているのはここ

mButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
      SharedPreferences.Editor editor = pref.edit(); 
       if (mEditTextBench.getText().length() > 0 && mEditTextDead.getText().length() > 0 && mEditTextSquat.getText().length() > 0) { 
       maxBenchDB = mEditTextBench.getText().toString(); 
       maxSquatDB = mEditTextSquat.getText().toString(); 
       maxDeadDB = mEditTextDead.getText().toString(); 
       maxTotalDB = mTxtTotal.getText().toString(); 

       DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
       Date today = Calendar.getInstance().getTime(); 
       reportDate = df.format(today); 

       RecordsDatabase DB = new RecordsDatabase(mContext); 
       DB.putInformation(DB, reportDate, maxSquatDB, maxDeadDB, maxBenchDB, maxTotalDB); 
       Toast.makeText(getBaseContext(), "added", Toast.LENGTH_LONG).show(); 

です私はsqliteデータベースと私のcustom.xml(MyProgress.java)を使用して設定したいリストビュー:

public class MyProgress extends BaseActivity { 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_progress); 
    mToolBar = activateToolbar(); 
    setUpNavigationDrawer(); 
    getSupportActionBar().setTitle("My Progress"); 

    ListView listViewRec = (ListView) findViewById(R.id.listViewRecord); 

D HERESに私のcustom_record.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Date" 
    android:textSize="18sp" 
    android:id="@+id/txtDate" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Bench" 
    android:id="@+id/txtBench" 
    android:paddingRight="5dp" 
    android:textSize="18sp" 
    android:layout_gravity="center_horizontal" 
    android:layout_alignParentTop="true" 
    android:paddingLeft="5dp" 
    android:layout_toLeftOf="@+id/txtSquat" 
    android:layout_toStartOf="@+id/txtSquat"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Squat" 
    android:textSize="18sp" 
    android:paddingRight="5dp" 
    android:id="@+id/txtSquat" 
    android:layout_alignParentTop="true" 
    android:paddingLeft="5dp" 
    android:layout_toLeftOf="@+id/txtDead" 
    android:layout_toStartOf="@+id/txtDead" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Dead" 
    android:paddingRight="5dp" 
    android:textSize="18sp" 
    android:id="@+id/txtDead" 
    android:layout_alignParentTop="true" 
    android:paddingLeft="5dp" 
    android:layout_toLeftOf="@+id/txtTotal" 
    android:layout_toStartOf="@+id/txtTotal"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Total" 
    android:textSize="18sp" 
    android:paddingRight="5dp" 
    android:paddingLeft="5dp" 
    android:id="@+id/txtTotal" 
    android:layout_marginRight="34dp" 
    android:layout_marginEnd="34dp" 

    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true"/> 

そして、ここでは、これまでのところ、私のCursorAdapterです:

public class TodoCursorAdapter extends CursorAdapter { 

    public TodoCursorAdapter(Context context, Cursor c, int flags) { 
      super(context, c, flags); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      return LayoutInflater.from(context).inflate(R.layout.custom_record, parent, false); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
      TextView txtDate = (TextView) view.findViewById(R.id.txtDate); 
      TextView txtSquat = (TextView) view.findViewById(R.id.txtSquat); 
      TextView txtBench = (TextView) view.findViewById(R.id.txtBench); 
      TextView txtDead = (TextView) view.findViewById(R.id.txtDead); 
      TextView txtTotal = (TextView) view.findViewById(R.id.txtTotal); 

      String date = cursor.getString(cursor.getColumnIndexOrThrow("date")); 
      String squat = cursor.getString(cursor.getColumnIndexOrThrow("squat")); 
      String bench = cursor.getString(cursor.getColumnIndexOrThrow("bench")); 
      String dead = cursor.getString(cursor.getColumnIndexOrThrow("dead")); 
      String total = cursor.getString(cursor.getColumnIndexOrThrow("total")); 

      txtBench.setText(bench); 
      txtDate.setText(date); 
      txtDead.setText(dead); 
      txtSquat.setText(squat); 
      txtTotal.setText(total); 
    } 
} 

私はリストビューを使用して移入するために得ることについて行くだろうかわかりませんsqlite db ..私は正しい方法でデータベースに項目を追加していると思う。助けてくれてありがとう!

+0

アダプタが正しいように見えます。 'TodoCursorAdapter'のインスタンスを作成し、それを' ListView'の 'setAdapter()'に送るだけです。 'SimpleCursorAdapter'の使用は、' TodoCursorAdapter'でやっていることすべてを行うので、あなたも考えてみてください。 –

+0

MyProgress.javaでTodoCursorAdapterのインスタンスを作成しようとしたときに、予期しているパラメータ "cursor"と "flags"をどのように置くべきかわからない例を挙げてください。 – LBJ33

答えて

1

まず私はTodoCursorAdapterコンストラクタへの変更を提案する:

public TodoCursorAdapter(Context context, Cursor c) { 
    super(context, c, 0); 
} 

私は自分自身を、パラメータフラグで混乱しています。特に、フラグを持たないコンストラクタCursorは、しばらくの間非難されています。私が言うことから、0の値は正常に動作します。

次に、putInformation()getRecords()の両方のRecordsDatabase rdbパラメータを削除する必要があります。このパラメータは、Javaがすでにキーワードthisとして自動的に送信されるため、完全に不要です。ただし、実際にはどこでもthisを使用する必要はありません。あなたは単に直接使用したいメソッドを呼び出します。あなたはそれを残している場合ので、代わりに

rdb.getWritableDatabase(); 

の代わり

getWritableDatabase(); 

を行うだけで、あなたが

this.getWritableDatabase(); 

が、thisを行うことができます自動的想定しています。

コンストラクタに渡すには、Cursorが必要です。あなたはSQLiteOpenHelper拡張し、この使用してRecordsDatabase行います

public class MyProgress { 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     // ...snip 
     RecordsDatabase helper = new RecordsDatabase(this); 
     Cursor c = db.getRecords(); 
     ListView listViewRec = (ListView) findViewById(R.id.listViewRecord); 
     Adapter adapter = new TodoRecordsAdapter(this, c); 
     listViewRec.setAdapter(adapter); 
    } 
} 

また、あなたがTodoCursorAdapterコンストラクタからCursorパラメータを削除することを検討することがあります。その他のオプション

  1. TodoCursorAdapterコンストラクタ
  2. に直接Cursorを作成しているMyProgressCursorを作成し、アダプタ上changeCursor()を呼び出します。
+0

ありがとう、私はエラーなしで実行するが、私は "MyProgress"ページをクリックするとクラッシュし、取得:java.lang.IllegalArgumentException:列 '_id'が存在しません..どこにしようとしているそのlolを確認してください – LBJ33

+0

私はToDoCursorAdapterクラスとMyProgressクラスに私の進行中のこの行を送ります:TodoCursorAdapterアダプタ=新しいTodoCursorAdapter(this、c); – LBJ33

+0

@ LBJ33一部のAndroid APIメソッドでは、SQLiteテーブルに '_id'という名前の列があると想定しています。最も簡単な解決策は、テーブルにこれを追加することです。代わりに、どのメソッドがこれを必要としているのかを判断し、それらを使用することを避けることができますこれはあまりにも多くの仕事IMOを取るように思えます...あなたはあなたのために仕事をするためにAPIに頼ることができないので、より多くのコードを書くことを意味します。 –

1

アダプタのオブジェクトを作成し、それをListViewのアダプタとして設定する必要があります。

この行の後のあなたの活動では、

ListView listViewRec = (ListView) findViewById(R.id.listViewRecord); 

このコードを入力してください。

まず次にTodoCursorAdapterのオブジェクトを作るRecordsDatabaseクラス

Cursor recordsCursor = recordsDatabase.getRecords(); 

からすべてのレコードを取得します。 developer.android.comによると

TodoCursorAdapter adapter = new TodoCursorAdapter(getContext(), recordsCursor, 
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 

についてFLAG_REGISTER_CONTENT_OBSERVER

にアダプタがカーソル上のコンテンツのオブザーバを登録しますと通知が入って来たときにonContentChanged()を呼び出します設定された場合。これを使用する場合は注意してくださいflag:登録されたオブザーバによるリークを防ぐために、現在のCursorをアダプタからアンセットする必要があります。 CursorLoaderでCursorAdapterを使用する場合、このフラグは不要です。

次に、このアダプタをListViewのアダプタとして設定します。

listViewRec.setAdapter(adapter); 
+0

'RecordsAdapter 'RecordAdapter extends ArrayAdapter'に変更するのではなく、CursorAdapter'を拡張します。そうすれば、アダプタはデータベースをクエリし、 'ListView'を手動で更新するコードを必要とするのではなく、基礎となるデータベースが変更されたときに自動的に' ListView'を更新します。また、Cursorは 'ListView'のようにすべてのレコードをメモリに保持しません。 –

+0

あなたが投票している間に更新された回答を送信していただけでした。今すぐ見て、答えが良いなら投票を調整してください –

+0

これは良いです... 'getRecords()'がパラメータを取らないように変更されていると仮定してください。別のタイプ(ここでそれを持ち出して以来)...私は 'FLAG_REGISTER_CONTENT_OBSERVER'について混乱しています。 「コンテンツオブザーバー」は何をしているのか知っていますか? 'onContentChanged()'のドキュメントは、それが古くなった管理されたクエリの再クエリメカニズムに関連していることを示唆しています。そうであれば、このフラグは使用しないでください。私はそれがフラグ値 '0'を使うべきであることを意味しますが、これが正しいかどうかはまだ確認していません。 –

関連する問題