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:
I have no idea what I am doing wrong here