firebaseリアルタイムデータベースを使用してすべてのデバイスのデータを更新したい 私はcreate appを持っていますが、データを 'SQLitedb'にローカルに保存します。 しかし、誰かがdbでCRUD操作を行うと、それはすべてのデバイスで更新する必要がありますか?Android Firebaseリアルタイムデータベース
ここではクラスPersonクラスです。
でも、私はリストビューを使ってdbのすべてのレコードを表示しています。
public class Person {
// Labels table name
public static final String DATABASE_NAME = "PersonInfo.db";
// Labels Table Columns names
public static final String KEY_ROWID = "_id";
public static final String KEY_ID = "id";
public static final String CONTACTS_TABLE_NAME = "Person";
public static final String CONTACTS_COLUMN_FNAME = "FNAME";
public static final String CONTACTS_COLUMN_MNAME = "MNAME";
public static final String CONTACTS_COLUMN_LNAME = "LNAME";
public static final String CONTACTS_COLUMN_ADDRESS = "ADDRESS";
public static final String CONTACTS_COLUMN_YADINO = "YADINO";
public static final String CONTACTS_COLUMN_MOBILENO = "MOBILENO";
public static final String CONTACTS_COLUMN_VOTINGID = "VOTINGID";
// property help us to keep data
public int person_ID;
public String name;
public String email;
public int age;
public String fname;
public String mname;
public String lname;
public String address;
public String yadino;
public String mobileno;
public String votingid;
public int getPerson_ID() {
return person_ID;
}
public void setPerson_ID(int person_ID) {
this.person_ID = person_ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getYadino() {
return yadino;
}
public void setYadino(String yadino) {
this.yadino = yadino;
}
public String getMobileno() {
return mobileno;
}
public void setMobileno(String mobileno) {
this.mobileno = mobileno;
}
public String getVotingid() {
return votingid;
}
public void setVotingid(String votingid) {
this.votingid = votingid;
}
}
ここで私はdbの操作を実行するクラスです。
public class StudentRepo { private DBHelper dbHelper;
public StudentRepo(Context context) {
dbHelper = new DBHelper(context);
}
public int insert(Person person) {
//Open connection to write data
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Person.CONTACTS_COLUMN_FNAME, person.getFname());
values.put(Person.CONTACTS_COLUMN_MNAME, person.getMname());
values.put(Person.CONTACTS_COLUMN_LNAME, person.getLname());
values.put(Person.CONTACTS_COLUMN_ADDRESS, person.getAddress());
values.put(Person.CONTACTS_COLUMN_YADINO,person.getYadino());
values.put(Person.CONTACTS_COLUMN_MOBILENO, person.getMobileno());
values.put(Person.CONTACTS_COLUMN_VOTINGID,person.getVotingid());
// Inserting Row
long student_Id = db.insert(Person.CONTACTS_TABLE_NAME, null, values);
db.close(); // Closing database connection
return (int) student_Id;
}
public Cursor getStudentList() {
//Open connection to read only
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT rowid as " +
Person.KEY_ROWID + "," +
Person.KEY_ID + "," +
Person.CONTACTS_COLUMN_FNAME + "," +
Person.CONTACTS_COLUMN_MNAME + "," +
Person.CONTACTS_COLUMN_LNAME + "," +
Person.CONTACTS_COLUMN_ADDRESS + "," +
Person.CONTACTS_COLUMN_YADINO + "," +
Person.CONTACTS_COLUMN_MOBILENO + "," +
Person.CONTACTS_COLUMN_VOTINGID +
" FROM " + Person.CONTACTS_TABLE_NAME;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
public Cursor getStudentListByKeyword(String search) {
//Open connection to read only
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT rowid as " +
Person.KEY_ROWID + "," +
Person.KEY_ID + "," +
Person.CONTACTS_COLUMN_FNAME + "," +
Person.CONTACTS_COLUMN_MNAME + "," +
Person.CONTACTS_COLUMN_LNAME + "," +
Person.CONTACTS_COLUMN_ADDRESS + "," +
Person.CONTACTS_COLUMN_YADINO + "," +
Person.CONTACTS_COLUMN_MOBILENO + "," +
Person.CONTACTS_COLUMN_VOTINGID +
" FROM " + Person.CONTACTS_TABLE_NAME +
" WHERE " + Person.CONTACTS_COLUMN_FNAME + " LIKE '%" +search + "%' "
;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
public Person getStudentById(int Id){
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Person.KEY_ID + "," +
Person.CONTACTS_COLUMN_FNAME + "," +
Person.CONTACTS_COLUMN_MNAME + "," +
Person.CONTACTS_COLUMN_LNAME + "," +
Person.CONTACTS_COLUMN_ADDRESS + "," +
Person.CONTACTS_COLUMN_YADINO + "," +
Person.CONTACTS_COLUMN_MOBILENO + "," +
Person.CONTACTS_COLUMN_VOTINGID +
" FROM " + Person.CONTACTS_TABLE_NAME
+ " WHERE " +
Person.KEY_ID + "=?";// It's a good practice to use parameter ?, instead of concatenate string
int iCount =0;
Person person = new Person();
Cursor cursor = db.rawQuery(selectQuery, new String[] { String.valueOf(Id) });
if (cursor.moveToFirst()) {
do {
person.person_ID =cursor.getInt(cursor.getColumnIndex(Person.KEY_ID));
person.setFname(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_FNAME)));
person.setMname(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_MNAME)));
person.setLname(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_LNAME)));
person.setAddress(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_ADDRESS)));
person.setYadino(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_YADINO)));
person.setMobileno(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_MOBILENO)));
person.setVotingid(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_VOTINGID)));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return person;
}
public Person getStudentByName(String fname){
// Log.d("SHAZ","ID OF STUDENT= "+String.valueOf(Id));
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Person.KEY_ID + "," +
Person.CONTACTS_COLUMN_FNAME + "," +
Person.CONTACTS_COLUMN_MNAME + "," +
Person.CONTACTS_COLUMN_LNAME + "," +
Person.CONTACTS_COLUMN_ADDRESS + "," +
Person.CONTACTS_COLUMN_YADINO + "," +
Person.CONTACTS_COLUMN_MOBILENO + "," +
Person.CONTACTS_COLUMN_VOTINGID +
" FROM " + Person.CONTACTS_TABLE_NAME
+ " WHERE " +
Person.CONTACTS_COLUMN_FNAME + "=?";// It's a good practice to use parameter ?, instead of concatenate string
int iCount =0;
Person person = new Person();
Cursor cursor = db.rawQuery(selectQuery, new String[] { String.valueOf(Person.CONTACTS_COLUMN_FNAME) });
if (cursor.moveToFirst()) {
do {
person.person_ID =cursor.getInt(cursor.getColumnIndex(Person.KEY_ID));
person.setFname(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_FNAME)));
person.setMname(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_MNAME)));
person.setLname(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_LNAME)));
person.setAddress(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_ADDRESS)));
person.setYadino(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_YADINO)));
person.setMobileno(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_MOBILENO)));
person.setVotingid(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_VOTINGID)));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return person;
}
}
これは、リストビュー
public class CustomAdapter extends CursorAdapter {
private LayoutInflater mInflater;
public CustomAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.item, parent, false);
ViewHolder holder = new ViewHolder();
holder.txtId = (TextView) view.findViewById(R.id.txtId);
holder.txtFname = (TextView) view.findViewById(R.id.txtdFname);
holder.txtMname = (TextView) view.findViewById(R.id.txtMname);
holder.txtLname = (TextView)view.findViewById(R.id.txtLname);
holder.txtVotingId = (TextView)view.findViewById(R.id.txtVotingId);
view.setTag(holder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//If you want to have zebra lines color effect uncomment below code
/*if(cursor.getPosition()%2==1) {
view.setBackgroundResource(R.drawable.item_list_backgroundcolor);
} else {
view.setBackgroundResource(R.drawable.item_list_backgroundcolor2);
}*/
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtId.setText(cursor.getString(cursor.getColumnIndex(Person.KEY_ID)));
holder.txtFname.setText(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_FNAME)));
holder.txtMname.setText(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_MNAME)));
holder.txtLname.setText(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_LNAME)));
holder.txtVotingId.setText(cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_VOTINGID)));
}
static class ViewHolder {
TextView txtId;
TextView txtFname;
TextView txtMname;
TextView txtLname;
TextView txtVotingId;
}
}
のための私のCustomAdapterであり、これは私がfirebaseに人物オブジェクトを追加した場合、どのように私は戻ってそれを取得することができるようになります
public class DBHelper extends SQLiteOpenHelper {
//version number to upgrade database version
//each time if you Add, Edit table, you need to change the
//version number.
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Person.db";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//All necessary tables you like to create will create here
String CREATE_TABLE_STUDENT = "CREATE TABLE " + Person.CONTACTS_TABLE_NAME + "("
+ Person.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ "FNAME" + " TEXT, "
+ "MNAME" + " TEXT, "
+ "LNAME" + " TEXT, "
+ "ADDRESS" + " TEXT, "
+ "YADINO" + " TEXT, "
+ "MOBILENO" + " TEXT, "
+ "VOTINGID" + " TEXT)";
db.execSQL(CREATE_TABLE_STUDENT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed, all data will be gone!!!
db.execSQL("DROP TABLE IF EXISTS " + Person.CONTACTS_TABLE_NAME);
// Create tables again
onCreate(db);
}
}
私DBHelperクラスですdb内の他のデバイスにローカルに保存することができます。
これは私のクラスです。ここで私はdbの入力をすべて受けています。
public class AddPeopleActivity extends AppCompatActivity {
Person per = null;
StudentRepo student;
EditText eFname,eMname,eLname,eAddress,eYadiNo,eMobileNo,eVoterNo;
FancyButton btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_people);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
student = new StudentRepo(AddPeopleActivity.this);
final Person person = new Person();
//init of EditText's
btnAdd = (FancyButton)findViewById(R.id.btnAddPeople);
eFname = (EditText)findViewById(R.id.eFname);
eMname = (EditText)findViewById(R.id.eMname);
eLname = (EditText)findViewById(R.id.eLname);
eAddress = (EditText)findViewById(R.id.eAddress);
eYadiNo = (EditText)findViewById(R.id.eYadiNo);
eMobileNo = (EditText)findViewById(R.id.eMobileNo);
eVoterNo = (EditText)findViewById(R.id.eVoterNo);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("SHAZ","ADDPEOPLE ACTIVITY ONCLICK INVOKED");
String efname = eFname.getText().toString();
String emname = eMname.getText().toString();
String elname = eLname.getText().toString();
String eaddress = eAddress.getText().toString();
String eyadi = eYadiNo.getText().toString();
String emobileno = eMobileNo.getText().toString();
String evoterno = eVoterNo.getText().toString();
person.setFname(efname);
person.setMname(emname);
person.setLname(elname);
person.setAddress(eaddress);
person.setYadino(eyadi);
person.setMobileno(emobileno);
person.setVotingid(evoterno);
if(person!=null) {
student.insert(person);
Toast.makeText(AddPeopleActivity.this, "Person Added Successfully", Toast.LENGTH_SHORT).show();
per = null;
}
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for(DataSnapshot userSnap:dataSnapshot.getChildren())
{
per = dataSnapshot.getValue(Person.class);
Cursor cursor = student.getStudentList();
String cVoting = cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_VOTINGID));
if(cVoting.equals(per.getVotingid()))
{
student.updateList(per);
}else{
student.insert(per);
}
}
}
}
});
}
は、これは私がAddPeopleActivity
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for(DataSnapshot userSnap:dataSnapshot.getChildren())
{
per = dataSnapshot.getValue(Person.class);
Cursor cursor = student.getStudentList();
String cVoting = cursor.getString(cursor.getColumnIndex(Person.CONTACTS_COLUMN_VOTINGID));
if(cVoting.equals(per.getVotingid()))
{
student.updateList(per);
}else{
student.insert(per);
}
}
}
私はデータを追加しました。 ** myRef.child(person.getVotingid())。setValue(person); **。私がヌルオブジェクトを取得しているデータmを取得しようとしていますか? ** Person per = dataSnapshot.getValue(Person.class); ** –
@Shaz実際にWebベースのコンソールでデータを見ることはできますか? – Hitesh
実際に私は上記の問題を解決しましたが、今私は試したすべてのデバイス上のレコードを更新する必要がありますが、それはデータを更新しませんし、IDの重複のチャンスもありますか? –