我想使用我的arrayadapter内的接口去另一个片段

时间:2019-05-17 23:25:04

标签: java android

我想从显示酒店列表的arrayadpter中去,如果我单击酒店,我想转到同一活动中的另一个片段。我在适配器中使用了接口,但是在片段中我总是出现nullPointerException,并且我的应用程序关闭了,我需要您的帮助

公共类MainActivity扩展了AppCompatActivity,实现了NavigationView.OnNavigationItemSelectedListener,HomeFragment.OnFragmentHomeInteractionListener,         JSONFragment.OnFragmentJSONInteractionListener,HotelAdapterRV.SuccessResponse {     私有静态最终整数MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;     私有DrawerLayout抽屉;     私有DBConnections数据库;     弦城     字符串名字;     字符串电子邮件;     酒店酒店;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = findViewById(R.id.drawer_layout);

    database = new DBConnections(this);


    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    View headerView = navigationView.getHeaderView(0);


    TextView tv = headerView.findViewById(R.id.email_head);
    TextView tv2 = headerView.findViewById(R.id.name_head);

    Intent i = getIntent();
    Bundle b = i.getExtras();
     firstname = b.getString("firstname");
     email= b.getString("email");

    String s = firstname;
    tv.setText(email);
    tv2.setText(s);


    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);

    drawer.addDrawerListener(toggle);
    toggle.syncState();

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new HomeFragment()).commit();

        //navigationView.setCheckedItem(R.id.nav_message);
    }


}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.nav_message:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new MessageFragment()).commit();
            break;

        case R.id.nav_Home:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();
            break;

        case R.id.nav_boekings:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new YourBoekingsFragment()).commit();
            break;
        case R.id.nav_favourites:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new FavoritesFragment()).commit();
            break;
        case R.id.nav_info:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new AboutFragment()).commit();
            break;
        case R.id.log_out:
            Intent intent = new Intent(this, SignupActivity.class);
            startActivity(intent);
        case R.id.nav_call_us:
            makeCall();
            break;


    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
}

private void makeCall() {
    String number = ("tel: 0465692160 ");
    Intent mIntent = new Intent(Intent.ACTION_CALL);
    mIntent.setData(Uri.parse(number));

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CALL_PHONE},
                MY_PERMISSIONS_REQUEST_CALL_PHONE);


    } else {

        try {
            startActivity(mIntent);
        } catch(SecurityException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
    } else {
        getFragmentManager().popBackStack();
    }

}


@Override
public void setCity(String city) {
    this.city=city;
}

@Override
public void setDetails(String email, String firstname) {
    JSONFragment jsonFragment = new JSONFragment();

}


@Override
public String getEmail() {
    return this.email;
}

@Override
public String getCity() {
    return this.city;
}

@Override
public String getFirstname() {
    return this.firstname;
}



@Override
public void navigateFromFragmentHome() {
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new JSONFragment())
            .commit();
}

@Override
public void setHotel(Hotel h) {
    this.hotel =h;
}

}

public class HotelAdapterRV extends RecyclerView.Adapter<HotelAdapterRV.MyViewHolder> {
private List<Hotel> hotelList;
private Context context;
String email;
String firstname;
SuccessResponse successResponse;


public HotelAdapterRV(List<Hotel> hotelList,Context context,String email,String firstname) {
    this.context = context;
    this.hotelList = hotelList;
    this.email = email;
    this.firstname = firstname;

}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_row, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    Hotel hotel = hotelList.get(position);
    holder.name.setText(hotel.getName());
    holder.city.setText(hotel.getCityname());
    holder.website.setText(hotel.getWebsite());
    holder.street.setText(hotel.getStreet());

    String img = hotelList.get(position).getImageUrl();


    Picasso.with(context)
            .load(img)
            .resize(134,134)
            .into(holder.image);


    holder.parentLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            successResponse.setHotel(hotel);



        }
    });

}

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


public static  class MyViewHolder extends RecyclerView.ViewHolder {
    TextView name;
    TextView city;
    TextView street;
    TextView website;
    ImageView image;
    ConstraintLayout parentLayout;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name_header);
        city = (TextView) itemView.findViewById(R.id.website);
        image = (ImageView) itemView.findViewById(R.id.image);
        website = (TextView) itemView.findViewById(R.id.website);
        street = (TextView) itemView.findViewById(R.id.street);
        parentLayout = itemView.findViewById(R.id.list_row);
    }
}

public interface SuccessResponse{
    void setHotel(Hotel h);
}

}

公共类JSONFragment扩展Fragment实现HotelAdapterRV.SuccessResponse {     活动mainActivty;     私有ArrayList hoteldata = new ArrayList();     私有静态最终字符串MEMBER_URL =“ https://opendata.visitflanders.org/tourist/accommodation/hotels_v2.json”;     私人RecyclerView recyclerView;     私有RecyclerView.LayoutManager layoutManager;     私有JSONDB jsondb;     私人HotelAdapterRV hAdapter;     私人ProgressBar进度栏;     私人字符串电子邮件;     私有字符串名字;     私有字符串城市名称;     私有字符串图像;     私有OnFragmentJSONInteractionListener mListener;

Hotel hotel;

public JSONFragment() {
    // Required empty public constructor
}


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mainActivty = getActivity();

    View view = inflater.inflate(R.layout.fragment_json, container, false);
    jsondb = new JSONDB(getContext());
    recyclerView = view.findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(mainActivty);
    recyclerView.setLayoutManager(layoutManager);
    //mListener.setDetails(email,firstname,cityname);
    this.email = mListener.getEmail();
    this.firstname = mListener.getFirstname();
    this.cityname = mListener.getCity();
    hAdapter = new HotelAdapterRV(hoteldata, mainActivty, email, firstname);
    recyclerView.setAdapter(hAdapter);

    jsondb.setEmail(email, firstname);

    progressbar = view.findViewById(R.id.progressBar);
    progressbar.setVisibility(View.VISIBLE);

    jsondb.parse(recyclerView, cityname, progressbar);
    return view;

}


@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    try{
        mListener = (OnFragmentJSONInteractionListener) context;
    }catch (ClassCastException e)
    {
        throw new ClassCastException(context.toString()+"must implement OnFragmentHomeInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener=null;

}

@Override
public void setHotel(Hotel h) {
    hotel = h;
}


public interface OnFragmentJSONInteractionListener
{
    String getEmail();
    String getCity();
    String getFirstname();
}

}

0 个答案:

没有答案