gpt4 book ai didi

java - 当我使用Retrofit向数据库添加数据时,如何刷新RecyclerView?

转载 作者:行者123 更新时间:2023-12-02 03:05:31 24 4
gpt4 key购买 nike

我正在使用 Retrofit2 进行一些测试,以显示用户列表并创建新用户。我的问题是如何在向我的 api 添加具有 POST 请求的用户后刷新 RecyclerView

我有一个 MainActivity 向 Laravel api 发出 GET 请求。然后,我创建一个 CreateUserActivity ,其中包含用于创建新用户的表单。单击“创建用户”按钮后,我完成该 Activity 以显示 MainActivity,但我想使用新用户更新 RecyclerView

CreateUserActivity.java

public class CreateUserActivity extends AppCompatActivity {

private TextInputEditText inputFirstName;
private TextInputEditText inputLastName;
private TextInputEditText inputEmail;
private TextInputEditText inputPassword;
private Button saveButton;
private ApiService api;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_user);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

inputFirstName = (TextInputEditText) findViewById(R.id.txtInputFirstName);
inputLastName = (TextInputEditText) findViewById(R.id.txtInputLastName);
inputEmail = (TextInputEditText) findViewById(R.id.txtInputEmail);
inputPassword = (TextInputEditText) findViewById(R.id.txtInputPassword);
saveButton = (Button) findViewById(R.id.btnCreateUser);

api = RetroClient.getApiService();

saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String firstName = inputFirstName.getText().toString().trim();
String lastName = inputLastName.getText().toString().trim();
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();

User user = new User();

user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
user.setPassword(password);
user.setGenreId(1);

if (!TextUtils.isEmpty(firstName)) {
sendPost(user);
}
}
});
}

private void sendPost(User user) {
Call<User> call = api.saveUser(user);

call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {

if(response.isSuccessful()) {
String message = "Usuario creado correctamente";
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}

}

@Override
public void onFailure(Call<User> call, Throwable t) {
System.out.println("Fail");
}
});
}

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), CreateUserActivity.class);
startActivityForResult(intent, 1);
}
});

ApiService api = RetroClient.getApiService();

Call<UsersList> call = api.getUsers();

call.enqueue(new Callback<UsersList>() {
@Override
public void onResponse(Call<UsersList> call, Response<UsersList> response) {
if (!response.isSuccessful()) {
return;
}

usersList = response.body().getUser();
usersRecyclerView = (RecyclerView) findViewById(R.id.dataTable);
usersAdapter = new UsersAdapter(usersList);
RecyclerView.LayoutManager usersLayoutManager = new LinearLayoutManager(getApplicationContext());
usersRecyclerView.setLayoutManager(usersLayoutManager);
usersRecyclerView.setItemAnimator(new DefaultItemAnimator());
usersRecyclerView.setAdapter(usersAdapter);

}

@Override
public void onFailure(Call<UsersList> call, Throwable t) {
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1 && resultCode == RESULT_OK) {
usersAdapter.notifyDataSetChanged();
}

}

最佳答案

因此,在 UserAdapter 类中创建一个 setUserList 函数,并在将适配器内的用户列表设置为新列表后,在其中使用 notificationDatabaseChange() 。

 public void setUserList(List<UserList> userList) {
userList.clear();
this.userList = userList;
notifyDataSetChanged();

}

关于java - 当我使用Retrofit向数据库添加数据时,如何刷新RecyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57024800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com