`I'm trying to create a dependent spinner with data from sqlite on android studio. But the spinners isn't showing any content as intended. The Prefix is suppose to narrow down the selection for the Lotno and finally display the Plantation_name based on database SmartSawit.db. pls help :> (its been days)
Spinner spinnerPrefix, spinnerLotno, spinnerPlantation_name;
Context context;
database Database;
String prefixValue, lotnoValue, nameValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daftar_pokok);
spinnerPrefix = findViewById(R.id.spinnerPrefix);
spinnerLotno = findViewById(R.id.spinnerLotno);
spinnerPlantation_name = findViewById(R.id.spinnerPlantation_name);
context = this;
Database = new database(this, "SmartSawit.db", 1);
try {
Database.CheckDB();
fillSpinner(context,spinnerPrefix, "registered_plantation", "prefix", "");
spinnerPrefix.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
prefixValue = parent.getItemAtPosition(position).toString();
fillSpinner(context,spinnerLotno,"registered_plantation","lotno","where prefix = '"+prefixValue+"'");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerLotno.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
lotnoValue = parent.getItemAtPosition(position).toString();
fillSpinner(context,spinnerPlantation_name,"registered_plantation","plantation_name","where prefix = '"+prefixValue+"' and lotno ='"+lotnoValue+"'");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}catch(Exception e){
e.printStackTrace();
}
}
#SuppressLint("Range")
private void fillSpinner (Context context, Spinner mSpinner,String table, String column, String where) {
SQLiteDatabase db = Database.OpenDatabase("registered_plantation.db");
ArrayList<String> mArray = new ArrayList<>();
Cursor cursor = db.rawQuery("Select distinct "+column+" from "+table+""+where, null);
while (cursor.moveToNext()){
mArray.add(cursor.getString(cursor.getColumnIndex(column)));
}
cursor.close();
db.close();
ArrayAdapter mAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item,mArray);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(mAdapter);
}
}
my databasehelper code:
private Context context;
private static final String DATABASE_NAME = "SmartSawit.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "registered_plantation";
//private static final String COLUMN_NO = "_no";
private static final String COLUMN_NAME = "plantation_name";
private static final String COLUMN_PREFIX = "prefix";
private static final String COLUMN_LOT_NUMBER = "lotno";
String DbPath;
Context mcontext;
String DbName;
public database(Context context, String name, int version) {
super(context, DATABASE_NAME, null, version);
this.context = context;
this.mcontext = context;
this.DbName = DATABASE_NAME;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.DbPath = context.getFilesDir() + "/database/";
}
}
#Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_NAME + " TEXT PRIMARY KEY, " +
COLUMN_PREFIX + " TEXT, " +
COLUMN_LOT_NUMBER + " INTEGER);";
db.execSQL(query);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void CheckDB(){
SQLiteDatabase checkDB = null;
String filePath = DbPath + DbName;
File file = new File(filePath);
if(file.isFile() && file.exists()){
Toast.makeText(mcontext, "already exist", Toast.LENGTH_SHORT).show();
} else {
CopyDatabase();
}
}
private void CopyDatabase(){
try {
InputStream ios = mcontext.getAssets().open(DbName);
File directory = new File(DbPath);
if(!directory.exists()){
directory.mkdirs();
}
OutputStream os = new FileOutputStream(DbPath + DbName);
byte[] buffer = new byte[1024];
int len;
while((len = ios.read(buffer)) >0) {
os.write(buffer,0, len);
}
os.flush();
ios.close();
os.close();
Log.d("CopyDb", "Databse Copied");
} catch (Exception e) {
e.printStackTrace();
}
}
void addLadang (String prefix, String namaLadang, int lotNo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_PREFIX, prefix );
cv.put(COLUMN_NAME, namaLadang );
cv.put(COLUMN_LOT_NUMBER, lotNo );
long result = db.insert(TABLE_NAME,null, cv);
if (result == -1){
Toast.makeText(context, "FAILED", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "SUCCESSFULLY SAVED", Toast.LENGTH_SHORT).show();
}
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if(db != null){
cursor = db.rawQuery(query, null);
}
return cursor;
}
public SQLiteDatabase OpenDatabase(String dbName){
String filePath = DbPath + dbName;
return SQLiteDatabase.openDatabase(filePath,null,0);
}
}
You have a number of issues (not actually with the spinners).
First you are trying to open a non-existent database. That is you are using the the table name not the database name in the fillSpinner method by using:-
SQLiteDatabase db = Database.OpenDatabase("registered_plantation.db");
i.e. the database name is SmartSawit.db not registered_plantation.db.
The following fixes that issue (SEE EMBEDDED COMMENTS):-
SQLiteDatabase db = Database.OpenDatabase(/*"registered_plantation.db"<<<<<<<<<<< TABLE NAME NOt DATABASE NAME so USED >>>>>>>>>>*/ database.DATABASE_NAME /*after making it public */);
You then have issues with missing spaces in the WHERE clauses again in the fillSpinner method (fixed using, again see comments):-
Cursor cursor = db.rawQuery("Select distinct "+column+" from "+table+" "/*<<<<<<<<<< ADDED SPACE*/+where, null);
Applying the above fixes and using a layout that has the 3 spinners side by side and with different coloured backgrounds and with an excessive height. And with the following data in the database (in the assets folder name SmartSawit.db):-
Then when first run:-
Clicking on the first spinner
i.e. A and B, the 2 prefixes are selected accordingly
Clicking on B and
i.e. as expected
and so on.
Additional
If you checked the log with your original code, even though it doesn't crash you could have spotted the issue e.g. the log would have included:-
2023-02-07 11:13:59.641 24792-24792/a.a.so75365360javasqlitespinners D/CopyDb: Databse Copied
2023-02-07 11:13:59.642 24792-24792/a.a.so75365360javasqlitespinners E/SQLiteLog: (14) cannot open file at line 36667 of [c255889bd9]
2023-02-07 11:13:59.642 24792-24792/a.a.so75365360javasqlitespinners E/SQLiteLog: (14) os_unix.c:36667: (2) open(/data/user/0/a.a.so75365360javasqlitespinners/files/database/registered_plantation.db) -
2023-02-07 11:13:59.643 24792-24792/a.a.so75365360javasqlitespinners E/SQLiteDatabase: Failed to open database '/data/user/0/a.a.so75365360javasqlitespinners/files/database/registered_plantation.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:211)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:195)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:503)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:204)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:196)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:880)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:865)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:766)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
at a.a.so75365360javasqlitespinners.database.OpenDatabase(database.java:124)
at a.a.so75365360javasqlitespinners.MainActivity.fillSpinner(MainActivity.java:69)
at a.a.so75365360javasqlitespinners.MainActivity.onCreate(MainActivity.java:36)
So although the copy was successfully accomplished e.g. using Device Explorer you would something along the lines of:-
Thus it is easy to see that registered_plantation.db is not at the expected location but SmartSwait.db is.
In short. If using try/catch and things are not working as expected the Check the Log for what you expect to see in the log (e.g. looking for Database copied would have made it easy to see that things were not as expected as the error lines immediately follow that message).
Related
Search Function
mSearchView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.d("data", charSequence.toString());
}
#Override
public void onTextChanged(CharSequence charSequence2, int i, int i1, int i2) {
String select = "SELECT * FROM RECORD WHERE name LIKE '"+charSequence2+"%'";
Cursor cursor = mSQLiteHelper.getData(select);
mList.clear();
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
String phone = cursor.getString(2);
mList.add(new Model(id,name,phone));
}
mAdapter.notifyDataSetChanged();
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Update: This is LongClickListener where I tap to updated and delete my inserted data
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
//Alert dialog to display options of update and delete
final CharSequence [] items = {"Update","Delete","Call"};
AlertDialog.Builder dialog = new AlertDialog.Builder(RecordListActivity.this);
dialog.setTitle("Choose an Action");
dialog.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(i == 0){
//update
Cursor c = mSQLiteHelper.getData("SELECT id FROM RECORD");
ArrayList<Integer> arrID = new ArrayList<Integer>();
while(c.moveToNext() ){
arrID.add(c.getInt(0));
}
//Show update Dialog
showDialogUpdate(RecordListActivity.this,arrID.get(position));
}
if(i==1){
//delete
Cursor c = mSQLiteHelper.getData("SELECT id FROM RECORD");
ArrayList<Integer> arrID = new ArrayList<Integer>();
while(c.moveToNext()){
arrID.add(c.getInt(0));
}
showDialogDelete(arrID.get(position));
}
//Call try
if(i==2){
TextView tvPhone = view.findViewById(R.id.textphone);
String phone = tvPhone.getText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phone));
getBaseContext().startActivity(intent);
}
}
});
dialog.show();
return true;
}
});
Model: This is the Model
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
DataBase
public void updateData(String name, String phone, int id){
SQLiteDatabase database = this.getWritableDatabase();
//Query to update record
String sql = "UPDATE RECORD SET name=? , phone=? WHERE id=?";
SQLiteStatement statement = database.compileStatement(sql);
statement.bindString(1,name);
statement.bindString(2,phone);
statement.bindDouble(3,(double)id);
statement.execute();
database.close();
}
//Delete Data
public void deleteData(int id){
SQLiteDatabase database = this.getWritableDatabase();
//query to delete record using id
String sql = "DELETE FROM RECORD WHERE id=?";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindDouble(1,(double)id);
statement.execute();
database.close();
}
//Getting Data
public Cursor getData(String sql){
SQLiteDatabase database = this.getReadableDatabase();
return database.rawQuery(sql,null);
}
Dialog
//DeleteDialog
private void showDialogDelete(final int idRecord) {
AlertDialog.Builder dialogDelete =new AlertDialog.Builder(RecordListActivity.this);
dialogDelete.setTitle("Warning!!");
dialogDelete.setMessage("Are you sure you want to delete this?");
dialogDelete.setPositiveButton("OK", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i) {
try{
mSQLiteHelper.deleteData(idRecord);
Toast.makeText(getApplicationContext(), "Delete Successfully", Toast.LENGTH_SHORT).show();
}catch (Exception error){
Log.e("Delete error",error.getMessage());
}
}
});
dialogDelete.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialogDelete.show();
}
//UpdateDialog
private void showDialogUpdate(Activity activity, int position){
Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.update_dialog);
dialog.setTitle("Update");
final EditText updateNameId = dialog.findViewById(R.id.updateNameId);
final EditText updatePhoneId = dialog.findViewById(R.id.updatePhoneId);
final Button updatebuttonId = dialog.findViewById(R.id.updatebuttonId);
//get Data Row Clicked from SQLite
Cursor cursor = mSQLiteHelper.getData("SELECT * FROM RECORD WHERE id="+position);
mList.clear();
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
updateNameId.setText(name);
String phone = cursor.getString(2);
updatePhoneId.setText(phone);
mList.add(new Model(id,name,phone));
}
//set width of dialog
int width = (int)(activity.getResources().getDisplayMetrics().widthPixels * 0.95);
//set height of dialog
int height = (int)(activity.getResources().getDisplayMetrics().heightPixels * 0.7);
dialog.getWindow().setLayout(width,height);
dialog.show();
updatebuttonId.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
try{
mSQLiteHelper.updateData(
updateNameId.getText().toString().trim(),
updatePhoneId.getText().toString().trim(),
position);
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
}catch(Exception error){
Log.e("Update error",error.getMessage());
}
updateRecorderList();
}
});
}
private void updateRecorderList() {
//get all data from SQLite
Cursor cursor = mSQLiteHelper.getData("SELECT * FROM RECORD");
mList.clear();
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
String phone = cursor.getString(2);
mList.add(new Model(id,name,phone));
}
mAdapter.notifyDataSetChanged();
}
When I search any data in the search bar It shows me the searched data but when I click to update it, it shows me the first row data to update, and also when I click to delete it shows me the first row to delete, where is my wrong logic can you enlighten me? anyone
You took the ID incorrectly, because you used a position that was always changing and cannot be correlated with the data.
You do not have to go to the database for the ID, as you already have it:
if (i == 0) {
//update
int contactId = mList.get(position).getId();
showDialogUpdate(RecordListActivity.this, contactId);
}
if (i == 1) {
//delete
int contactId = mList.get(position).getId();
showDialogDelete(contactId);
}
SQlite Data is not updating into listview. no error showing just not save the edited value. when I click the list view it will open with the inserted value to edit but when I push the update button it won't update. Any Solution for my Problem ??
ListDataActivity.java
listView = findViewById(R.id.listViewId);
loadData();
}
public void loadData() {
//Create an arraylist so that i can load the data to add in the arraylist
ArrayList<String> listData = new ArrayList<>();
Cursor cursor = databaseHelper.showAllData();
//jodi kono cursor e row na thake
if (cursor.getCount() == 0) {
Toast.makeText(getApplicationContext(), "no data is available", Toast.LENGTH_LONG).show();
} else {
while (cursor.moveToNext()) {
listData.add(cursor.getString(1) + " \t " + cursor.getString(2));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.textViewId, listData);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String selectedValue = adapterView.getItemAtPosition(position).toString();
cursor.moveToPosition(position);
String name = cursor.getString(1);
String surName = cursor.getString(2);
// listData.add(cursor.getString(1) + " \t " + cursor.getString(2));
Intent intent = new Intent(ListDataActivity.this, UpdateActivity.class);
Toast.makeText(getApplicationContext(), "Selected Value : " + selectedValue, Toast.LENGTH_LONG).show();
intent.putExtra("id", id);
intent.putExtra("name", name);
intent.putExtra("surName", surName);
startActivity(intent);
finish();
}
});
UpdateActivity.java
id = getIntent().getExtras().getLong("id");
String name = getIntent().getExtras().getString("name");
String surName = getIntent().getExtras().getString("surName");
updateName.setText(name);
updatePhone.setText(surName);
updateButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
String name = updateName.getText().toString();
String surName = updatePhone.getText().toString();
if(view.getId()==R.id.updatebuttonId){
ListDataActivity.databaseHelper.updateInformation(id,name,surName);
startActivity(new Intent(UpdateActivity.this,ListDataActivity.class));
finish();
}
}
});
DatabaseHelper.Java
public int updateInformation(long id, String name, String surName) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(SURNAME,surName);
String whereArgs[] = {""+id};
int count = sqLiteDatabase.update(TABLE_NAME,contentValues,NAME+ "=?",whereArgs);
return count;
}
I believe that your issue is that you are trying to update using a WHERE clause that is effectively saying update the row where the NAME = the id that has been passed (likely to never find such a row).
You need to change the 3rd parameter passed to the update method to be the name of the id column. So you want something like :-
int count = sqLiteDatabase.update(TABLE_NAME,contentValues,ID+ "=?",whereArgs); //<<<<< ID instead of NAME
Note the above assumes that the String Constant ID contains the name of the id column. So you may have to change ID accordingly.
Good evening to all,
I am creating an application with java on android studio.but after creating my database and making the registration page, the information does not go into the database.the errors I had said that there is a colons not created, I checked and everything is in place. when I remove the code that allows the insertion of information in the database the application works if not it is blocked at the stage of registration.I also tried nothing
A part of my database code
public static final String DATABASE_NAME = "Login.db";
public static final String TABLE_SIGN = "userss";
//public static final String TABLE_PROSPECT = "prospect";
public static final int DATABASE_VERSION = 1;
public static final String ID_COL= "id";
public static final String NAME_COL = "name";
public static final String USERNAME_COL = "username";
public static final String EMAIL_COL = "email";
public static final String PHONE_COL = "phone";
public static final String PASSWORD_COL = "password";
public MyDbHandler(Context context) {
super(context,DATABASE_NAME,null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ TABLE_SIGN
+ "(" + ID_COL + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"+
NAME_COL + "TEXT,"+ USERNAME_COL + "TEXT," + EMAIL_COL + "TEXT," + PHONE_COL
+ " TEXT,"+ PASSWORD_COL + "TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SIGN);
}
public boolean insertData(String name, String username, String email, String phone, String password)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME_COL, name);
contentValues.put(USERNAME_COL,username);
contentValues.put(EMAIL_COL,email);
contentValues.put(PHONE_COL,phone);
contentValues.put(PASSWORD_COL,password);
long result = db.insert(TABLE_SIGN, null,contentValues);
if(result == -1)
{
return false;
}else
{
return true;
}
}
The registration methods
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nam = name.getText().toString();
String user = username.getText().toString();
String email = mail.getText().toString();
String phoneN = phoneNo.getText().toString();
String pass = password.getText().toString();
if(nam.equals("") && user.equals("") && email.equals("") && phoneN.equals("") && pass.equals(""))
{
Toast.makeText(SignUp.this,"Veuillez remplir tout les champs",Toast.LENGTH_SHORT).show();
}
else{
Boolean regResult = db.insertData(nam,user,email,phoneN,pass);
if(regResult == true)
{
Toast.makeText(SignUp.this,"Connexion réussie avec succès",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),LogIn.class);
startActivity(intent);
}
else
{
Toast.makeText(SignUp.this,"Echec de connexion",Toast.LENGTH_SHORT).show();
}
}
}
});
The errors that i have:
2021-06-23 22:51:16.489 28861-28861/com.example.suiviprospection E/SQLiteLog: (1) table userss has no column named email
2021-06-23 22:51:16.490 28861-28861/com.example.suiviprospection E/SQLiteDatabase: Error inserting phone=60005882 email=shiv#gmail.com name=GOGNON Shiva password=Shiv username=shiv
android.database.sqlite.SQLiteException: table userss has no column named email (code 1): , while compiling: INSERT INTO userss(phone,email,name,password,username) VALUES (?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1472)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.suiviprospection.MyDbHandler.insertData(MyDbHandler.java:76)
at com.example.suiviprospection.SignUp$1.onClick(SignUp.java:48)
at android.view.View.performClick(View.java:5637)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Thank you for helping me , I'm waiting for your answers
I have an activity with tow tabs(with viewPager). inside viewPager is a fragment that have RecyclerView.
first tab is for (Monday-Friday) and second tab is for (Saturday-Sunday)
I want to retrieve data from database and show in recyclerview on each tab. but i cant update my recyclerView. Please help me to do that.And another problem is that when RecyclerView show data only show first row, but the number of rows in the table.like this:
10:06:00
10:06:00
10:06:00
10:06:00
10:06:00
10:06:00
here is my code:
this is my Fragment code:
public class toDastgheybFragment extends BaseFragment {
private List<DatabaseModel> Times = new ArrayList<DatabaseModel>();
DatabaseHelper databaseHelper;
private View mView;
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_to_dastgheyb, container,
false);
databaseHelper = new DatabaseHelper(getContext());
Times = databaseHelper.getAllUsers();
mRecyclerView = mView.findViewById(R.id.myRecycler);
mAdapter = new DataAdapter(Times);
mRecyclerView.setLayoutManager(new
LinearLayoutManager(getContext()));
mRecyclerView.setAdapter(mAdapter);
return mView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<DatabaseModel> LineList = new ArrayList<>();
LineList.clear();
mAdapter = new DataAdapter(LineList);
DatabaseHelper db = new DatabaseHelper(getContext());
final List<DatabaseModel> m = db.getAllUsers();
if (m.size() > 0) {
for (int i = 0; i < m.size(); i++) {
LineList.add(m.get(i));
mAdapter.notifyDataSetChanged();
}
}
db.close();
}
}
and this is my DataHelper code:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MetroDB";//name of the
database
private static final String TABLE_NAME = "stationtime";//name for the
table
static String db_path = "/data/data/ir.shirazmetro/databases/";
private static final String Station = "station";
private static final String Time = "time";
private static final String Line = "line";
private final Context context;
private SQLiteDatabase database;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " +
TABLE_NAME + "(" + Station + " TEXT," + Time + " TEXT," + Line + " TEXT)";
db.execSQL(CREATE_CONTACTS_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
private boolean checkExist() {
SQLiteDatabase db = null;
try {
db = SQLiteDatabase.openDatabase(db_path + DATABASE_NAME, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
}
return db != null;
}
private void copyDatabase() throws IOException {
OutputStream myOutput = new FileOutputStream(db_path +
DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
InputStream myInput = context.getAssets().open(DATABASE_NAME +
".db");
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
public void importIfNotExist() throws IOException {
boolean dbExist = checkExist();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void open() {
database = SQLiteDatabase.openDatabase(db_path + DATABASE_NAME, null,
SQLiteDatabase.OPEN_READWRITE);
}
public List<DatabaseModel> getAllUsers() {
List<DatabaseModel> contactList = new ArrayList<DatabaseModel>();
String whatStation = C.whatStation;
String whatLine = C.whatLine;
String selectQuery = "SELECT " + Time + " FROM " + TABLE_NAME + "
WHERE " + Station + " LIKE '%" + whatStation + "%' AND " + Line + " LIKE '%"
+ whatLine + "%'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
do {
DatabaseModel m = new DatabaseModel();
m.setTime(cursor.getString(cursor.getColumnIndex(Time)));
contactList.add(m);
} while (cursor.moveToNext());
}
return contactList;
}
}
and finally this is my activity code:
public class station extends BaseActivity {
Toolbar mToolbar;
private TabLayout tbLayout;
private ViewPager vPager;
RecyclerView.Adapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_station);
final DatabaseHelper helper = new DatabaseHelper(this);
try {
helper.importIfNotExist();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
mToolbar = findViewById(R.id.tlbr1);
setSupportActionBar(mToolbar);
initView();
setupWithViewPager();
tbLayout.addOnTabSelectedListener(new
TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
public void line1(View view) {
Intent line1 = new Intent(this, line1.class);
startActivity(line1);
}
private void setupWithViewPager() {
BasePagerAdapter basePagerAdapter = new BasePagerAdapter(this,
getSupportFragmentManager());
vPager.setAdapter(basePagerAdapter);
tbLayout.setupWithViewPager(vPager);
}
private void initView() {
vPager = findViewById(R.id.view_pager);
tbLayout = findViewById(R.id.tab_layout);
backBtn = findViewById(R.id.backBtn);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.backBtn:
line1(view);
return;
}
}
}
Senior Geeks.
I'd like to request a simple but fully working example of how to implement an ExpandableListView while extending from BaseExpandableListAdapter Yet Reading data from an Sqlite Database.
I have researched and experimented on the question (see here), but with minimal success where i was able to display some data in the header, albeit it was same values repeating for all group headers. Also child items don't show.
The reason for extending with BaseExpandableListAdapter is to have a custom layout for the group header. The reason for SQLite access is naturally because thats where my data is stored.
All examples trawled on the net so far use either SimpleCursorTreeAdapter or CursorTreeAdapter as the extender in DB based applications or simply BaseExpandableListAdapter when data used is in ArrayLists.
Below is the Experimentation thus far. (with this code,only the group header is populated with the same figures over and over. Childitems dont appear)
public class ExpandableListViewAdapterCustom extends BaseExpandableListAdapter {
protected Activity currentActivity;
public ExpandableListViewAdapterCustom(Activity callingActivity){
this.currentActivity = callingActivity;
}
private Cursor mGroupsCursorLocal ;
private Cursor mChildCursor;
private Context ctx;
private int groupItem;
private int childItem;
private String[] fieldsToUseFromGroupCursor;
private int[] screenTextsToMapGroupDataTo;
private String[] fieldsToUseFromChildCursor;
private int[] screenTextsToMapChildDataTo;
public ArrayList<String> tempChild;
public LayoutInflater minflater;
public Activity activity;
public int intGroupTotal;
public void setCurrentActivity(Activity activity) {
this.activity = activity;
}
public void setCtx(Context ctx) {
this.ctx = ctx;
}
public void setGroupItem(int groupItem) {
this.groupItem = groupItem;
}
public void setChildItem(int childItem) {
this.childItem = childItem;
}
public Activity getCurrentActivity() {
return currentActivity;
}
public Cursor getmGroupsCursorLocal() {
return mGroupsCursorLocal;
}
public Context getCtx() {
return currentActivity.getBaseContext();
}
public void setmGroupsCursorLocal(Cursor mGroupsCursor) {
this.mGroupsCursorLocal = mGroupsCursor;
}
public ExpandableListViewAdapterCustom(Cursor mGroupsCursor,
Activity activity,
int groupItem,
int childItem,
String[] fieldsToUseFromGroupCursor,
int[] screenTextsToMapGroupDataTo,
String[] fieldsToUseFromChildCursor,
int[] screenTextsToMapChildDataTo) {
DatabaseRoutines db = new DatabaseRoutines(activity);
setmGroupsCursorLocal(mGroupsCursor);
mGroupsCursorLocal = db.fetchGroup();
activity.startManagingCursor (mGroupsCursor);
mGroupsCursorLocal.moveToFirst();
mChildCursor=db.fetchChildren(mGroupsCursorLocal.getColumnIndex("Year"));
mChildCursor.moveToFirst();
activity.startManagingCursor(mChildCursor);
setCtx(activity);
setCurrentActivity(activity);
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition,
int childPosition,boolean
isLastChild,
View convertView,
ViewGroup parent) {
View v = convertView;
if (v == null)
{
LayoutInflater inflater =
(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.exp_listview_childrow, parent, false);
}
TextView txtMonth = (TextView) v.findViewById(R.id.txtMonth);
TextView txtMonthAmountSent = (TextView)
v.findViewById(R.id.txtMonthAmountSentValue);
TextView txtMonthReceived = (TextView)
v.findViewById(R.id.txtMonthAmountReceivedValue);
txtMonth.setText(mChildCursor.getString(mChildCursor.getColumnIndex("Month")));
txtMonthAmountSent.setText
(mChildCursor.getString(mChildCursor.getColumnIndex("TotalSent")));
txtMonthReceived.setText
(mChildCursor.getString(mChildCursor.getColumnIndex("TotalReceived")));
return v;
}
#Override
public int getChildrenCount(int groupPosition) {
return (mChildCursor.getCount());
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return mGroupsCursorLocal.getCount();
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(
int groupPosition,
boolean isExpanded,
View convertView,
ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater inflater =
(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.exp_listview_groupheader, parent, false);
}
TextView txtYear = (TextView) v.findViewById(R.id.txtYearValue);
TextView txtAmountSent = (TextView) v.findViewById(R.id.txtAmountSentValue);
TextView txtAmountRecieved = (TextView)
v.findViewById(R.id.txtAmountReceivedValue);
txtYear.setText(mGroupsCursorLocal.getString(
mGroupsCursorLocal.getColumnIndex("Year")));
txtAmountSent.setText(
mGroupsCursorLocal.getString(mGroupsCursorLocal.getColumnIndex("TotalSent")));
txtAmountRecieved.setText(
GroupsCursorLocal.getString(mGroupsCursorLocal.getColumnIndex("TotalReceived")));
return v;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
The Database code is like this
public Cursor fetchGroup() {
SQLiteDatabase db = this.getReadableDatabase(); //if memory leaks check here
String query = "SELECT DISTINCT MIN(ID) AS id,
Year, SUM(SentAmount) AS TotalSent, SUM(ReceivedAmount) AS TotalReceived
FROM MyTbl GROUP BY Year ORDER BY Year DESC ";
return db.rawQuery(query, null);}
public Cursor fetchChildren(int Yr) {
SQLiteDatabase db = this.getReadableDatabase(); //if memory leaks check here
String query = "SELECT MIN(ID) AS id,
Year, Month, SUM(SentAmount) AS TotalSent,
SUM(ReceivedAmount) AS TotalReceived
FROM MyTbl Where Year= "+ Yr +" GROUP BY Year,
Month ORDER BY Year DESC, Month DESC";
return db.rawQuery(query, null);
}
The Code is called from main activity using the following
ExpandableListView elv = (ExpandableListView)
findViewById(R.id.expandableListView);
ExpandableListAdapter mAdapter = new
ExpandableListViewAdapterCustom(mGroupsCursor,
MyActivity.this,
R.layout.exp_listview_groupheader,// Your row layout for a group
R.layout.exp_listview_childrow, // Your row layout for a child
new String[] { "Year",
"TotalSent",
"TotalReceived" },// Field(s) to use from group cursor
new int[] {R.id.txtYearValue,
R.id.txtAmountSentValue,
R.id.txtAmountReceivedValue },// Widget ids to put group data
into new String[] { "Year","Month",
"TotalSent",
"TotalReceived" }, // Field(s) to use from child cursors new
int[] {R.id.txtMonthValue,
R.id.txtMonthAmountSentValue,
R.id.txtMonthAmountReceivedValue});// Widget ids to put child d
data into
elv.setClickable(true);
elv.setAdapter(mAdapter); // set the
After almost two weeks and no answer, i decided to simply use an ExpandableListView example using ArrayLists and modified it such that the ArrayLists were populated by data from the DB. Its not what i wanted but it works. I was actually suprised that nowhwere on the web is there an example of using ExpandableListview extended form BaseAdapter but reading from SQlite using say cursorTreeAdapter or SimpleCursorAdapter.
Below is how i did it in case it helps someone in future. the code shown is the bit that populates the ArrayList from DB
public ArrayList<ExpandListGroup> SetStandardGroups() {
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
int intMonthNum;
ExpandListGroup grp;
ExpandListChild chld;
//initialize db code here
DatabaseRoutines db = new DatabaseRoutines(this);
//create the Groups retreival cursor;
Cursor mGroupsCursor = db.fetchGroup();
//---the database call is done using this code which is in my
//---custom db class which implements the sqlhelper methods etc
//------start of db code snippet-------------------------------
//---public Cursor fetchGroup() {
//---SQLiteDatabase db = this.getReadableDatabase();
//--- String query = "SELECT DISTINCT MIN(ID) AS id, Year,
//--- SUM(SentAmount) AS TotalSent,
//--- SUM(ReceivedAmount) AS TotalReceived
//--- FROM Tbl GROUP BY Year ORDER BY Year DESC ";
//--- return db.rawQuery(query, null);}
//------end of db code snippet-------------------------------
mGroupsCursor.moveToFirst();
//method is depreciated from api14 but i'm targeting Gingerbread (api10) so i need to use it.
startManagingCursor(mGroupsCursor);
int intYear;
int intHeaderCounter = 0;
int intChildCounter = 0;
int intChildTotalCount = 0;
int intHeaderTotalGroupCount = mGroupsCursor.getCount();
//set the starting Year for the loop, if there is data;
if (intHeaderTotalGroupCount > 0) {
//get the first year
//intYear = mGroupsCursor.getInt(mGroupsCursor.getColumnIndex("Year"));
for (intHeaderCounter = 0; intHeaderCounter < intHeaderTotalGroupCount; intHeaderCounter++) {
grp = new ExpandListGroup();
intYear = mGroupsCursor.getInt(mGroupsCursor.getColumnIndex("Year"));
grp.setYear(intYear);
grp.setYearAmountReceived(mGroupsCursor.getDouble(mGroupsCursor.getColumnIndex("TotalReceived")));
grp.setYearAmountSent(mGroupsCursor.getDouble(mGroupsCursor.getColumnIndex("TotalSent")));
grp.setTag(mGroupsCursor.getString(mGroupsCursor.getColumnIndex("id")));
//Prepare counters for inner loop for child items of each
Cursor mChildCursor = db.fetchChildren(intYear);
mChildCursor.moveToFirst();
intChildTotalCount = mChildCursor.getCount();
//populate child items
for (intChildCounter = 0; intChildCounter < intChildTotalCount; intChildCounter++) {
chld = new ExpandListChild();
intMonthNum = mChildCursor.getInt(mChildCursor.getColumnIndex("Month"));
chld.setMonthNumber(intMonthNum);
chld.setTotalReceivedMonth(mChildCursor.getInt(mChildCursor.getColumnIndex("TotalReceived")));
chld.setTotalSentMonth(mChildCursor.getInt(mChildCursor.getColumnIndex("TotalSent")));
chld.setTag(mGroupsCursor.getString(mGroupsCursor.getColumnIndex("id")).toString());
list2.add(chld);
//grp.setItems(list2);
//move to next child record;
mChildCursor.moveToNext();
}
grp.setItems(list2);
list.add(grp);
list2 = new ArrayList<ExpandListChild>();
//move to next parent record;
mGroupsCursor.moveToNext();
}
} else {
log.d( "yourdebugtag_here", "Sorry, No Transactions Found.");
}
//db.close();
return list;
}