2016-12-29 57 views
2

我在練習SQLiteDatabases。我做了兩項活動MainActivityEditorActivity。我用一個自定義適配器擴展遊標適配器的列表視圖來顯示MainActivity中的數據。插入到數據庫中是成功的,但列表視圖沒有顯示在MainActivity中。請幫我解決一下這個。 代碼下面ListView在使用自定義CursorAdapter時沒有出現在活動中

MainActivity類別

public class MainActivity extends AppCompatActivity { 

Toolbar toolbar; 
ImageButton FAB; 
ListView listView; 
CustomAdapter adapter; 

DatabaseHelper mDbHelper; 

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

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    listView = (ListView) findViewById(R.id.list_view); 

    FAB = (ImageButton) findViewById(R.id.imageButton); 
    FAB.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(MainActivity.this , EditorActivity.class)); 
     } 
    }); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    mDbHelper = new DatabaseHelper(getBaseContext()); 
    SQLiteDatabase db = mDbHelper.getReadableDatabase(); 
    Cursor c; 

    c = db.query(
      DatabaseContract.TABLE_NAME, 
      null, 
      null, 
      null, 
      null, 
      null, 
      null 
    ); 

    adapter = new CustomAdapter(MainActivity.this , c , false); 

    listView.setAdapter(adapter); 

    c.close(); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
}} 

編輯活動類

public class EditorActivity extends AppCompatActivity { 

Toolbar toolbar; 
EditText edtName, edtAge , edtMarks; 
Button submit; 
DatabaseHelper dbHelper; 

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

    toolbar = (Toolbar) findViewById(R.id.toolbar_editor); 
    setSupportActionBar(toolbar); 
    toolbar.setTitle("Editor"); 

    edtName = (EditText) findViewById(R.id.edt_name); 
    edtAge = (EditText) findViewById(R.id.edt_age); 
    edtMarks = (EditText) findViewById(R.id.edt_marks); 

    submit = (Button) findViewById(R.id.btn_submit); 

    submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      String name = edtName.getText().toString(); 
      int age = Integer.parseInt(edtAge.getText().toString()); 
      int marks = Integer.parseInt(edtMarks.getText().toString()); 

      dbHelper = new DatabaseHelper(getBaseContext()); 
      SQLiteDatabase db = dbHelper.getWritableDatabase(); 

      ContentValues values = new ContentValues(); 
      values.put(DatabaseContract.COLUMN_NAME , name); 
      values.put(DatabaseContract.COLUMN_AGE , age); 
      values.put(DatabaseContract.COLUMN_MARKS , marks); 

      long rowId = db.insert(
        DatabaseContract.TABLE_NAME, 
        null, 
        values 
      ); 

      if (rowId == -1) { 
       Toast.makeText(EditorActivity.this ,"Error inserting row"+rowId , Toast.LENGTH_SHORT).show(); 
      }else 
      { 
       Toast.makeText(EditorActivity.this ,"Succesfully inserted students data", Toast.LENGTH_SHORT).show(); 
       finish(); 

      } 


     } 
    }); 


}} 

定義適配器

public class CustomAdapter extends CursorAdapter { 

public CustomAdapter(Context context, Cursor c, boolean autoRequery) { 
    super(context, c, autoRequery); 
} 

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

@Override 
public void bindView(View view, Context context, Cursor c) { 

    TextView nameTv = (TextView) view.findViewById(R.id.list_name); 
    TextView marksTv = (TextView) view.findViewById(R.id.list_marks); 

    String nameString = c.getString(c.getColumnIndex(DatabaseContract.COLUMN_NAME)); 
    int marks = c.getInt(c.getColumnIndex(DatabaseContract.COLUMN_MARKS)); 

    nameTv.setText(nameString); 
    marksTv.setText(String.valueOf(marks)); 

}} 

activity_main XML代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<include 
    android:id="@+id/toolbar" 
    layout="@layout/tool_bar" /> 

    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbar" 
     ></ListView> 
     <ImageButton 
     android:layout_margin="15dp" 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:src="@drawable/ic_add_black_36dp" 
     android:background="@drawable/circle" 
     android:id="@+id/imageButton" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     /> 
     </RelativeLayout> 

列表視圖沒有顯示出來。該活動即將開始,但即使值正在更新到sqlite數據庫,看起來好像沒有列表視圖。 請幫幫我 謝謝。

回答

0

如果您看到您的代碼,您將創建一個光標,將其分配給適配器和列表視圖,然後關閉光標。如果您關閉光標,你會釋放所有的資源和信息在光標:

https://developer.android.com/reference/android/database/Cursor.html#close()

必須創建光標信息列表/陣列,設置適配器使用此信息,每次你來自的EditorActivity(的onResume方法主要活動)更新此陣列中的信息並通知適配器有關更改:

@Override 
protected void onResume() { 
    super.onResume(); 
    mDbHelper = new DatabaseHelper(getBaseContext()); 
    SQLiteDatabase db = mDbHelper.getReadableDatabase(); 
    Cursor c; 

    c = db.query(
      DatabaseContract.TABLE_NAME, 
      null, 
      null, 
      null, 
      null, 
      null, 
      null 
    ); 
    // This is pseudo-code, maybe methods are not correct. 
    List<DataFromDB> list = new ArrayList<DataFromDB>(); 
    while(cursor.hasNext()){ 
     DataFromDB obj ; 
     // create your obj with data from cursor 
     list.add(obj); 
    } 
    // end pseudo-code 
    c.close(); 

    adapter = new CustomAdapter(MainActivity.this , list , false); 

    listView.setAdapter(adapter); 

} 

您必須更改Adapter類以適應此代碼。如果需要幫助,請告訴我們。

+0

非常感謝。我明白了。如果我有任何問題,我會嘗試一下,然後回來。 –

+0

如果答案沒問題,請將其標爲正確。 – Neonamu

相關問題