给onBindViewHolder监听器AlertDialog

时间:2017-12-24 14:48:12

标签: android

有人可以帮帮我吗?我很难让我的AlertDialog显示出来。我根据点击的项目制作AlertDialog。

如果有人可以指导我做我必须做的事情来使这个警报对话框工作。这里也是我的问题的截图链接: https://i.imgur.com/oVzRwUg.png

这是代码:

    package com.mario.restaurantcroatia;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

public class LandingRestaurantAdapter extends RecyclerView.Adapter<LandingRestaurantAdapter.RestaurantViewHolder>{

    public interface PositionClickListener
    {
        void itemClicked(int position);
    }

    private Context context;
    private List<LandingRestaurants> restaurantsList;


    public LandingRestaurantAdapter(Context context, List<LandingRestaurants> restaurantsList) {
        this.context = context;
        this.restaurantsList = restaurantsList;
    }

    @Override
    public RestaurantViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from( context );
        View view = inflater.inflate( R.layout.landing_items, null );
        RestaurantViewHolder holder = new RestaurantViewHolder( view );
        return holder;
    }

    @Override
    public void onBindViewHolder(final RestaurantViewHolder holder, final int position) {

        LandingRestaurants restaurant = restaurantsList.get( position );

        holder.imageView.setImageDrawable( context.getResources().getDrawable( restaurant.getPicture() )  );

        //onClickListener for landing page town cards
        holder.imageView.setOnClickListener( new View.OnClickListener( ) {
            @Override
            public void onClick(View view) {

            /*    LayoutInflater inflater = LayoutInflater.from( context );

                final AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
                view = inflater.inflate(R.layout.landing_click_on_town_alert_dialog,null);
                ImageView location = (ImageView)view.findViewById( R.id.location );
                ImageView currentRestaurant = (ImageView)view.findViewById( R.id.restaurant );
                ImageView history = (ImageView)view.findViewById( R.id.history );
                ImageView attractions = (ImageView)view.findViewById( R.id.attractions );

                mBuilder.setView( view );
                AlertDialog dialog = mBuilder.create();
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                dialog.show();*/



            }
        } );



    }

    @Override
    public int getItemCount() {
        return restaurantsList.size();
    }

    class RestaurantViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;


        public RestaurantViewHolder(View itemView) {
            super( itemView );

            imageView = (ImageView)itemView.findViewById( R.id.picture );

        }
    }



}

2 个答案:

答案 0 :(得分:1)

这是由于对象名称onClick方法视图和您的inflater视图不匹配,因此将视图iflater对象名称view更改为dialogView或者

view = inflater.inflate(R.layout.landing_click_on_town_alert_dialog,null);

将其改为喜欢

LayoutInflater inflater = LayoutInflater.from( context );

            final AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
            View dialogView = inflater.inflate(R.layout.landing_click_on_town_alert_dialog,null);
            ImageView location = (ImageView)dialogView.findViewById( R.id.location );
            ImageView currentRestaurant = (ImageView)dialogView.findViewById( R.id.restaurant );
            ImageView history = (ImageView)dialogView.findViewById( R.id.history );
            ImageView attractions = (ImageView)dialogView.findViewById( R.id.attractions );

            mBuilder.setView( dialogView);
            AlertDialog dialog = mBuilder.create();
            dialog.show();

答案 1 :(得分:0)

您是否尝试删除透明背景?

LayoutInflater inflater = LayoutInflater.from( view.getContext());

                final AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
                View viewdialog = inflater.inflate(R.layout.landing_click_on_town_alert_dialog, null, false);
                ImageView location = (ImageView)viewdialog.findViewById( R.id.location );
                ImageView currentRestaurant = (ImageView)viewdialog .findViewById( R.id.restaurant );
                ImageView history = (ImageView)viewdialog.findViewById( R.id.history );
                ImageView attractions = (ImageView)viewdialog.findViewById( R.id.attractions );

                mBuilder.setView( viewdialog );
                AlertDialog dialog = mBuilder.create();
                dialog.show();