Question

I have created a new project using AndroidStudio and I have included the NavigationDrawer that comes from with the IDE.
I would like to modify the Navigation that comes from the left side and add two TextView, one will hold the menu text and the second will hold just a letter right now.

So I have create a class to manage my custom menu:

public class MioMenu{
    private String voce;
    private String immagine;

    public MioMenu(String immagine, String voce){
        this.immagine = immagine;
        this.voce = voce;
    }

    public String getVoce(){
        return this.voce;
    }

    public String getImmagine(){
        return this.immagine;
    }
}

Then I have created my CustomAdapter and override the getView method:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MioMenuAdapter extends ArrayAdapter<MioMenu>{

    public MioMenuAdapter(Context context, int textViewResourceId, MioMenu[] objects){
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_row_menu_laterale, null);
        TextView icona = (TextView)convertView.findViewById(R.id.iconaCustom);
        TextView testo = (TextView)convertView.findViewById(R.id.testoCustom);

        MioMenu mm = getItem(position);
        icona.setText(mm.getImmagine());
        testo.setText(mm.getVoce());

        return convertView;
    }
}

And this is my custom_layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/iconaCustom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#00AA00"
        />

    <TextView
        android:id="@+id/testoCustom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#00AA00"
        />

</LinearLayout>

Now I have edited the onCreateView in the NavigationDrawerFragment.java created by default with the IDE:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });

        //Creo una nuova lista che conterrà i miei dati per generare il menu
        List list = new LinkedList();

        //Creo le voci
        list.add(new MioMenu("a", getString(R.string.title_section1)));
        list.add(new MioMenu("b", getString(R.string.title_section2)));
        list.add(new MioMenu("c", getString(R.string.title_section3)));
        list.add(new MioMenu("d", getString(R.string.title_section4)));
        list.add(new MioMenu("e", getString(R.string.title_section5)));
        list.add(new MioMenu("f", getString(R.string.title_section6)));

        /*mDrawerListView.setAdapter(new ArrayAdapter<String>(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_2,
                android.R.id.text1,
                //Elenco voci del menu laterale
                new String[]{
                        getString(R.string.title_section1),
                        getString(R.string.title_section2),
                        getString(R.string.title_section3),
                        getString(R.string.title_section4),
                        getString(R.string.title_section5),
                        getString(R.string.title_section6),
                }));
           */

        MioMenuAdapter mio_adapter = new MioMenuAdapter(getActivity(), R.layout.custom_row_menu_laterale, list);
        mDrawerListView.setAdapter(mio_adapter);

        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }

As you can see I have commented out the setAdapter and I have create a List to save my custom object and then pass it to my custom adapter but the IDE stops me with a red line under

MioMenuAdapter mio_adapter = new MioMenuAdapter(getActivity(), R.layout.custom_row_menu_laterale, list);

With this error:

enter image description here

I have no idea what I am doing wrong here

Was it helpful?

Solution

Make changes in your Adapter and pass List as third parameter or remove it from constructor because I am not seeing your are using it at all anywhere in your adapter. Might be that is also a mistake of yours, So If you want to use your list as well which you have passed in your constructor your Adapter implementation should be something like given below :

 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.TextView;

 public class MioMenuAdapter extends ArrayAdapter<MioMenu>{

 List list;

public MioMenuAdapter(Context context, int textViewResourceId, List myList){
    super(context, textViewResourceId, objects);
    list=myList;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.custom_row_menu_laterale, null);
    TextView icona = (TextView)convertView.findViewById(R.id.iconaCustom);
    TextView testo = (TextView)convertView.findViewById(R.id.testoCustom);

    MioMenu mm = list.get(position);
    icona.setText(mm.getImmagine());
    testo.setText(mm.getVoce());

    return convertView;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top