我會自定義導航抽屜。我使用了在其他線程的stackoverflow中找到的示例。 該代碼是一個複製和粘貼,但在logcat中運行應用程序時會獲取一個異常:「ResourceNotFoundException」。 下面粘貼一段logcat中的導航抽屜,ResourceNotFoundException
08-02 20:06:26.367: E/AndroidRuntime(6084): android.content.res.Resources$NotFoundException: String resource ID #0x0
08-02 20:06:26.367: E/AndroidRuntime(6084): at android.content.res.Resources.getText(Resources.java:239)
08-02 20:06:26.367: E/AndroidRuntime(6084): at android.widget.TextView.setText(TextView.java:3837)
08-02 20:06:26.367: E/AndroidRuntime(6084): at it.ustation.DrawerAdapter.getView(DrawerAdapter.java:81)
08-02 20:06:26.367: E/AndroidRuntime(6084): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
....
我的適配器的代碼
public class DrawerAdapter extends ArrayAdapter<ListNavigationItemModel>{
public DrawerAdapter(Context context) {
super(context, 0);
}
public void addHeader(int title) {
add(new ListNavigationItemModel(title, -1, true));
}
public void addItem(int title, int icon) {
add(new ListNavigationItemModel(title, icon, false));
}
public void addItem(ListNavigationItemModel itemModel) {
add(itemModel);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return getItem(position).isHeader ? 0 : 1;
}
@Override
public boolean isEnabled(int position) {
return !getItem(position).isHeader;
}
public static class ViewHolder {
public final TextView textHolder;
public final ImageView imageHolder;
public ViewHolder(TextView text1, ImageView image1) {
this.textHolder = text1;
this.imageHolder = image1;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ListNavigationItemModel item = getItem(position);
ViewHolder holder = null;
View view = convertView;
if (view == null) {
int layout = R.layout.list_item_drawer;
if (item.isHeader)
layout = R.layout.ns_menu_row_header;
view = LayoutInflater.from(getContext()).inflate(layout, null);
TextView text1 = (TextView) view.findViewById(R.id.nd_title);
ImageView image1 = (ImageView) view.findViewById(R.id.nd_thumb_image);
view.setTag(new ViewHolder(text1, image1));
}
if (holder == null && view != null) {
Object tag = view.getTag();
if (tag instanceof ViewHolder) {
holder = (ViewHolder) tag;
}
}
if (item != null && holder != null) {
if (holder.textHolder != null)
holder.textHolder.setText(item.title);
if (holder.imageHolder != null) {
if (item.iconRes > 0) {
holder.imageHolder.setVisibility(View.VISIBLE);
holder.imageHolder.setImageResource(item.iconRes);
} else {
holder.imageHolder.setVisibility(View.GONE);
}
}
}
return view;
}
}
和模型是
public class ListNavigationItemModel {
public int title;
public int iconRes;
public boolean isHeader;
public ListNavigationItemModel(int title, int iconRes, boolean header) {
this.title = title;
this.iconRes = iconRes;
this.isHeader = header;
}
public ListNavigationItemModel(int title, int iconRes) {
this(title, iconRes, false);
}
}
和XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/nd_thumb_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:contentDescription="@string/thumbnail_desc"
android:scaleType="fitCenter"
android:src="@drawable/ic_save" />
<TextView
android:id="@+id/nd_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/nd_thumb_image"
android:text="@string/c_desc"
android:textColor="#fff"
android:textSize="16sp" />
這是用於在適配器上添加每個項目的代碼。 mPlanetTitles是一個數組字符串。
for (String item : mPlanetTitles) {
int id_menu_one = getResources().getIdentifier(item, "string", this.getPackageName());
int id_menu_one_icons = getResources().getIdentifier(menuOneIcons[oneIcons], "drawable", this.getPackageName());
ListNavigationItemModel mItem = new ListNavigationItemModel(id_menu_one, id_menu_one_icons);
mAdapter.addItem(mItem);
oneIcons++;
}
「DrawerAdapter.java」中的第81行是什麼?什麼是'item.title'。它是一個字符串嗎? – Raghunandan
DrawerAdapter中的第81行是'holder.textHolder.setText(item.title);' – davideagostini
是'item.title'字符串而不是整數? – Raghunandan