【问题标题】:How do I update the imageview inside my listview如何更新列表视图中的图像视图
【发布时间】:2011-08-26 08:54:40
【问题描述】:

我正在制作一个包含自定义布局的列表视图。自定义布局内部是一个图像视图。当我使用 Listview lv.setonclick 侦听器时,它为我提供了一个变量 View,我可以使用它来像这样操作可绘制对象。 lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View listview,
                    int position, long arg3) {
                ImageView pincolor = (ImageView) listview
                        .findViewById(R.id.ivimtrackingpin);
           pincolor.setImageResource(R.drawable.pinred);

但是,当我想创建另一个函数时,我没有这个 View listview 变量。我如何获得它以便我可以在 onclicklistener 之外做同样的事情?谢谢

编辑 这是我的列表视图和适配器

SpecialAdapter adapter = new SpecialAdapter(this, list,
                    R.layout.imtracking_row_text, new String[] { "name",
                            "location" }, new int[] { R.id.tvImtrackingName,
                            R.id.tvImtrackingLocation });
HashMap<String, String> temp = new HashMap<String, String>();
                JSONObject user = tracking_users.getJSONObject(i);
                temp.put("name", user.getString("full_name"));
                // upload location time
                temp.put("location", user.getString("last_updated_at"));
                list.add(temp);
lv.setAdapter(adapter);

public class SpecialAdapter extends SimpleAdapter {
    private int[] colors = new int[]{R.drawable.row_background_grey, R.drawable.row_background_white};

    public SpecialAdapter(Context context,
            ArrayList<HashMap<String, String>> list, int resource,
            String[] from, int[] to) {
        super(context, list, resource, from, to);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        int colorPos = position % colors.length;
        view.setBackgroundResource(colors[colorPos]);
        return view;
    }
}

【问题讨论】:

  • “当我想做另一个函数时”是什么意思?
  • 我的意思是,如果我想做与 onclick 版本相同的事情,而不是当有人点击某些东西时。想象一个名为 setcolor 的函数,它只取一个位置。
  • 好的,我明白了。我会在一秒钟内给出答案。

标签: android view inflate


【解决方案1】:

全局声明一个视图并将列表视图分配给该视图,您可以在任何地方使用它。

【讨论】:

  • 你能解释一下吗?我尝试将 listview lv 添加到顶部,但它没有做任何事情
【解决方案2】:

您可以通过这种方式找到您的ImageView

private ImageView getImageViewAtPosition(ListView listView, int pos) {
    final View      line  = listView.getChildAt(pos); 
    final ImageView image = (ImageView) line.findViewById(R.id.the_id_of_your_image_in_the_xml_layout_of_the_line);

    return image;
}

如果您需要知道列表中有多少条目:

private int getCount(ListView listView) {
    return listView.getChildCount();
}

更多详情here.

【讨论】:

  • 我刚试过这个,我对两件事感到困惑。 listView.getChildCount() 始终为 0。如果我尝试执行 listView.getChildAt(x),它会导致我出现源错误。我的列表视图填充了我将在上面添加的适配器
  • 总是 0?嗯,我不知道为什么到目前为止。我描述的方式是我这边的方式,但我没有那个 0 问题。
【解决方案3】:

您可能想要编写自己的 BaseAdapter 子类或扩展您正在使用的任何 ListAdapter 实现并覆盖 getView()。在那里,您可以对 ListView 行进行扩充并对 ImageView 执行任何必要的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多