implements ListAdapter : ListAdapter « UI « Android






implements ListAdapter

 
//src\com\commonsware\android\fancylists\seven\AdapterWrapper.java
/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package com.commonsware.android.fancylists.seven;

import android.database.DataSetObserver;
import android.widget.ListAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

public class AdapterWrapper implements ListAdapter {
  ListAdapter delegate=null;
  
  public AdapterWrapper(ListAdapter delegate) {
    this.delegate=delegate;
  }
  
  public int getCount() {
    return(delegate.getCount());
  }
  
  public Object getItem(int position) {
    return(delegate.getItem(position));
  }
  
  public long getItemId(int position) {
    return(delegate.getItemId(position));
  }
  
  public View getView(int position, View convertView,
                      ViewGroup parent) {
    return(delegate.getView(position, convertView, parent));
  }
  
  public void registerDataSetObserver(DataSetObserver observer) {
    delegate.registerDataSetObserver(observer);
  }
  
  public boolean hasStableIds() {
    return(delegate.hasStableIds());
  }
  
  public boolean isEmpty() {
    return(delegate.isEmpty());
  }
  
  public int getViewTypeCount() {
    return(delegate.getViewTypeCount());
  }
  
  public int getItemViewType(int position) {
    return(delegate.getItemViewType(position));
  }
  
  public void unregisterDataSetObserver(DataSetObserver observer) {
    delegate.unregisterDataSetObserver(observer);
  }
  
  public boolean areAllItemsEnabled() {
    return(delegate.areAllItemsEnabled());
  }
  
  public boolean isEnabled(int position) {
    return(delegate.isEnabled(position));
  }
}



//src\com\commonsware\android\fancylists\seven\RateListView.java
/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package com.commonsware.android.fancylists.seven;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListAdapter;
import android.widget.ListView;

public class RateListView extends ListView {
  public RateListView(Context context) {
    super(context);
  }
  
  public RateListView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
                  
  public RateListView(Context context, AttributeSet attrs,
                        int defStyle) {
    super(context, attrs, defStyle);
  }
  
  public void setAdapter(ListAdapter adapter) {
    super.setAdapter(new RateableWrapper(getContext(), adapter));
  }
}



//src\com\commonsware\android\fancylists\seven\RateListViewDemo.java
/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package com.commonsware.android.fancylists.seven;

import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.CompoundButton;
import android.widget.ListView;

public class RateListViewDemo extends ListActivity {
  String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
          "consectetuer", "adipiscing", "elit", "morbi", "vel",
          "ligula", "vitae", "arcu", "aliquet", "mollis",
          "etiam", "vel", "erat", "placerat", "ante",
          "porttitor", "sodales", "pellentesque", "augue",
          "purus"};
  
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    
    setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1,
                        items));
  }
}



//src\com\commonsware\android\fancylists\seven\RateableWrapper.java
/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package com.commonsware.android.fancylists.seven;

import android.content.Context;
import android.database.DataSetObserver;
import android.widget.ListAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RatingBar;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;

public class RateableWrapper extends AdapterWrapper {
  Context ctxt=null;
  float[] rates=null;
  
  public RateableWrapper(Context ctxt, ListAdapter delegate) {
    super(delegate);
    
    this.ctxt=ctxt;
    this.rates=new float[delegate.getCount()];
    
    for (int i=0;i<delegate.getCount();i++) {
      this.rates[i]=2.0f;
    }
  }
  
  public View getView(int position, View convertView,
                      ViewGroup parent) {
    ViewWrapper wrap=null;
    View row=convertView;
                            
    if (convertView==null) {
      LinearLayout layout=new LinearLayout(ctxt);
      RatingBar rate=new RatingBar(ctxt);
      
      rate.setNumStars(3);
      rate.setStepSize(1.0f);
      
      View guts=delegate.getView(position, null, parent);
    
      layout.setOrientation(LinearLayout.HORIZONTAL); 
          
      rate.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.FILL_PARENT));
      guts.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.FILL_PARENT));
      
      RatingBar.OnRatingBarChangeListener l=
                    new RatingBar.OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar,
                                      float rating,
                                      boolean fromTouch)  {
          rates[(Integer)ratingBar.getTag()]=rating;
        }
      };
      
      rate.setOnRatingBarChangeListener(l);
          
      layout.addView(rate);           
      layout.addView(guts);
      
      wrap=new ViewWrapper(layout);
      wrap.setGuts(guts);
      layout.setTag(wrap);
      
      rate.setTag(new Integer(position));
      rate.setRating(rates[position]);
        
      row=layout;        
    }
    else {
      wrap=(ViewWrapper)convertView.getTag();
      wrap.setGuts(delegate.getView(position, wrap.getGuts(),
                                    parent));
      wrap.getRatingBar().setTag(new Integer(position));
      wrap.getRatingBar().setRating(rates[position]);
    }
    
    return(row);
  }    
}



//src\com\commonsware\android\fancylists\seven\ViewWrapper.java
/***
  Copyright (c) 2008-2009 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package com.commonsware.android.fancylists.seven;

import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;

class ViewWrapper {
  ViewGroup base;
  View guts=null;
  RatingBar rate=null;
  
  ViewWrapper(ViewGroup base) {
    this.base=base;
  }
  
  RatingBar getRatingBar() {
    if (rate==null) {
      rate=(RatingBar)base.getChildAt(0);
    }
    
    return(rate);
  }
  
  void setRatingBar(RatingBar rate) {
    this.rate=rate;
  }
  
  View getGuts() {
    if (guts==null) {
      guts=base.getChildAt(1);
    }
    
    return(guts);
  }
  
  void setGuts(View guts) {
    this.guts=guts;
  }
}



//res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.commonsware.android.fancylists.seven.RateListView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/list"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:drawSelectorOnTop="false"
/>

   
  








Related examples in the same category

1.BaseExpandableListAdapter Demo
2.extends BaseExpandableListAdapter
3.A list view example where the data comes from a cursor, and a SimpleCursorListAdapter is used to map each item to a two-line display.
4.A list view example where the data comes from a custom ListAdapter
5.A list view example where the data comes from a custom ListAdapter
6.ListAdapter for use with HiberDroid Criteria Results (Lists).