0

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

3 回答 3

2

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

于 2011-08-26T09:04:43.667 回答
0

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

于 2011-08-26T09:08:20.983 回答
0

你可以找到你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();
}

更多细节在这里

于 2011-08-26T09:38:37.407 回答