-1

I want to create a custom list in which each item consists of a custom view and some other stuff like text views...

What should I do in adapter so that I can show the Custom Views of different coulours in the list ???

Below is the code I have so far

Main.java

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //some data declaration

    ListView lv = (ListView)findViewById(R.id.myList);
    lv.setAdapter(new MyAdapter(this, data, R.layout.item));
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >
 <ListView
  android:id="@+id/myList"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" >
 </ListView>
</LinearLayout>

MyAdapter.java

public class MyAdapter extends BaseAdapter {
   //private variables
   //some functions

   public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    if(convertView == null) vi = inflater.inflate(gif_view);

    View customView = (View)findViewById(R.id.my_custom_view);
    /* what to do here ?? */
    /*                   */
}

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.android.me.MyView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    class="com.android.gif.GIFView"
    android:id="@+id/my_custom_view"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
/*
   some other stuff
*/
</RelativeLayout>

MyView.java

public class MyView extends View{
  int color;

  public MyView(Context context, int color){
    super(context);
    this.color = color;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    canvas.drawColor(color);
    super.onDraw(canvas);

    this.invalidate();
  }
}

Any help would be appreciated. Thanks :)

Hassan Ahmed
  • 168
  • 2
  • 9

1 Answers1

1

Your MyView will be instantiated by the system, so you will have to change the color manually. I added the setColor(int) method.

public class MyView extends View{
  int color;

  public MyView(Context context){
    super(context);
  }

  public void setColor(int color) {
    this.color = color;
    invalidate();
  }


  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(color);

    // DON'T CALL THIS, OR IT WILL LOOP INDEFINITELY
    //this.invalidate();
  }
}



public class MyAdapter extends BaseAdapter {
   public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    if(convertView == null) vi = inflater.inflate(gif_view);

    ((MyView)(vi.findViewById(R.id.my_custom_view)).setColor(randomColor);
}
Simon Marquis
  • 7,248
  • 1
  • 28
  • 43