I am using this 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.0' 3rd party library for recyclerview swipe to delete an item
myclick listener is as below:
holder.deleteLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeItem(position);
}
});
Based on my condition i wrote delete logic
public void removeItem(int position) {
Batchinfo batchinfo = abc.get(position);
Iterator it = abcd.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
Batchinfo a = (Batchinfo) pair.getValue();
if(a.getBatchId().equalsIgnoreCase(batchinfo.getBatchId()))
{
ExpandableAdapter.quantityadded.remove(pair.getKey());
break;
}
}
Here ExpandableAdapter.quantityadded is a static hashmap .
but unable to delete the view ...any help ???
try this out!
you just replace your this
holder.deleteLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeItem(position);
notifyDataSetChanged();
}
});
Related
I have a button submit_house inside a navdraw that gets a onclick listener inside a different button's (add_house) onclick listener. submit_house is the button that ends the onclick of add_house and after that it gets a new onclick. yet for some reason the first submit_house onclick does not work ( the log command there really doesn't register). why is that?
code in main.java:
building_onclick = new View.OnClickListener() {
#Override
public void onClick(final View v) {
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
mMap.setOnMapClickListener(null);
// this line clears the arraylist
arrayList.clear();
// next thing you have to do is check if your adapter has changed
adapter.notifyDataSetChanged();
building.emptyBuilding();
//the navdraw
buildingViewNavdraw(add_house, houseList);
//launches drawer
// locks drawer from opening from swipes
navDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// If the navigation drawer is not open then open it, if its already open then close it.
if(!navDrawer.isDrawerOpen(Gravity.RIGHT)) navDrawer.openDrawer(Gravity.RIGHT);
else navDrawer.closeDrawer(Gravity.LEFT);
//sets building latlng
building.setLat(point.latitude);
building.setLng(point.longitude);
building.setMarkerId(Double.toString(building.getLat()).replace(".","") + Double.toString(building.getLng()).replace(".",""));
View.OnClickListener add_houseOnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO: create alert dialog to get apartment number
house.setApartment("1");
houseEditNavdraw(status, activists, familyNameEdit, addressEdit, descriptionEdit, currentStatusEdit, submit_house, add_report);
isHouseUploaded = false;
isInBuilding = true;
house.setType("House");
//copies over content from building to house
house.setLat(building.getLat());
house.setLng(building.getLng());
house.setMarkerId("A" + house.getApartment() + building.getMarkerId());
house.setLocation(building.getLocation());
currentStatusEdit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if (currentStatusEdit.getSelectedItem().toString().equals("קשר המשך")){
currentActivistsEdit.setVisibility(View.VISIBLE);
activists.setVisibility(View.VISIBLE);
currentActivists.setVisibility(View.INVISIBLE);
add_report.setVisibility(View.VISIBLE);
} else if (currentStatusEdit.getSelectedItem().toString().equals("לא מעוניינים") || currentStatusEdit.getSelectedItem().toString().equals("מעוניינים")){
currentActivists.setVisibility(View.INVISIBLE);
activists.setVisibility(View.INVISIBLE);
currentActivistsEdit.setVisibility(View.INVISIBLE);
add_report.setVisibility(View.VISIBLE);
} else if (currentStatusEdit.getSelectedItem().toString().equals("לא עונים")) {
currentActivists.setVisibility(View.INVISIBLE);
activists.setVisibility(View.INVISIBLE);
currentActivistsEdit.setVisibility(View.INVISIBLE);
add_report.setVisibility(View.INVISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
// this line adds the data of the apartmnet and puts in your array
arrayList.add(house.getApartment() + ": " + "משפחת " + house.getName());
// next thing you have to do is check if your adapter has changed
adapter.notifyDataSetChanged();
//sets the report onClicks
add_report.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
reportEditNavdraw(reportEdit, reportDateEdit, reportActivistsEdit, submit_report);
submit_report.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
houseEditNavdraw(status, activists, familyNameEdit, addressEdit, descriptionEdit, currentStatusEdit, submit_house, add_report);
if (currentStatusEdit.getSelectedItem().toString() == "קשר המשך"){
activists.setVisibility(View.VISIBLE);
currentActivistsEdit.setVisibility(View.VISIBLE);
}
Date date = new Date();
Report rpt = new Report(reportActivistsEdit.getText().toString(), reportEdit.getText().toString(), reportDateEdit.getText().toString());
reportNameList.add(house.getMarkerId() + date.toString().replace(" ", "").replace(":", "").replace("GMT+", "").replace(".",""));
reportList.add(rpt);
lastReport.setVisibility(View.VISIBLE);
lastReport.setText(reportEdit.getText());
reportActivistsTitle.setVisibility(View.VISIBLE);
reportActivists.setVisibility(View.VISIBLE);
reportActivists.setText(reportActivistsEdit.getText());
reportDateTitle.setVisibility(View.VISIBLE);
reportDate.setVisibility(View.VISIBLE);
reportDate.setText(reportDateEdit.getText());
reportEdit.setVisibility(View.INVISIBLE);
reportDateEdit.setVisibility(View.INVISIBLE);
reportActivistsEdit.setVisibility(View.INVISIBLE);
submit_report.setVisibility(View.INVISIBLE);
reportDateEdit.setText("");
reportActivistsEdit.setText("");
reportEdit.setText("");
}
});
}
});
submit_house.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO: add safeguards against partial input
//sets data for "blank" drawer
familyName.setText(familyNameEdit.getText());
familyNameEdit.setVisibility(View.INVISIBLE);
familyName.setVisibility(View.VISIBLE);
address.setText(addressEdit.getText());
addressEdit.setVisibility(View.INVISIBLE);
address.setVisibility(View.VISIBLE);
description.setText(descriptionEdit.getText());
descriptionEdit.setVisibility(View.INVISIBLE);
description.setVisibility(View.VISIBLE);
currentStatusEdit.setVisibility(View.INVISIBLE);
currentStatus.setText(currentStatusEdit.getSelectedItem().toString());
currentStatus.setVisibility(View.VISIBLE);
if (currentStatusEdit.getSelectedItem().toString() == "קשר המשך"){
currentActivists.setText(currentActivistsEdit.getText());
currentActivistsEdit.setVisibility(View.INVISIBLE);
currentActivists.setVisibility(View.VISIBLE);
activists.setVisibility(View.VISIBLE);
house.setActivists(currentActivists.getText().toString());
}
submit_report.setVisibility(View.INVISIBLE);
add_report.setVisibility(View.INVISIBLE);
currentStatusEdit.setSelection(0, false);
//setting all the values to house
house.setName(familyName.getText().toString());
house.setAddress(address.getText().toString());
house.setDescription(description.getText().toString());
house.setStatus(currentStatus.getText().toString());
//upload reports to db
if (reportNameList.size() != 0){
for (int i = 0; i < reportNameList.size(); i++){
mDatabase.child("reports").child(reportNameList.get(i)).setValue(reportList.get(i));
}
//sets the report list to the house
house.setReport(reportNameList);
house.setLatestReport(reportNameList.get(reportNameList.size() - 1));
//clears the lists for next use
reportNameList.clear();
reportList.clear();
}
mDatabase.child("houses").child(house.getLocation()).child(house.getMarkerId()).setValue(house);
house.emptyHouse();
isHouseUploaded = true;
building.addHouse(house.getMarkerId());
Log.d("here", building.getHouses().toString());
emptyNavdraw();
buildingViewNavdraw(add_house, houseList);
}
});
}
};
add_house.setOnClickListener(add_houseOnClick);
add_houseOnClick.onClick(v);
submit_house.setVisibility(View.VISIBLE);
add_house.setVisibility(View.INVISIBLE);
submit_house.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDatabase.child("houses").child(building.getLocation()).child("B" + building.getMarkerId()).setValue(building);
LatLng latLng = new LatLng(building.getLat(), building.getLng());
Marker marker = mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.building15x15)));
marker.setTag(building.getMarkerId());
building.emptyBuilding();
submit_house.setVisibility(View.INVISIBLE);
add_house.setVisibility(View.VISIBLE);
submit_house.setOnClickListener(null);
}
});
}
});
}
};
Implement your activity with View.OnClickListener interface.
It will implement a method with name onClick().
Now add this line in onCreate() method : submit_house.setOnClickListener(this);
And you can perform your work in onClick() as
public void onClick(View v)
{
if(v.id==R.id.submit_house)
{
//
}
}
But you cannot perform two different functions on a single button. For that
you have to use internal variable to identify the case.
I have two imageviews with in my card view, now how can I setOnClickListeners so that I can know which button in which cardview is selected. im1 and im2 are my clickable ImageViews
This is my code :
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ConnIfInfo dataModel = ifList.get(position);
Log.d("Name", "if list name: "+dataModel.getName());
holder.name.setText(dataModel.getName());
holder.appName.setText(dataModel.getApp().toString());
if(String.valueOf(dataModel.getPreferredModeMode().toString()) .equals( String.valueOf(ProjectionTypes.OperationMode.AOA_AA)))
{
holder.im1.setImageResource(R.drawable.auto1);
holder.im2.setImageResource(R.drawable.carlife);
holder.im1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
iPhoneProjectionManager.startApp(1,"Nexus 5");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
holder.im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
iPhoneProjectionManager.startApp(2,"Nexus 6");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
}
#Override
public int getItemCount() {
return ifList.size();
}
You can set the tag to the view. The tag can be any object.
holder.im2.setTag(position);
you can set many tags also, with the key, and the key should be unique resId. you can dump the ids in ids.xml. Refer this
holder.im2.setTag(<res_id>, position);
and you can get the tag as
holder.im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getTag()
//or if you set with key then
v.getTag(<res_id>)
// please check for null value
}
});
instead of setting the onClickListener inside the onBindViewHolder you can do that in ViewHolder class itself
You can also do this by using Interface also
For more details please check out This link contains the brief about the how to handle click event by using interface.
I have a RecyclerView with post cards. So there is a like button in every post card. When I tap on a like button. Sometimes it works fine and updates on current item. But after some scroll when I tap on like button it updates on another item simultaneously.
It doesn't send that data to server. But, only the view is updated. Then when I scroll up and down again. The data goes back to normal.
Problem is in the following onClick method:-
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("like").exists()){
holder.like.setImageResource(R.drawable.ic_favorite_red_500_36dp);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").removeValue();
}
});
}else if(dataSnapshot.child("dislike").exists()) {
holder.like.setImageResource(R.drawable.brokenheart);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").removeValue();
}
});
}else{
holder.like.setImageResource(R.drawable.ic_favorite_border_red_500_36dp);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").removeValue();
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").setValue(true);
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
holder.like.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").removeValue();
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").setValue(true);
return true;
}
});
Full Adapter class https://pastebin.com/UnFGahWT
Try the following:-
Set Your on click listeners in the public ViewHolder(View itemView) method and make Your ViewHolder class implement the View.OnClickListener.
In Adapter add:
public Topic getItem(int position) {
return topics.get(position);
}
In ViewHolder's onClick method add:
int position = getAdapterPosition();
if (position == RecyclerView.NO_POSITION) return;
item = getItem(position);
Thus You will get the exact object You need to change or do something with it.
I want to use viewpager inside recycler view and set data on pager from api.
For more details, i'm adding my code in question.
My Code is
public void onBindViewHolder(final worksheetAdapter.CustomViewHolder holder, final int position) {
final int a = position; rec_worksheet.setTag(position);
Set text on TextView
holder.question.setText(userlist.get(position).get("ques"));
holder.question.startAnimation(animFadein);
holder.tvans1.setText(userlist.get(position).get("ans1"));
holder.tvans1.startAnimation(animFadein);
holder.tvans2.setText(userlist.get(position).get("ans2"));
holder.tvans2.startAnimation(animFadein);
holder.tvans3.setText(userlist.get(position).get("ans3"));
holder.tvans3.startAnimation(animFadein);
holder.tvans4.setText(userlist.get(position).get("ans4"));
holder.tvans4.startAnimation(animFadein);
holder.submitbtn.setVisibility(View.GONE);
if(position == userlist.size() - 1){
holder.submitbtn.setVisibility(View.VISIBLE);
}
holder.queNo.setText("Question No "+String.valueOf(position+1));
holder.tvTotalQue.setText("Total Ques. "+String.valueOf(userlist.size()));
holder.tvSubName.setText(userlist.get(position).get("subject_name"));
holder.submitbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
submit();
}
});
Check if first answer is equals to correct answer
holder.llAns1.setTag(position);
holder.llAns1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getTag(position);
holder.llAns1.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
allAns.add(userlist.get(position).get("ans1"));
correctans = userlist.get(position).get("correctans");
if(holder.tvans1.getText().toString().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans1"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans1"));
CorrectAns.remove(position);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if secound answer is equals to correct answer
holder.llAns2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns2.setTag(position);
holder.llAns2.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans2.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans2"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans2"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if third answer is equals to correct answer
holder.llAns3.setTag(position);
holder.llAns3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns3.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans3.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans3"));
}else {
try {
wrongAns.add(userlist.get(position).get("ans3"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
}
}
});
Check if forth answer is equals to correct answer
holder.llAns4.setTag(position);
holder.llAns4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.llAns4.setBackgroundResource(R.drawable.que_correct_back);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
correctans = userlist.get(position).get("correctans");
if(holder.tvans4.getText().equals(correctans)){
CorrectAns.add(userlist.get(position).get("ans4"));
Log.d("corrAns1",String.valueOf(CorrectAns));
}else {
try {
wrongAns.add(userlist.get(position).get("ans1"));
CorrectAns.remove(correctans);
}catch (IndexOutOfBoundsException e){}
Log.d("corrAns2",String.valueOf(CorrectAns));
}
}
});
}
You cannot use a viewpager inside a recyclerview. If you want to use horizontal swiping inside a recyclerview then you will have to use a recyclerview inside a recyclerview. This child recyclerview will be have a horizontal layout.
This question precisely addresses that.
For the 2nd issue:
Add the following lines
holder.llAns2.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns1.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns3.setBackgroundResource(R.drawable.round_strock_green);
holder.llAns4.setBackgroundResource(R.drawable.round_strock_green);
below this line:
if(position == userlist.size() - 1){
holder.submitbtn.setVisibility(View.VISIBLE);
}
for 3rd issue:
//add this below your adapter class
private ArrayList selectedOptions;
in your onbindviewholder add this below holder.llAns4.setBackgroundResource(R.drawable.round_strock_green)
if(selectedOptions.get(position) != null){
switch (selectedOptions.get(position)){
case 1:
holder.llAns1.setBackgroundResource(R.drawable.que_correct_back);
break;
case 2:
holder.llAns2.setBackgroundResource(R.drawable.que_correct_back);
break;
case 3:
holder.llAns3.setBackgroundResource(R.drawable.que_correct_back);
break;
case 4:
holder.llAns4.setBackgroundResource(R.drawable.que_correct_back);
break;
}
}
I have answered 3 things now. Please ask new stackoverflow question for further issues.
Already I have some static data in expanded list view and from that list if any customer click on that multiple items they has to be selected and it has to be added into cart.So for that please give me some suggestions and if u have any implemented code please post here.
Thanks in advance.
Assume there is two button for add and remove item on cart screen so both have click event on adapter class below is just sample example
holder.imgAddItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCartDetail mCartDetail;
if (Utility.mCartList.containsKey(mcategoryProductDetail.productdetails.get(0).psid)) {
mCartDetail = Utility.mCartList.get(mcategoryProductDetail.productdetails.get(0).psid);
int finalMmaxBuy = 0;
if (!mCartDetail.categoryProductDetail.max_buy_qty.equalsIgnoreCase(" ")) {
finalMmaxBuy = Integer.parseInt(mCartDetail.categoryProductDetail.max_buy_qty);
}
if (mCartDetail.addQuantity < finalMmaxBuy) {
mCartDetail.addQuantity++;
}
} else {
mCartDetail = new mCartDetail();
mCartDetail.categoryProductDetail = mcategoryProductDetail.productdetails.get(0);
mCartDetail.addQuantity = 1;
Utility.mCartList.put(mcategoryProductDetail.productdetails.get(0).psid, mCartDetail);
}
mCartDetail.totalprice = Float.parseFloat(mCartDetail.categoryProductDetail.our_price) * mCartDetail.addQuantity;
holder1.tvProductCounter.setText(String.valueOf(mCartDetail.addQuantity));
}
});
holder.imgRemoveItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Utility.mCartList.containsKey(mcategoryProductDetail.productdetails.get(0).psid)) {
mCartDetail mCartDetail = Utility.mCartList.get(mcategoryProductDetail.productdetails.get(0).psid);
mCartDetail.addQuantity--;
mCartDetail.totalprice = Float.parseFloat(mCartDetail.categoryProductDetail.our_price) * mCartDetail.addQuantity;
holder1.tvProductCounter.setText(String.valueOf(mCartDetail.addQuantity));
if (mCartDetail.addQuantity == 0) {
Utility.mCartList.remove(mCartDetail.categoryProductDetail.psid);
notifyDataSetChanged();
}
}
});
and below is my model class and hashmap for data storing and send to server
public static HashMap<String, CartDetail> mCartList;
public CartDetail mCartDetail;
Hope this concept will help you to implement in your scenario