I have an activity called ListOfPetsActivity.java
which is displaying a ListView of Pet object.
Pet.java looks like this
public class Pet {
private int petId;
private String petName;
private int age;
//getters, setters and constructors
}
I am displaying the pet objects in the ListView with a custom adapter which currently only displays the pet's name and has a button called requestAcceptButton
.
Here is the adapter
public class RequesterAdapter extends ArrayAdapter<Pet> {
private List<Pet> requestersList;
private Context context;
public RequesterAdapter(Context context, List<Pet> requestersList) {
super(context, -1, requestersList);
this.context = context;
this.requestersList = requestersList;
}
@NonNull
@Override
public View getView(int position, @Nullable View currentItemView, @NonNull ViewGroup parent) {
if (currentItemView == null) {
currentItemView = LayoutInflater.from(getContext()).inflate(R.layout.requester_item, parent, false);
}
// get the position of the view from the ArrayAdapter
Pet currentPetPosition = getItem(position);
TextView requesterNameTextView = currentItemView.findViewById(R.id.requesterNameTextView);
requesterNameTextView.setText(currentPetPosition.getPetName() ", " currentPetPosition.getAge());
Button requestAcceptButton = currentItemView.findViewById(R.id.requestAcceptButton);
return currentItemView;
}
}
Now, the current activity which shows the ListView of all the pets, ListOfPetsActivity.java
shows the list of pets with their name and age and has a button next to it.
When I click on it, I want to get all values from the pet object from that ListView item and save it in variables of string and int type so that I can communicate with the database. The thing is, I need to do that in this activity and not in the adapter. So, how am I able to get all items from the ListView item?
I am setting up the ListView and its adapter like this in the ListOfPetsActivity
.
RequesterAdapter requesterAdapter = new RequesterAdapter(ListOfPetsActivity.this, friendRequesterPetsList);
requesterListView.setAdapter(requesterAdapter);
CodePudding user response:
Step1: Create one interface
interface PetClickListener {
fun onPetSelected(petAge: Int)
}
Step2: Implement interface in your Activity:
ListOfPetsActivity implement PetClickListener {
{
override onPetSelected(petAge: Int) {
// here you will get pet Age of selected Pet in the list
}
}
Step3: update your adapter constructor:
RequesterAdapter requesterAdapter = new RequesterAdapter(ListOfPetsActivity.this, friendRequesterPetsList, this);
Step4: Create one global variable in adapter:
private PetClickListener petClickListener;
Step5: Initialise it in constructor:
public RequesterAdapter(Context context, List<Pet> requestersList, PetClickListener listener) {
super(context, -1, requestersList);
this.context = context;
this.requestersList = requestersList;
petClickListener = listener;
}
Step6: use this petClickListener when you click on button
requestAcceptButton.setOnClickListener {
petClickListener(currentPetPosition.getAge())
}
This way you will get a callback in your activity with pet age. Note: I have used Kotlin and java here. But this will give you an idea to solve your issue.
Let me know if you need further help.
CodePudding user response:
You can use an interface
for getting your Pet
object in the activity.
Create an interface in your RequesterAdapter
class
public interface ItemClickListener {
void onItemClick(Pet pet);
}
Change RequestAdapter
constructor to accept ItemClickListener
and assign it to itemClickListener
object.
private ItemClickListener itemClickListener;
public RequesterAdapter(Context context, List<Pet> requestersList, ItemClickListener itemClickListener) {
super(context, -1, requestersList);
this.context = context;
this.requestersList = requestersList;
this.itemClickListener = itemClickListener;
}
Implement this interface in MainActivity
and override onItemClick
public class MainActivity extends AppCompatActivity implements RequesterAdapter.ItemClickListener {
@Override
public void onItemClick(Pet pet) {
// execute your database operations
}
}
Trigger onItemClick
method, from requestAcceptButton
clickListener
requestAcceptButton.setOnClickListener(view -> itemClickListener.onItemClick(currentPetPosition));