0

カスタムレイアウトを含むリストビューを作成しています。カスタムレイアウトの中にはimageviewがあります。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);

ただし、別の関数を作成したい場合は、このViewlistview変数がありません。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;
    }
}
4

3 に答える 3

2

1つのビューをグローバルに宣言し、そのビューにlistviewを割り当てると、どこでも使用できます。

于 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 に答える