我使用AndroidStudio
创建了一个新项目,并且我已经包含了来自IDE的NavigationDrawer
。
我想修改左侧的Navigation
并添加两个TextView
,其中一个将保留菜单文本,第二个将只保留一个字母。
所以我创建了一个类来管理我的自定义菜单:
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;
}
}
然后我创建了CustomAdapter
并覆盖了getView
方法:
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;
}
}
这是我的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>
现在我已经使用IDE编辑了默认创建的onCreateView
中的NavigationDrawerFragment.java
:
@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;
}
正如您所看到的,我已经注释了setAdapter
并且我创建了一个List
来保存我的自定义对象,然后将其传递给我的自定义适配器,但IDE在下面用红线阻止我
MioMenuAdapter mio_adapter = new MioMenuAdapter(getActivity(), R.layout.custom_row_menu_laterale, list);
出现此错误:
我不知道我在这里做错了什么
答案 0 :(得分:0)
在适配器中进行更改并将List作为第三个参数传递或从构造函数中删除它,因为我没有看到您在适配器的任何位置使用它。可能这也是你的错误,所以如果你想使用你在构造函数中传递的列表,你的Adapter实现应该如下所示:
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;
}
}