java - 带自定义适配器的 ListView 不加载标题数组,仅使用抽屉导航加载 0

标签 java android android-listview listadapter navigation-drawer

我正在寻找可以通过客户列表适配器帮助我使用抽屉导航的人。我的 Listview 遇到问题,没有填充字符串数组和列表标题字符串,而是加载某种资源 ID 号。

我是 ListView 的新手,只需要更熟悉的人来查看下面的代码来帮助我弄清楚为什么会发生这种情况。

谢谢

enter image description here

代码

NsMenuAdapter

 public class NsMenuAdapter extends ArrayAdapter<NsMenuItemModel> {

/*
 * public NsMenuAdapter(Context context, int resource, int
 * textViewResourceId, String[] objects) { super(context,
 * R.layout.ns_menu_row, textViewResourceId, objects); }
 */
public NsMenuAdapter(Context context) {
    super(context, 0);
}
public void addHeader(int title) {
    add(new NsMenuItemModel(title, -1, true));
}
public void addItem(int title, int icon) {
    add(new NsMenuItemModel(title, icon, false));
}
public void addItem(NsMenuItemModel 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) {

    NsMenuItemModel item = getItem(position);
    ViewHolder holder = null;
    View view = convertView;
    if (view == null) {
        int layout = R.layout.ns_menu_row;
        if (item.isHeader)
            layout = R.layout.ns_menu_row_header;

        view = LayoutInflater.from(getContext()).inflate(layout, null);

        TextView text1 = (TextView) view.findViewById(R.id.menurow_title);
        ImageView image1 = (ImageView) view.findViewById(R.id.menurow_icon);
        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);
            holder.textHolder.setText(String.valueOf(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;        
   }
 }

NsMenuItemModel

public class NsMenuItemModel {

public int title;
public int iconRes;
public boolean isHeader;

public NsMenuItemModel(int title, int iconRes,boolean header) {
    this.title = title;
    this.iconRes = iconRes;
    this.isHeader=header;

}

public NsMenuItemModel(int title, int iconRes) {
    this(title,iconRes,false);
}

}

主要 Activity

    private ListView mDrawerList;
private DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
private CharSequence mDrawerTitle;
private CharSequence mTitle;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
     mTitle = mDrawerTitle = getTitle();

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    _initMenu();
    mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
    mDrawer.setDrawerListener(mDrawerToggle);

}

private void _initMenu() {
    NsMenuAdapter mAdapter = new NsMenuAdapter(this);

    mAdapter.addHeader(R.string.header1);
    menuItems = getResources().getStringArray(
            R.array.dashboard_array);
    String[] menuItemsIcon = getResources().getStringArray(
            R.array.ns_menu_items_icon);

    int res = 0;
    for (String item : menuItems) {

        int id_title = getResources().getIdentifier(item, "string",
                this.getPackageName());
        int id_icon = getResources().getIdentifier(menuItemsIcon[res],
                "drawable", this.getPackageName());

        NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
        mAdapter.addItem(mItem);
        res++;
    }

    mAdapter.addHeader(R.string.header2);

    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    if (mDrawerList != null)
        mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

}

private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {

    public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
        super(
            mActivity,
            mDrawerLayout, 
            R.drawable.ic_drawer,
            R.string.drawer_open, 
            R.string.drawer_close);
    }

    @Override
    public void onDrawerClosed(View view) {
        getActionBar().setTitle(getString(R.string.solartools_gosolar_title));
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }

    @Override
    public void onDrawerOpened(View drawerView) {
        getActionBar().setTitle(getString(R.string.solartools_pvwatts_title));
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        mDrawerList.setItemChecked(position, true);
        String text= "menu click... should be implemented";
        Toast.makeText(MainActivity.this, text , Toast.LENGTH_LONG).show();
        mDrawer.closeDrawer(mDrawerList);

    }

}
@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

布局

**ms_menu_row_header**

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical" >

<TextView
    android:id="@+id/menurow_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:padding="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:singleLine="true"
    android:text=""
    android:textAllCaps="true"
    android:textAppearance="@android:style/TextAppearance.Medium"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/menu_divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/menurow_title"
    android:src="#DADADC" />

  </RelativeLayout>

**ns_menu_row**

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

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/menurow_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/menurow_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
         />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="1dp"
    android:layout_marginBottom="1dp"
    android:background="#DADADC" />

  </LinearLayout>

 **activity_dashboard**

  <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/left_drawer"
    style="@style/ListViewAppTheme"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ffffff"
    android:choiceMode="singleChoice"
    android:divider="#cecbce"
    android:dividerHeight="1dp"
    android:dividerPadding="1dp" />

  </android.support.v4.widget.DrawerLayout>

数组

 <string-array name="dashboard_array">
    <item>Home</item>
    <item>Community</item>
    <item>Blog</item>
    <item>Website</item>
    <item>The latest</item>
    <item>News</item>        
    <item>Support</item>
</string-array>

<array name="ns_menu_items_icon">
    <item>ic_action_web_site</item>
    <item>ic_action_share</item>
    <item>ic_action_negative</item>
    <item>ic_action_web_site</item>
    <item>ic_action_new</item>
    <item>ic_action_negative</item>
    <item>ic_action_expand</item>
</array>

最佳答案

and instead loading some sort of resource id number.

根据您的代码,您所看到的就是您应该得到的。首先,对于大数字(21...这是代码中的字符串 id),您会得到这个,因为在您使用的 getView() 方法中:

holder.textHolder.setText(String.valueOf(item.title));

item.title 是字符串资源的id,你不能只是传递它以期望获得实际的字符串,你需要在使用之前将其转换。这可以使用以下方法完成:

holder.textHolder.setText(getContext().getResources().getString(item.title)); 

对于带有 0 文本的行,发生这种情况是因为您没有正确设置非标题行。您在字符串数组资源 dashboard_array 中拥有字符串,但是当您构建适配器的项目时,您将使用 for 循环:

int id_title = getResources().getIdentifier(item, "string", this.getPackageName());

这不起作用,因为您正在通过仅存在于字符串数组资源中的名称查找字符串资源(因此没有 R.string.item_name_here),它将返回 0,稍后将其作为文本插入,就像对标题所做的那样。因此,您可以将该字符串数组资源分成 7 个单独的字符串并使用当前代码(使用 getIdentifier() 方法),或者更改处理非标题行文本的方式(不使用 item.title 对于普通行)。

顺便说一句,您没有正确实现 ListView 的两种类型的行。这个想法是实现 getViewTypeCOuntgetItempViewType() 方法,然后在 getView() 方法中使用它们来膨胀并设置正确的行类型。有很多关于如何执行此操作的示例。

关于java - 带自定义适配器的 ListView 不加载标题数组,仅使用抽屉导航加载 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20214495/

相关文章:

java - 获取自定义对话框中包含的 EditText 的值

android - 如何动态添加onclick到arraylist

java - Android 从 JSON 数组添加 ListView 项目

android - 删除操作后更新 ListView

java - 如何让一个变量先从 True 变为 False?

java - hibernate.cfg.xml 的 MappingException

java - 将 Java 与 Html 或 Html 与 Java 混合

java - Java应用程序如何知道它正在Docker容器中运行

javascript - React Native 嵌套导航的标题

android - 检测我的应用程序自己的android :versionCode at run time