如何更新列表视图中的图像视图

发布于 2024-12-02 01:35:41 字数 1953 浏览 1 评论 0原文

我正在制作一个包含自定义布局的列表视图。自定义布局内部是一个图像视图。当我使用 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;
    }
}

I'm making a listview that contains a custom layout. Inside the custom layout is an imageview. When I use Listview lv.setonclick listener it provides me with a variable View that I can use to manipulate the drawable like so.
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);

However when I want to make another function I don't have this View listview variable. How do I get it so that I can do the same thing just outside of the onclicklistener? Thanks

EDIT
here is my list view and the adapter

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

脱离于你 2024-12-09 01:35:41

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

Declare one View globally and assign listview to that view and you can use it anywhere.

梦萦几度 2024-12-09 01:35:41

您可以通过这种方式找到您的 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();
}

更多详细信息此处

You can find your ImageView that way:

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;
}

And if you need to know how many entries you have in the list:

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

More details here.

姐不稀罕 2024-12-09 01:35:41

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

You will probably want to write your own BaseAdapter subclass or extend whatever ListAdapter implementation you are using and override getView(). There you can inflate the ListView row and do anything necessary to the ImageView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文