【问题标题】:ListView Filter.. Images not following filterListView 过滤器.. 图像不遵循过滤器
【发布时间】:2011-10-13 13:47:06
【问题描述】:

我为我的列表视图创建了一个自定义适配器,它可以扩展自定义视图,并允许用户过滤列表。

这一切都很好,但问题是我放置在列表视图项中的图标没有跟随过滤后的数据。

示例

请注意图标没有改变位置,但名称已正确过滤。

这是完成自定义列表视图的自定义适配器

我使用的是 Mono,所以它是用 C# 编写的,但希望你们这些 java 人也能知道我在这里做什么......

/// <summary>
/// ArrayAdapter to handle displaying student with risk indicator
/// </summary>
private class StudentListAdapter : ArrayAdapter<Student>
{
    private IList<Student> items;
    private Context outer_context;

    public StudentListAdapter(Context context, int resource, int textViewResourceId, IList<Student> items)
        : base(context, resource, textViewResourceId, items)
    {
        this.items = items;
        this.outer_context = context;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater vi = (LayoutInflater)outer_context.GetSystemService(Context.LayoutInflaterService);
        View tmpView = vi.Inflate(Resource.Layout.ItemStudent, null);

        // call base.GetView that uses Object.ToString() to keep sorting intact
        View v = base.GetView(position, tmpView, parent);

        Student s = items[position];
        if (s != null)
        {
            // determine correct risk image
            ImageView listRisk = v.FindViewById<ImageView>(Resource.Id.listRisk);
            if (s.R != null)
            {
                switch ((int)s.R.Value)
                {
                    case 1:
                        listRisk.SetImageResource(Resource.Drawable.risk_green);
                        break;
                    case 0:
                        listRisk.SetImageResource(Resource.Drawable.risk_yellow);
                        break;
                    case -1:
                        listRisk.SetImageResource(Resource.Drawable.risk_red);
                        break;
                    case -2:
                        listRisk.SetImageResource(Resource.Drawable.risk_red2);
                        break;
                    default:
                        listRisk.SetImageResource(Resource.Drawable.risk_gray);
                        break;
                }
            }
            else
            {
                listRisk.SetImageResource(Resource.Drawable.risk_gray);
            }
        }

        return v;
    }
}

更新

经过一些断点和检查,我发现当列表过滤时,它会在过滤之前重建整个列表

例如,当在过滤字段中键入一个字母时,第一个运行 GetView() 过程的学生 (s) 是 Heath, Ackerson,它是列表中的第一个学生,即使过滤器没有匹配他。

所以,似乎过滤是在运行 GetView 之后进行的,将图像留在原处......仍然不知道如何修复它......

* 完整解决方案 *

http://jondavidjohn.com/blog/2011/08/android-custom-listview-filtering

【问题讨论】:

  • 如果您在过滤时放置一些断点,代码是否会碰到代码的 SetImageResource 部分?
  • 嗯...不,好电话,我想它不会重新调用 getView
  • 基于此,我会检查's'是否是你的罪魁祸首。也许那些学生不在“项目”列表中?
  • @Brian 在帖子末尾更新了新发现

标签: c# android filter mono android-listview


【解决方案1】:

我会做以下事情: 在您的布局中放置 EditText 和 ListView。 使用 BaseAdapter 而不是 ArrayAdapter。其实是一样的,只是我不调用base.getView():

private class StudentListAdapter : BaseAdapter
{
    private IList<Student> items;
    private Context outer_context;

    public StudentListAdapter(Context context, IList<Student> items)
    {
        this.items = items;
        this.outer_context = context;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater vi = (LayoutInflater)outer_context.GetSystemService(Context.LayoutInflaterService);
        View v = vi.Inflate(Resource.Layout.ItemStudent, null);

        Student s = items[position];

        v.FindViewById<TextView>(R.id.text_view_id).setText(s.Name);

        // determine correct risk image
        ImageView listRisk = v.FindViewById<ImageView>(Resource.Id.listRisk);
        if (s.R != null)
        {
            switch ((int)s.R.Value)
            {
                case 1:
                    listRisk.SetImageResource(Resource.Drawable.risk_green);
                    break;
                case 0:
                    listRisk.SetImageResource(Resource.Drawable.risk_yellow);
                    break;
                case -1:
                    listRisk.SetImageResource(Resource.Drawable.risk_red);
                    break;
                case -2:
                    listRisk.SetImageResource(Resource.Drawable.risk_red2);
                    break;
                default:
                    listRisk.SetImageResource(Resource.Drawable.risk_gray);
                    break;
            }
        }
        else
        {
            listRisk.SetImageResource(Resource.Drawable.risk_gray);
        }

        return v;
    }

    void displayNewData(IList<Student> new_items)
    {
        items=new_items;
        notifyDatasetChanged();
    }
}

因此,当过滤器文本发生变化时,您只需使用 displayNewData() 方法将新数据放入适配器即可:

public class StudentsActivity extends Activity {

    public override void onCreate(Bundle savedInstanceState) 
    {
        ...
        filterEditText.addTextChangedListener(filterTextWatcher);
    }

    private TextWatcher filterTextWatcher = new TextWatcher() 
    {

        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {}

        public void onTextChanged(CharSequence filter, int start, int before, int count) 
        {
            IList<Student> items = getStudentsFiltered(filter);
            adapter.displayNewData(items);
        }
    };

    //getStudentsFiltered() method may look like that:
    getStudentsFiltered(String filter)
    {
        IList<Student> students_filtered=new List<Students>();
        foreach(Student student in students)
        if(student.Name.Contains(filter))
                students_filtered.Add(student);
        return students_filtered;
    }
}

我很确定这种方法可以正常工作。随时提出任何问题。
我没有编译代码,所以可能会有一些小错误。

【讨论】:

  • getStudentsFiltered() 函数是什么?
  • filter 是在过滤器 EditText 中输入的过滤器字符串。 getStudentsFiltered() 方法返回按此过滤器字符串过滤的学生。例如,如果字符串是“j”,getStudentsFiltered() 将返回所有包含“j”的学生。你应该写这个方法。所以由你来实现过滤器逻辑。
  • 感谢您的提示,引导我找到我在问题中发布的最终答案。
  • @Fedor:你能看看我的问题吗:stackoverflow.com/q/10764675/450534 ??我没有办法试图解决这个问题,它正在使用你的延迟加载技术。很抱歉以这种方式问你。
【解决方案2】:

我注意到的一件事(在使用 Java 而不是 Mono 编码时)是,如果您尝试执行以下操作:

imageView.setImageResource(R.myResourceID1);
imageView.setImageResource(R.myResourceID2);

显示的图像将始终是 myResourceID1。为了解决这个问题,当您想要设置已设置的 imageView 的资源时,与我一起工作的人建议使用此代码。所以:

imageView.setImageResource(R.myResourceID1);
imageView.setImageResource(0);  //supposedly invalidates the image resource
imageView.setImageResource(R.myResourceID2);

虽然在阅读您的更新后,如果您在 getView() 方法中设置 imageViews 后进行过滤,这可能不是您的问题。我想我会分享,因为通过 getView() 第二次设置已经使用的 imageView 的资源听起来像我上面描述的问题。

【讨论】:

    【解决方案3】:

    检查 tmpView 和 v。我没有 Mono 的东西,所以我无法查看 ArrayAdapter 的实现。

    我已经看到问题的出现,因为我的视图最终在我做人口数据之前被添加到父级中。 GetView() 的默认实现会将视图添加到父视图并返回父视图,而不是新膨胀的视图。在父级上执行 findViewById() 总是返回相同的 ImageView(可能是列表中的第一个),而不是我新膨胀的视图中的 ImageView。

    如果是这种情况,请不要将 parent 传递给 base.GetView(),而是传递 null。在 GetView() 实现结束时将新膨胀的视图添加到父视图。

    【讨论】:

    • 我试一试,不知道您所说的“在 GetView() 实现结束时将新膨胀的视图添加到父视图。”
    • 已经有一段时间了,所以我有点模糊。也许添加不是必需的。我绝对知道有时您会返回父 ViewGroup,这有点不明显。
    • 如果没有基本实现,我不能肯定地说。首先,我想知道你是否必须打电话给基地。我的适配器都没有这样做,但它们都在 Java 中。如果您不必调用它,那么您当然会使用 tmpView。如果您必须调用它,那么这意味着它会执行某些操作并向您返回修改后的 View 或(我的猜测)带有您的 View 的父 ViewGroup。在这种情况下,我会尝试将 null 传递给 parent 参数,然后使用 v(基本返回值)。
    • 你不需要调用base,但是这样做会保留默认的过滤行为。
    • 在帖子末尾更新了新发现
    【解决方案4】:

    尝试实例化ImageView listRisk = v.FindViewById&lt;ImageView&gt;(Resource.Id.listRisk),然后在每种情况下设置资源。

    【讨论】:

    • 在帖子末尾更新了新发现
    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2015-11-11
    • 2013-03-17
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    相关资源
    最近更新 更多