'How do we open google map from a button inside onBindViewHolder? [duplicate]
I would like to open google map on button click. The code below works but when I went back to the app, it throws FATAL EXCEPTION. The button is located inside onBindViewHolder method on class ProductAdapter which inherits from FirebaseRecyclerAdapter.
public class ProductAdapter extends FirebaseRecyclerAdapter<ProductModel,ProductAdapter.myViewHolder> {
/**
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
* {@link FirebaseRecyclerOptions} for configuration options.
*
* @param options
*/
public ProductAdapter(@NonNull FirebaseRecyclerOptions<ProductModel> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull ProductModel model) {
holder.name.setText(model.getName());
holder.description.setText(model.getDescription());
holder.price.setText(model.getPrice().toString());
Glide.with(holder.img.getContext())
.load(model.getSurl())
.placeholder(R.drawable.common_google_signin_btn_icon_dark)
.circleCrop()
.error(R.drawable.common_google_signin_btn_icon_dark_normal)
.into(holder.img);
holder.btnViewDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final DialogPlus dialogPlus = DialogPlus.newDialog(holder.img.getContext())
.setContentHolder(new ViewHolder(R.layout.detail_popup))
.setExpanded(true, 1920)
.create();
View v = dialogPlus.getHolderView();
TextView name = v.findViewById(R.id.prodName);
TextView quant = v.findViewById(R.id.prodQuant);
TextView price = v.findViewById(R.id.prodPrice);
TextView description = v.findViewById(R.id.prodDesc);
ImageView prodImg = v.findViewById(R.id.prodImg);
TextView location = v.findViewById(R.id.prodLocation);
Button btnProceed = v.findViewById(R.id.btnProceed);
Button btnMap = v.findViewById(R.id.btnMap);
name.setText(model.getName());
quant.setText(model.getQuant().toString());
price.setText(model.getPrice().toString());
description.setText(model.getDescription());
location.setText(model.getLocation());
Glide.with(prodImg.getContext())
.load(model.getSurl())
.placeholder(R.drawable.common_google_signin_btn_icon_dark)
.error(R.drawable.common_google_signin_btn_icon_dark_normal)
.into(prodImg);
dialogPlus.show();
btnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri = Uri.parse("geo:0, 0?q=" + location.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setPackage("com.google.android.apps.maps");
view.getContext().startActivity(intent);
dialogPlus.dismiss();
}
});
}
});
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prod_item, parent, false);
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder {
CircleImageView img;
TextView name, description, price;
Button btnViewDetail, btnProceed;
public myViewHolder(View itemView) {
super(itemView);
img = (CircleImageView) itemView.findViewById(R.id.img1);
name = (TextView) itemView.findViewById(R.id.nameTxt);
description = (TextView) itemView.findViewById(R.id.descTxt);
price = (TextView) itemView.findViewById(R.id.priceTxt);
btnViewDetail = (Button) itemView.findViewById(R.id.btnViewDetail);
}
}
}
I've been following and modifying the code in this youtube tutorial
Android runtime exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rentana, PID: 22441
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionmyViewHolder{36a72e6 position=5 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{d140f49 VFED..... .F....I. -21,73-1104,2078 #7f0801b0 app:id/rv}, adapter:com.example.rentana.ProductAdapter@9a8f04e, layout:androidx.recyclerview.widget.LinearLayoutManager@7a456f, context:com.example.rentana.DashboardActivity@682ace7
at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:6156)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6339)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4255)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4010)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at android.widget.FrameLayout.layoutChildren(Unknown Source:166)
at android.widget.FrameLayout.onLayout(Unknown Source:6)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at android.widget.FrameLayout.layoutChildren(Unknown Source:166)
at android.widget.FrameLayout.onLayout(Unknown Source:6)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at android.widget.LinearLayout.setChildFrame(Unknown Source:4)
at android.widget.LinearLayout.layoutVertical(Unknown Source:196)
at android.widget.LinearLayout.onLayout(Unknown Source:5)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at android.widget.FrameLayout.layoutChildren(Unknown Source:166)
at android.widget.FrameLayout.onLayout(Unknown Source:6)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at android.widget.LinearLayout.setChildFrame(Unknown Source:4)
at android.widget.LinearLayout.layoutVertical(Unknown Source:196)
at android.widget.LinearLayout.onLayout(Unknown Source:5)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at android.widget.FrameLayout.layoutChildren(Unknown Source:166)
at android.widget.FrameLayout.onLayout(Unknown Source:6)
at com.android.internal.policy.DecorView.onLayout(Unknown Source:0)
at android.view.View.layout(Unknown Source:74)
at android.view.ViewGroup.layout(Unknown Source:21)
at android.view.ViewRootImpl.performLayout(Unknown Source:29)
at android.view.ViewRootImpl.performTraversals(Unknown Source:2307)
at android.view.ViewRootImpl.doTraversal(Unknown Source:31)
E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(Unknown Source:2)
at android.view.Choreographer$CallbackRecord.run(Unknown Source:20)
at android.view.Choreographer.doCallbacks(Unknown Source:79)
at android.view.Choreographer.doFrame(Unknown Source:182)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Unknown Source:9)
at android.os.Handler.handleCallback(Unknown Source:2)
at android.os.Handler.dispatchMessage(Unknown Source:4)
at android.os.Looper.loop(Unknown Source:242)
at android.app.ActivityThread.main(Unknown Source:98)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
at com.android.internal.os.ZygoteInit.main(Unknown Source:275)
I/Process: Sending signal. PID: 22441 SIG: 9
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|