0

ダイアログ内にタイトルとリストビューとして2つの画像を含むカスタムダイアログを作成しています。そのリストビューには、表示しているアイテムがいくつかありますが、リストビューのアイテムの背景を変えたいですか?リストビューに表示するアイテムが5つあるとすると、位置1、3、5のアイテムの背景は位置2、4のアイテムとは異なります。出来ますか?助けて

4

2 に答える 2

0

リストビューにアイテムをロードするときに、カスタム アダプターを使用できます。

リストビュー内のアイテムのレイアウト xml を作成して設定します。

myitemlist_row.xml のように、アクティビティで:

MyAdapter _adapter = new MyAdapter(getActivity().getApplicationContext(), R.layout.myitemlist_row, null, uiBindFrom, uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER, this);
_mylist.setAdpater(_adapter);

アダプタのサンプル コード:

@Override 
public void bindView(View view, Context context, Cursor cursor) 
{ 
    super.bindView(view, context, cursor); 

    ViewHolder holder = (ViewHolder)view.getTag(); 

    if (holder == null) 
    { 
        holder = new ViewHolder(); 
        holder.Background_Layout = (LinearLayout) view.findViewById(R.id.Background_Layout);
    }

    //you can use now holder.Background_Layout.setBackgroundColor or setBackgroundDrawable accordind to the data, as you want

それが役に立てば幸い :-)

于 2012-07-31T09:11:04.027 に答える
0

アダプターを使用していると仮定して、これを試してください...

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null){
        vi = inflater.inflate(R.layout.<item_layout>, null);

    }
    else
    if(pos%2==0)
         vi.setBackgroundResource(<image_1_res_id>);
    else
         vi.setBackgroundResource(<image_2_res_id>);
    return vi;
}
于 2012-07-31T09:12:01.730 に答える