【发布时间】: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 的函数,它只取一个位置。
-
好的,我明白了。我会在一秒钟内给出答案。