【问题标题】:ViewHolder failing to create a ListView, Can't store "inflater layout" as part of my ViewHolderViewHolder 无法创建 ListView,无法将“inflater 布局”存储为我的 ViewHolder 的一部分
【发布时间】:2016-09-14 11:26:18
【问题描述】:

以下是我创建列表视图的尝试:此代码用于我的 MainActivity,应用程序根本无法运行。 我试图将我的布局膨胀包含到我的视图持有者中,这破坏了一切。我将我当前的代码(不起作用)和它工作的最后一点(在尝试将布局包含到 View Holder 之前)都放入了。 这也是我唯一改变的部分,所以问题就在这里。 告诉我我做错了什么?有没有办法将布局包含到 ViewHolder 中?谢谢。

更新代码基于答案(仍然不起作用):

 public View getView(final int i, View view, ViewGroup viewGroup) {
    View rowView = view;
    ViewHolder viewHolder;

    /*using HolderView to reuse view*/
    if (rowView == null){
        viewHolder = new ViewHolder();

        //inflating the layout Once and then storing it in rowView for reusing
        rowView = inflater.inflate(R.layout.row_super_class, null);

        // configure view holder and store it for reusing
        viewHolder.largeText = (TextView) view.findViewById(R.id.largeText);
        viewHolder.smallText = (TextView) view.findViewById(R.id.smallText);

        rowView.setTag(viewHolder);
    } else{
        viewHolder = (ViewHolder)rowView.getTag();
    }

当前(不工作)代码:

    public View getView(final int i, View view, ViewGroup viewGroup) {
    View rowView = view;
    ViewHolder viewHolder = new ViewHolder();

    /*using HolderView to reuse view*/
    if (rowView == null){

        //inflating the layout Once and then storing it in rowView for reusing
        rowView = inflater.inflate(R.layout.row_super_class, null);

        // configure view holder and store it for reusing
        viewHolder.largeText = (TextView) view.findViewById(R.id.largeText);
        viewHolder.smallText = (TextView) view.findViewById(R.id.smallText);
    }

    /*using if condition to populate each row with correct information*/
    if (i >= 0) {

        viewHolder.largeText.setText(name[i]);
        viewHolder.smallText.setText(quantity[i]);

        view.setClickable(true);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context.getApplicationContext(), name[i] + " * " + quantity[i], Toast.LENGTH_SHORT).show();

                Intent goToNextActivity = new Intent(context, ClassActivity.class);
                goToNextActivity.putExtra("superClass_name", name[i]);
                context.startActivity(goToNextActivity);
            }
        });
    }
    return view;
}

这是我的应用正常运行的最后一点:

    public View getView(final int i, View view, ViewGroup viewGroup) {

    /*using HolderView to reuse view*/

    ViewHolder viewHolder = new ViewHolder();

    view = inflater.inflate(R.layout.row_super_class, null);

    viewHolder.largeText = (TextView) view.findViewById(R.id.largeText);
    viewHolder.smallText = (TextView) view.findViewById(R.id.smallText);

    /*using if condition to populate each row with correct information*/
    if (i >= 0) {

        viewHolder.largeText.setText(name[i]);
        viewHolder.smallText.setText(quantity[i]);

        view.setClickable(true);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context.getApplicationContext(), name[i] + " * " + quantity[i], Toast.LENGTH_SHORT).show();

                Intent goToNextActivity = new Intent(context, ClassActivity.class);
                goToNextActivity.putExtra("superClass_name", name[i]);
                context.startActivity(goToNextActivity);
            }
        });
    }
    return view;
}

/*using viewHolder to cache row view */
public static class ViewHolder {
    public TextView largeText;
    public TextView smallText;
}

我搜索了几个小时,但找不到答案。

【问题讨论】:

  • 当视图为空时初始化viewHolder = new ViewHolder();if (rowView == null){

标签: android android-layout listview android-viewholder getview


【解决方案1】:

感谢您的回答,在这里使用它们是我问题的答案: 1) 需要在 if 语句中初始化我的 viewHolder。 2) 需要将getView 的返回从视图更改为rowView。 3) 需要将我的 getView 中提到的所有视图更改为 rowView。

public View getView(final int i, View view, ViewGroup viewGroup) {
    View rowView = view;
    ViewHolder viewHolder;

    /*using HolderView to reuse view*/
    if (rowView == null){
        viewHolder = new ViewHolder();

        //inflating the layout Once and then storing it in rowView for reusing
        rowView = inflater.inflate(R.layout.row_super_class, null);

        // configure view holder and store it for reusing
        viewHolder.largeText = (TextView) rowView.findViewById(R.id.largeText);
        viewHolder.smallText = (TextView) rowView.findViewById(R.id.smallText);

        rowView.setTag(viewHolder);
    } else{
        viewHolder = (ViewHolder)rowView.getTag();
    }

    /*using if condition to populate each row with correct information*/
    if (i >= 0) {

        viewHolder.largeText.setText(name[i]);
        viewHolder.smallText.setText(quantity[i]);

        rowView.setClickable(true);
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context.getApplicationContext(), name[i] + " * " + quantity[i], Toast.LENGTH_SHORT).show();

                Intent goToNextActivity = new Intent(context, ClassActivity.class);
                goToNextActivity.putExtra("superClass_name", name[i]);
                context.startActivity(goToNextActivity);
            }
        });
    }
    return rowView;
}

/*using viewHolder to cache row view */
public static class ViewHolder {
    public TextView largeText;
    public TextView smallText;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多