Reputation: 381
This is how I am inserting data into database using Room Persistence Library:
@Entity(tableName = "users")
public class ExpParent implements Parent<ExpChild>, Serializable {
@Ignore
private List<ExpChild> mChildrenList;
@Embedded
@SerializedName("UserInfo")
private ExpChild childrenList;
private String UserName;
private String CreationDateTime;
@PrimaryKey(autoGenerate = false)
@NonNull
private String Id ="";
private String Topic;
//......
Data access object:
@Dao
public interface UserDao {
@Query("SELECT * FROM users")
List<ExpParent> getAllUsers();
@Insert(onConflict = IGNORE)
Long[] insertAllUsers(List<ExpParent> expParents);
}
but when I want to showing number of inserted object into my db I got this error:
FATAL EXCEPTION: main
Process: com.example.sayres.a2_expandrecyandretrofit, PID: 2830
android.content.res.Resources$NotFoundException: String resource ID #0x8
at android.content.res.Resources.getText(Resources.java:312)
at android.widget.Toast.makeText(Toast.java:286)
at com.example.sayres.a2_expandrecyandretrofit.MainActivity$2$1$1.run(MainActivity.java:72)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
this is my way to insert my object into db:
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
ServerResponse body = response.body();
final List<ExpParent> users = body.getUsers();
new Thread(new Runnable() {
@Override
public void run() {
final Long[] longs = db.userDao().insertAllUsers(users);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, longs.length, Toast.LENGTH_SHORT).show();
}
});
}
}).start();
I am by retrofit, fetch data from my service as a list and then into worker thread I have inserted these data into my db.finally I want to show how number row inserted into my database.but I got error!!! what is my mistake?
Upvotes: 0
Views: 1312
Reputation: 10871
You are misusing Toast.makeText()
.
2nd parameter can be either int
resource ID (example R.string.some_string
), or a String
.
You are trying to pass an integer (longs.length
), which is not a resource ID. To show the integer, you will have to convert it to a string first.
To do that you can for example add an empty string to it:
Toast.makeText(MainActivity.this, longs.length + "", Toast.LENGTH_SHORT).show();
Upvotes: 4