问题
I'm trying to design a page where address are stored in recycler view -> cardview.
When the user clicks the add address button from the Activity A the user is navigated to the add address page in Activity B. Here the user can input customer name, address line 1 and address line two.
Similarly, When the user clicks the edit address button from the Activity A**(data in activity A is passed to activity B)** the user is navigated to the edit address page in Activity B. Here the user can input customer name, address line 1 and address line two, city, state and pincode.
And once save button is clicked in Activity B, existing cardview should be updated with new information in Activity A.
This design is just like the amazon mobile app add address option.
Example: If the user has multiple address(A,B and C) and clicks edit button in Address B cardview. Then user is navigated to Activity B for editing the address and once save button is clicked the Address B cardview should be updated with the latest information.
Actual result as of now: A new cardview is created with the updated information instead of updating in the existing cardview.
Code in Activity A: I'm making use of the same onActivityResult() for both addAddress and editAddress
public class ProfileManageAdressFragment extends AppCompatActivity {
RecyclerView recyclerView;
ProfileManageAddressRecyclerAdapter adapter;
ArrayList<ProfileManageAddressGetterSetter> reviews;
@Override
protected void onCreate(Bundle savedInstanceState) {
Button addAddress;
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_profile_manage_adress);
addAddress = findViewById(R.id.addNewAddress);
addAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ProfileManageAdressFragment.this, AddNewAddress.class);
startActivityForResult(intent, 1);
}
});
reviews = new ArrayList<>();
recyclerView = findViewById(R.id.addressRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(ProfileManageAdressFragment.this));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String customer_name, address_one, address_two, city, state, pincode;
if (requestCode == 1 && resultCode == RESULT_OK) {
customer_name = data.getStringExtra("customer_name");
address_one = data.getStringExtra("address_one");
address_two = data.getStringExtra("address_two");
city = data.getStringExtra("city");
state = data.getStringExtra("state");
pincode = data.getStringExtra("pincode");
reviews.add(new ProfileManageAddressGetterSetter(customer_name, address_one, address_two, city, state, pincode));
adapter = new ProfileManageAddressRecyclerAdapter(ProfileManageAdressFragment.this, reviews);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
//saveData(customer_name, address_one, address_two, city, state, pincode);
} else if (requestCode == 2 && resultCode == RESULT_OK) {
customer_name = data.getStringExtra("customer_name");
address_one = data.getStringExtra("address_one");
address_two = data.getStringExtra("address_two");
city = data.getStringExtra("city");
state = data.getStringExtra("state");
pincode = data.getStringExtra("pincode");
reviews.add(new ProfileManageAddressGetterSetter(customer_name, address_one, address_two, city, state, pincode));
adapter = new ProfileManageAddressRecyclerAdapter(ProfileManageAdressFragment.this, reviews);
recyclerView.setAdapter(adapter);
//adapter.notifyDataSetChanged();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(ProfileManageAdressFragment.this, "Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
Code in RecyclerAdapter:
public class ProfileManageAddressRecyclerAdapter extends RecyclerView.Adapter<ProfileManageAddressRecyclerAdapter.ViewHolder> {
private ArrayList<ProfileManageAddressGetterSetter> mDataset = new ArrayList<>();
private String sCustomer_name, sAddress_one, sAddress_two, sCity, sState, sPincode;
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView customer_name, address_one, address_two, city, state, pincode;
private Button edit, remove;
public ViewHolder(View v) {
super(v);
customer_name = (TextView) v.findViewById(R.id.customerName);
address_one = (TextView) v.findViewById(R.id.addressLineOne);
address_two = v.findViewById(R.id.addressLineTwo);
city = (TextView) v.findViewById(R.id.cardCity);
state = (TextView) v.findViewById(R.id.cardState);
pincode = v.findViewById(R.id.cardPincode);
edit = v.findViewById(R.id.editAddress);
remove = v.findViewById(R.id.removeAddress);
}
}
public ProfileManageAddressRecyclerAdapter(ProfileManageAdressFragment profileManageAdressFragment, ArrayList<ProfileManageAddressGetterSetter> dataset) {
mDataset.clear();
mDataset.addAll(dataset);
}
@Override
public ProfileManageAddressRecyclerAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_manage_address, parent, false);
final ProfileManageAddressRecyclerAdapter.ViewHolder vh = new ProfileManageAddressRecyclerAdapter.ViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(@NonNull final ProfileManageAddressRecyclerAdapter.ViewHolder holder, int position) {
ProfileManageAddressGetterSetter profileManageAddressGetterSetter = mDataset.get(position);
holder.address_one.setText(profileManageAddressGetterSetter.getAddress_line_1());
holder.address_two.setText(profileManageAddressGetterSetter.getGetAddress_line_2());
holder.customer_name.setText(profileManageAddressGetterSetter.getContractor_name());
holder.city.setText(profileManageAddressGetterSetter.getCity());
holder.state.setText(profileManageAddressGetterSetter.getState());
holder.pincode.setText(profileManageAddressGetterSetter.getPincode());
holder.edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), EditNewAddress.class);
switch (holder.getAdapterPosition()) {
case 0:
mDataset.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(), mDataset.size());
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
System.out.println("*********" + holder.address_one.getText());
sCustomer_name = holder.customer_name.getText().toString();
sAddress_one = holder.address_one.getText().toString();
sAddress_two = holder.address_two.getText().toString();
sCity = holder.city.getText().toString();
sState = holder.state.getText().toString();
sPincode = holder.pincode.getText().toString();
intent.putExtra("customer_name", sCustomer_name);
intent.putExtra("address_one", sAddress_one);
intent.putExtra("address_two", sAddress_two);
intent.putExtra("city", sCity);
intent.putExtra("state", sState);
intent.putExtra("pincode", sPincode);
((Activity) v.getContext()).startActivityForResult(intent,2);
break;
case 1:
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
System.out.println("*********" + holder.address_one.getText());
sCustomer_name = holder.customer_name.getText().toString();
sAddress_one = holder.address_one.getText().toString();
sAddress_two = holder.address_two.getText().toString();
sCity = holder.city.getText().toString();
sState = holder.state.getText().toString();
sPincode = holder.pincode.getText().toString();
intent.putExtra("customer_name", sCustomer_name);
intent.putExtra("address_one", sAddress_one);
intent.putExtra("address_two", sAddress_two);
intent.putExtra("city", sCity);
intent.putExtra("state", sState);
intent.putExtra("pincode", sPincode);
((Activity) v.getContext()).startActivityForResult(intent,2);
break;
}
}
});
holder.remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDataset.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(), mDataset.size());
}
});
}
@Override
public int getItemCount() {
return mDataset.size();
}
}
Code in Activity B:
public class EditNewAddress extends AppCompatActivity {
private EditText customer_name, address_one, address_two, city, state, pincode;
private Button add_address;
private String sCustomer_name, sAddress_one, sAddress_two, sCity, sState, sPincode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_address);
customer_name = findViewById(R.id.customerName);
address_one = findViewById(R.id.addressOne);
address_two = findViewById(R.id.addressTwo);
add_address = findViewById(R.id.addAddress);
city = findViewById(R.id.city);
state = findViewById(R.id.state);
pincode = findViewById(R.id.pincode);
Intent intent = getIntent();
sCustomer_name = intent.getStringExtra("customer_name");
sAddress_one = intent.getStringExtra("address_one");
sAddress_two = intent.getStringExtra("address_two");
sCity = intent.getStringExtra("city");
sState = intent.getStringExtra("state");
sPincode = intent.getStringExtra("pincode");
customer_name.setText(sCustomer_name);
address_one.setText(sAddress_one);
address_two.setText(sAddress_two);
city.setText(sCity);
state.setText(sState);
pincode.setText(sPincode);
add_address.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(customer_name.getText().toString().equals("") || address_one.getText().toString().equals("") ||
address_two.getText().toString().equals("") || city.getText().toString().equals("") ||
state.getText().toString().equals("") || pincode.getText().toString().equals("")
) {
Toast.makeText(EditNewAddress.this, "Please input all fields", Toast.LENGTH_LONG).show();
}else {
sCustomer_name = customer_name.getText().toString();
sAddress_one = address_one.getText().toString();
sAddress_two = address_two.getText().toString();
sCity = city.getText().toString();
sState = state.getText().toString();
sPincode = pincode.getText().toString();
Intent intent = new Intent(EditNewAddress.this, ProfileManageAdressFragment.class);
intent.putExtra("customer_name", sCustomer_name);
intent.putExtra("address_one", sAddress_one);
intent.putExtra("address_two", sAddress_two);
intent.putExtra("city", sCity);
intent.putExtra("state", sState);
intent.putExtra("pincode", sPincode);
setResult(RESULT_OK, intent);
finish();
}
}
});
}
}
I'm new to android and kindly help me solve this. Million thanks in advance for solutions! :)
回答1:
You're doing the same mistake again.
Reinitializing List and adapter every time in onActivityResult, so every time your data length 1 because the list is Reinitialize and add new data.
For your current situation
You can not edit already added list data. so you need to do 2 things pass cardView or list position to activity 2 and after successfully editing pass position to activity 1 again. so now you have a position. In onActivityResult removeData from list using position Like list.remove(position);
and after removing add data to that position like. list.add(position,data);
来源:https://stackoverflow.com/questions/62730315/two-cardview-created-when-trying-to-update-the-cardview