androidlistviewandroid-fragmentsandroid-viewpagerslidingpanelayout

List view showing on another fragment


I'm using a view pager with a sliding panel inside, so when my panel is expanded it creates a request of users and instantiates viewholders to show them in the list view, the problem is that they get instantiated on wherever they want, how I can tell in what fragment it should be instantiated.

Here is my code:

@Override
public void onPanelAnchored(View panel) {
    final View cView = panel;
    EndpointInterface Service = ServiceAuthGenerator.createService(EndpointInterface.class);
    currentID = sharedpreferences.getInt("CURRENTID", 0);
    Call<List<Ride>> call = Service.getPassengers(currentRide);
    call.enqueue(new Callback<List<Ride>>() {

        @Override
        public void onResponse(Response<List<Ride>> response, Retrofit retrofit) {
            if (response.isSuccess() && !response.body().isEmpty()) {
                dialogx.dismiss();
                ArrayList<String> myUsersName = new ArrayList<>();
                ArrayList<String> myUsersLastName = new ArrayList<>();
                ArrayList<String> myUsersMapDirection = new ArrayList<>();
                ArrayList<Integer> myUsersID = new ArrayList<>();
                ArrayList<Boolean> myUsersRole = new ArrayList<>();

                for (int i = 0; i < response.body().size(); i++) {
                    myUsersRole.add(response.body().get(i).getRole());
                    myUsersName.add(response.body().get(i).getUser().getFirst_name());
                    myUsersLastName.add(response.body().get(i).getUser().getLast_name());
                    myUsersMapDirection.add(getAdress(new LatLng(response.body().get(i).getOrigin_lat(), response.body().get(i).getOrigin_lng())));
                    myUsersID.add(response.body().get(i).getId());
                    currentName = myUsersName.get(i) + " " + myUsersLastName.get(i);
                    mMap.addMarker(new MarkerOptions().snippet(getAdress(new LatLng(response.body().get(Integer.valueOf(i)).getOrigin_lat(), response.body().get(Integer.valueOf(i)).getOrigin_lng()))).position(new LatLng(response.body().get(Integer.valueOf(i)).getOrigin_lat(), response.body().get(Integer.valueOf(i)).getOrigin_lng())).title(response.body().get(Integer.valueOf(i)).getUser().getFirst_name()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
                }

                ListAdapter userAdapter = new CustomAdapterRequest(MainMenu.this, myUsersName, myUsersLastName, myUsersMapDirection, myUsersID, myUsersRole, currentRide);
                ListView userListView = (ListView) cView.findViewById(R.id.listViewUserRequest);
                userListView.setAdapter(userAdapter);

            }

        }


        @Override
        public void onFailure(Throwable t) {
            Toast.makeText(getApplicationContext(), "no", Toast.LENGTH_SHORT).show();
        }
    });
}    

Also, here is my adapter code:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    myViewHolder holder = null;

    if (row == null) {
        LayoutInflater customInflater = (LayoutInflater) contexto.getSystemService(contexto.LAYOUT_INFLATER_SERVICE);

        row = customInflater.inflate(R.layout.custom_row_request, parent, false);
        holder = new myViewHolder(row);
        row.setTag(holder);
    } else {
        holder = (myViewHolder) row.getTag();
    }

    String singleNameItem = itemName.get(position);
    String singleLastNameItem = itemLastName.get(position);
    String singleDir = itemDirection.get(position);
    Integer singleID = itemIDs.get(position);
    Boolean singleRole = itemRoles.get(position);

    holder.tv_name.setText(singleNameItem + " " + singleLastNameItem);
    holder.tv_Direction.setText(singleDir);
    holder.im_profilepic.setImageResource(R.mipmap.profile_photo3);

    return row;
} 

And my holder class.

class myViewHolder {
    TextView tv_name;
    TextView tv_Direction;
    ImageView im_profilepic;

    myViewHolder(View v) {
        tv_name = (TextView) v.findViewById(R.id.nameText);
        tv_Direction = (TextView) v.findViewById(R.id.originText);
        im_profilepic = (ImageView) v.findViewById(R.id.ivImage);
    }

}

This is the Fragment class

public class fragment1 extends Fragment {

    public fragment1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (container == null) {
            return null;
        }
        return (CardView) inflater.inflate(R.layout.layout1, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void setTextDestination(String origin, String Destination, Long Date, String estimatedTime, boolean singleRole) {
        TextView tv_Destination = (TextView) getView().findViewById(R.id.TextDestination);
        TextView tv_origin = (TextView) getView().findViewById(R.id.TextOrigin);
        TextView tv_Date = (TextView) getView().findViewById(R.id.textDatePager);
        TextView tv_EstimatedTiem = (TextView) getView().findViewById(R.id.estimatedTimeRoute);
        ImageView iv_roleType = (ImageView) getView().findViewById(R.id.ImgView_roleTypeLayout1);
        tv_Destination.setText(Destination);
        tv_origin.setText(origin);
        iv_roleType.setImageResource(singleRole ? R.mipmap.steerorange3 : R.mipmap.handorange3);
        tv_EstimatedTiem.setText(estimatedTime);

        java.util.Date date = new Date(Date * 1000L);
        DateFormat format = new SimpleDateFormat("dd-MM-yyyy    hh:mm a");
        format.setTimeZone(TimeZone.getDefault());
        String formatted = format.format(date);
        tv_Date.setText(formatted);
    }
}

I created a list of fragment1 which is mu fragment class and added it to a list, then depending on how many items on the list I have is the number of instances I get, my set text function works correctly but I don't know how to do that with the list view! Thanks! :D


Solution

  • moved the method that added the list view to the fragment that was instantiated.