I never understood how SQL Database works. So I searched a little and found this library. I'm still trying it.
I want to display the data into a RecyclerView
using a RecyclerAdapter
. I have always worked with it and I know how to do it but only using Cloud Firestore. So my question is: how to display the data in that database library that I have found into the RecyclerView?
I have written the adapter code and created the model class. I still need the part where the data are displayed.
This is how to read all data:
Cursor res = easyDB.getAllData();
while (res.moveToNext()) {
int anIntegerVariable = res.getInt(columnIndex);
String aStringVariable = res.getString(columnIndex);
}
User.java
public class User {
String id, name, password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public User() {
}
}
UsersRecyclerAdapater.java
public class UsersRecyclerAdapater extends RecyclerView.Adapter<UsersRecyclerAdapater.ViewHolder> {
public List<User> userList;
public Context context;
public UsersRecyclerAdapater(Context context, List<User> userList) {
this.userList = userList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_list_item, parent, false);
context = parent.getContext();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
holder.setIsRecyclable(false);
String id = userList.get(position).getId();
String name = userList.get(position).getName();
String password = userList.get(position).getPassword();
holder.setIdView(id);
holder.setName(name);
holder.setPasswordView(password);
}
@Override
public int getItemCount() {
return userList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView idView;
private TextView nameView;
private TextView passwordView;
public View view;
public ViewHolder(@NonNull View itemView) {
super(itemView);
view = itemView;
}
public void setIdView(String id) {
idView = view.findViewById(R.id.user_id);
idView.setText(id);
}
public void setName(String name) {
nameView = view.findViewById(R.id.user_name);
nameView.setText(name);
}
public void setPasswordView(String password) {
passwordView = view.findViewById(R.id.user_password);
passwordView.setText(password);
}
}
}
MainActivity.java
easyDB = EasyDB.init(this, DATABASE_NAME)
.setTableName(TABLE_NAME)
.addColumn(new Column(NAME_COLUMN, "TEXT"))
.addColumn(new Column(PASSWORD_COLUMN, "TEXT"))
.doneTableColumn();
mainAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mainName.getText().toString();
String password = mainPassword.getText().toString();
boolean done = easyDB.addData(1, name)
.addData(2, password)
.doneDataAdding();
if (done) {
Toast.makeText(MainActivity.this, "User added successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "User not added", Toast.LENGTH_SHORT).show();
}
}
});
mainRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
Cursor cursor = easyDB.getAllData();
while (cursor.moveToNext()) {
String name = cursor.getString(1);
String password = cursor.getString(2);
Toast.makeText(MainActivity.this, name + "\n" + password, Toast.LENGTH_SHORT).show();
}
*/
startActivity(new Intent(MainActivity.this, UsersActivity.class));
}
});
Any help please?
You have to prepare the user list from the DB cursor and use it to RecyclerView
. Check below:
List<User> userList = new ArrayList<>();
EasyDB easyDB = EasyDB.init(this, DATABASE_NAME).setTableName(TABLE_NAME);
Cursor res = easyDB.getAllData();
if(res != null && res.getCount() > 0) {
res.moveToFirst();
do {
int id = res.getInt(0);
String name = res.getString(1);
String pass = res.getString(2);
userList.add(new User(id, name, pass));
} while (res.moveToNext());
}
res.close();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
UsersRecyclerAdapater adapter = new UsersRecyclerAdapater(this, userList);
recyclerView.setAdapter(adapter);