androidlistviewandroid-fragmentsandroid-drawer

Custom List in Fragment wont work


So im new in Android programming I'm trying to make a custom ListView. I follow a tutorial on YouTube (https://www.youtube.com/watch?v=gAIB4fTm2BA) but i cant get it work on a fragment.

public class DriverFragment extends Fragment implements GeneralFragment   {

ListView listView;
public DriverFragment() {

    // Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view;
    view = inflater.inflate(R.layout.fragment_driver, container, false);
    listView = (ListView) view.findViewById(R.id.driverList);
    DriverAdapter driverAdapter = new DriverAdapter(getActivity().getApplicationContext(),R.layout.driver_listview);
    listView.setAdapter(driverAdapter);
    Driver a = new Driver("John Smith","Johnsmith@example.com","123");

    driverAdapter.add(a);

    return view;
}

Driver Adapter :

public class DriverAdapter extends ArrayAdapter {
ArrayList list = new ArrayList();

public DriverAdapter(Context context, int resource) {
    super(context, resource);
}


static  class Holder{
    TextView NAME;
    TextView EMAIL;
    TextView PHONE;
}
public void add(Driver driver) {
    list.add(driver);
    super.add(driver);

}

@Override
public Object getItem(int position) {
    return this.list.get(position);
}

public  int getCount(){
    return  this.list.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row = convertView;
    Holder holder;
    if(convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.driver_listview, parent, false);
        holder = new Holder();
        holder.NAME = (TextView) row.findViewById(R.id.driverName);
        holder.EMAIL = (TextView) row.findViewById(R.id.driverMail);
        holder.PHONE = (TextView) row.findViewById(R.id.driverPhone);
        row.setTag(holder);
    }else {
        holder  = (Holder) row.getTag();

    }
    Driver driver = (Driver)getItem(position);
    holder.NAME.setText(driver.getName());
    holder.EMAIL.setText(driver.getMail());
    holder.PHONE.setText(driver.getPhone());

    return  row;

}

The XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="DriverName"
    android:id="@+id/driverName"
    android:padding="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="DriverMail"
    android:id="@+id/driverMail"
    android:layout_gravity="center_horizontal"
    android:padding="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="DriverPhone"
    android:id="@+id/driverPhone"
    android:padding="10dp" />


Solution

  • My last answer didn't seem to help, I quickly put something together.

    I would strongly advise reading up more on this topics since Lists and Adapters are an integral part of Android development and having an understanding of how it works is really helpful.

    public class Driver {
    
        private String name;
        private String email;
        private String phone;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    }
    

    Fragment:

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.fragment, container, false);
            ListView listView = (ListView) view.findViewById(R.id.listView);
    
            Driver a = new Driver();
            a.setName("Name");
            a.setEmail("Email");
            a.setPhone("Phone");
    
            ArrayList<Driver> drivers = new ArrayList<>();
            drivers.add(a);
    
            DriverAdapter driverAdapter = new DriverAdapter(getActivity().getApplicationContext(),R.layout.driver_listview, drivers);
            listView.setAdapter(driverAdapter);
    
            return view;
    
        }
    

    Adapter

    public class DriverAdapter extends ArrayAdapter<Driver> {
    
    
        public DriverAdapter(Context context, int resource, List<Driver> objects) {
            super(context, resource, objects);
        }
    
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row;
            row = convertView;
            Holder holder;
            if(convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = layoutInflater.inflate(R.layout.driver_listview, parent, false);
                holder = new Holder();
                holder.name = (TextView) row.findViewById(R.id.driverName);
                holder.email = (TextView) row.findViewById(R.id.driverMail);
                holder.phone = (TextView) row.findViewById(R.id.driverPhone);
                row.setTag(holder);
            }else {
                holder  = (Holder) row.getTag();
    
            }
            Driver driver = getItem(position);
            holder.name.setText(driver.getName());
            holder.email.setText(driver.getEmail());
            holder.phone.setText(driver.getPhone());
    
            return  row;
        }
    
        public class Holder {
            protected TextView name;
            protected TextView email;
            protected TextView phone;
        }
    }
    

    Adapter XML:

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/driverName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="DriverName"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/black" />
    
        <TextView
            android:id="@+id/driverMail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="10dp"
            android:text="DriverMail"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/black" />
    
        <TextView
            android:id="@+id/driverPhone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="DriverPhone"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/black" />
    
    </LinearLayout>
    

    If this doesn't help you, there is a problem with your fragment showing on the device. As I have tested this myself and it displays correctly on the screen.