sqliteに名前を追加しようとしています。データがsqliteに挿入されるとすぐに、私はrecyclerviewを更新しています。以下は私のコード項目がedittext
を介して追加され、ボタンがclikedされるたびにsqliteで項目を追加した後、RecyclerViewに項目がありません
public class DbContract {
public static final int SYNC_STATUS_OK=0;
public static final int SYNC_STATUS_FAILED=1;
public static final String DATABASE_NAME="contactdb";
public static final String TABLE_NAME="contactinfo";
public static final String NAME="name";
public static final String SYNC_STATUS="syncstatus";
}
public class DbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION=1;
public static final String CREATE_TABLE="create table "+DbContract.TABLE_NAME+" (id integer primary key autoincrement, "+DbContract.NAME+" text,"+
DbContract.SYNC_STATUS+" integer);";
public static final String DROP_TABLE="drop table if exists "+DbContract.TABLE_NAME;
public DbHelper(Context context) {
super(context, DbContract.DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(DROP_TABLE);
onCreate(sqLiteDatabase);
}
public void saveToLocalDatabase(String name,int syncstatus,SQLiteDatabase sqLiteDatabase){
ContentValues contentValues=new ContentValues();
contentValues.put(DbContract.NAME,name);
contentValues.put(DbContract.SYNC_STATUS,syncstatus);
sqLiteDatabase.insert(DbContract.TABLE_NAME,null,contentValues);
}
public Cursor readFromLocalDatabase(SQLiteDatabase sqLiteDatabase){
String[] projection={DbContract.NAME,DbContract.SYNC_STATUS};
// Log.i("frff", "readFromLocalDatabase: "+projection.toString());
return (sqLiteDatabase.query(DbContract.TABLE_NAME,projection,null,null,null,null,null));
}
public void updateLocalDatabase(String name, int syncstatus,SQLiteDatabase sqLiteDatabase){
ContentValues contentValues=new ContentValues();
contentValues.put(DbContract.SYNC_STATUS,syncstatus);
String selection = DbContract.NAME+" LIKE ? ";
String[] selection_args={name};
sqLiteDatabase.update(DbContract.TABLE_NAME,contentValues,selection,selection_args);
}
}
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private EditText editText;
private Button button;
private ArrayList<Contact> arrayList;
private RecyclerAdapter recyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
editText = findViewById(R.id.et);
button = findViewById(R.id.button);
arrayList = new ArrayList<>();
recyclerAdapter = new RecyclerAdapter(arrayList);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(recyclerAdapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editText.getText().toString();
saveToLocalStorage(name);
editText.setText("");
}
});
}
public void submitName(View view) {
}
public boolean checkNetworkConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}
private void readFromLocalStorage() {
arrayList.clear();
DbHelper dbHelper = new DbHelper(this);
SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
Cursor cursor = dbHelper.readFromLocalDatabase(sqLiteDatabase);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(DbContract.NAME));
int sync_status = cursor.getInt(cursor.getColumnIndex(DbContract.SYNC_STATUS));
arrayList.add(new Contact(name, sync_status));
}
recyclerAdapter.notifyDataSetChanged();
cursor.close();
dbHelper.close();
}
private void saveToLocalStorage(String name) {
DbHelper dbHelper = new DbHelper(this);
SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
if (checkNetworkConnection()) {
} else {
dbHelper.saveToLocalDatabase(name, DbContract.SYNC_STATUS_FAILED, sqLiteDatabase);
}
readFromLocalStorage();
dbHelper.close();
}
}
で、何の項目は私のrecyclerview
に見ることができません。私は私のログキャッチに何のエラーもなく、どちらもアプリがクラッシュしない。どんな助けでも大歓迎です。
アプリを再起動すると?? –
私はそれをしましたが、まだ動作しません – Pritish
あなたのJavaクラスを表示します。 –