0

I'm trying to populate a drop down list with a list of users from a data source , which i am taking from my database. I created my own layout for this because I needed some extra information like email address along with the name and title of the user. The layout I pasted below doesn't have that because I wanted to just start by populating the full name first.

hcp_dropdown.xml (Layout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey_98"
android:orientation="horizontal">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/Row_assessment_file_for_list_assessment_text_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="normal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        tools:text="Lastname, Firstname" />

</android.support.constraint.ConstraintLayout>

I realised I then had to inflate this view so I did this in my adapter.

HCPListAdapter.java (Adapter):

public class HCPListAdapter extends ArrayAdapter<User> {
public HCPListAdapter(Context context, List<User> users){
    super(context, 0, users);
}

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.hcp_dropdown, parent, false);
    }
    HCPViewHolder viewHolder = (HCPViewHolder) convertView.getTag();

    if (viewHolder == null) {
        viewHolder = new HCPViewHolder();
        viewHolder.name = (TextView) convertView.findViewById(R.id.Row_assessment_file_for_list_assessment_text_name);

        convertView.setTag(viewHolder);
    }

    User user = getItem(position);
    if (user != null) {
        viewHolder.name.setText(user.getTitle() + ". " + user.getLastname() + ", " + user.getFirstname());
    } else {
        viewHolder.name.setText(R.string.error_unknown);
        return convertView;
    }
    return convertView;
}

    private class HCPViewHolder{
        public TextView name;
    }
}

Here is my problem When I try now use this adapter as my spinner and to populate it with information, it displays the first user and then when I click the drop down button it crashes and gives me an error in logcat (below my RegisterActivity.java)

RegisterActivity.java (Activity)

 HCPListAdapter HCPAdapter = new HCPListAdapter(RegisterActivity.this, MainActivity.sListHCPs);
        mspinnerCompleteHCP1.setAdapter(HCPAdapter);

...And I get this logcat message on the app crash:

FATAL EXCEPTION: main
                                                                     Process: co.uk.mednet.mednet_ghana, PID: 8483
                                                                     android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                         at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:202)
                                                                         at android.content.res.Resources.loadXmlResourceParser(Resources.java:2970)
                                                                         at android.content.res.Resources.getLayout(Resources.java:1986)
                                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:425)
                                                                         at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:380)
                                                                         at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:457)
                                                                         at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:1121)
                                                                         at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:1117)
                                                                         at android.widget.Spinner.measureContentWidth(Spinner.java:958)
                                                                         at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1369)
                                                                         at android.widget.Spinner$DropdownPopup.show(Spinner.java:1408)
                                                                         at android.widget.Spinner.performClick(Spinner.java:869)
                                                                         at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:438)
                                                                         at android.view.View$PerformClick.run(View.java:23748)
                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

1 Answers1

0

Try this code.

public class HCPListAdapter extends ArrayAdapter<User> {
List<User> users;
LayoutInflater inflater;

public HCPListAdapter(Activity context, int resouceId, int textviewId, List<User> list){
    super(context,resouceId,textviewId, list);
    users = list;
    inflater = context.getLayoutInflater();
}

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.hcp_dropdown, parent, false);
    }
    TextView name = convertView.findViewById(R.id.Row_assessment_file_for_list_assessment_text_name);

    User user = getItem(position);
    if (user != null) {
        name.setText(user.getTitle() + ". " + user.getLastname() + ", " + user.getFirstname());
    } else {
        name.setText(R.string.error_unknown);
        return convertView;
    }
    return convertView;
}

@Override
public int getCount() {
    return users.size();
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        convertView = inflater.inflate(R.layout.hcp_dropdown, parent, false);
    }
    User user = getItem(position);
    TextView txtTitle = convertView.findViewById(R.id.Row_assessment_file_for_list_assessment_text_name);
    txtTitle.setText(user.getTitle() + ". " + user.getLastname() + ", " + user.getFirstname());
    return convertView;
    }
}
Ranjan
  • 1,096
  • 10
  • 22
  • my layout file is called `hcp_dropdown.xml`. Sorry I will clear that up in an edit. – Mwikala Kangwa Jan 02 '18 at 14:58
  • Where are you setting `onItemClickListener` to list items? – Ranjan Jan 02 '18 at 15:06
  • My bad, problem is not that. Post onItemClickListener code. – Ranjan Jan 02 '18 at 15:08
  • I didn't do that yet because I thought it should still be able to list information without a listener – Mwikala Kangwa Jan 02 '18 at 15:08
  • You actually don't need `HCPViewHolder` class. You can create a `TextView` field inside your `getView` method. Also `Override` `getItem` method. – Ranjan Jan 02 '18 at 15:17
  • I added `@Override` above the method and it doesn't let me. It just asks me to remove `@Override` – Mwikala Kangwa Jan 02 '18 at 15:23
  • No luck @Ranjan I'm still getting the same logcat message, but that code does clean up my adapter. Thanks – Mwikala Kangwa Jan 02 '18 at 16:31
  • Try rebuilding your project – Ranjan Jan 02 '18 at 16:39
  • I get it now, you are trying to change layout of a spinner not a listView. [This](https://stackoverflow.com/questions/35983176/how-to-create-spinner-list-using-customadapter-in-android) should help. – Ranjan Jan 03 '18 at 11:13
  • Now it won't let me pass the data to the spinner. Sorry for bothering you this much. I don't have enough reputation to chat but if you wanted you could email me or some other form of contact – Mwikala Kangwa Jan 03 '18 at 11:51
  • Try this in your MainActivity. You have to use new Constructor. `Spinner spinner = findViewById(R.id.spinner); HCPListAdapter adapter = new HCPListAdapter(this, R.layout.hcp_dropdown, R.id.Row_assessment_file_for_list_assessment_text_name, users); spinner.setAdapter(adapter);` – Ranjan Jan 03 '18 at 11:57
  • Thank you, thank you so much. You have no idea how long i've been trying to achieve this. You are very talented. Thank you again – Mwikala Kangwa Jan 03 '18 at 12:05