我正在使用Retrofit2
进行一些测试,以显示用户列表并创建新用户。我的问题是在向我的api添加具有RecyclerView
请求的用户后,如何刷新POST
。
我有一个MainActivity
,它向Laravel API发出GET请求。然后,我制作一个CreateUserActivity
,该表格具有用于创建新用户的形式。单击创建用户按钮后,我完成了该活动以显示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();
}
}
答案 0 :(得分:0)
因此,在您的UserAdapter类中创建一个setUserList函数,并在将适配器内部的用户列表设置为新列表之后,在其中使用notifyDatabaseChange()。
public void setUserList(List<UserList> userList) {
userList.clear();
this.userList = userList;
notifyDataSetChanged();
}