0

I am having a custom layout for the list fragment. Inside that custom list layout, i want to put another custom view (which do drawing things). So my question is: how to add another view to convertView in method getView(). Here is my getView method:

private class TrainingAdapter extends ArrayAdapter<Product>
{

    public TrainingAdapter(ArrayList<Product> list) {
        super(getActivity(), 0, list);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
        {
            convertView = getActivity().getLayoutInflater().inflate(R.layout.trainings_custom_layer_listfragment, null);
        }

        Product p = getItem(position);
        //references to text view and image view goes here
        //begin to work with custom view
        StarView sv = (StarView) convertView.findViewById(R.id.star_view);
        return convertView; }

Here is my layout:

<ImageView android:id="@+id/imageview_product_category"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="mini photo" />

<TextView android:id="@+id/textview_training_title"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:textColor="@color/text_color_white" />

<TextView android:id="@+id/textview_training_location"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_title"
    android:textColor="@color/text_color_white" />

<TextView android:id="@+id/textview_training_datetime"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_location"
    android:textColor="@color/text_color_white" />

<TextView android:id="@+id/textview_training_cost"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_datetime"
    android:textColor="@color/text_color_white" />

<TextView android:id="@+id/textview_training_registrationenddate"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_cost"
    android:textColor="@color/text_color_white" />

<jacky.practice.view_custom.StarView 
    android:id="@+id/star_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/imageview_product_category"
    android:layout_below="@id/textview_training_registrationenddate"/>

Code for custom view:

public class StarView extends View {
private Paint myPaint;
private int mLevel = 0;

public StarView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public StarView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public StarView(Context context) {
    super(context);
    init();
}

private void init()
{
    myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    myPaint.setColor(Color.GREEN);
}

public void setExpensive(int level)
{
    mLevel = level;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(mLevel <= 3)
    {
        canvas.drawCircle(20, 20, 8, myPaint);
    }
    else if((mLevel > 3) && (mLevel <= 5))
    {
        canvas.drawCircle(20, 20, 8, myPaint);
        canvas.drawCircle(40, 20, 8, myPaint);
    }
    else
    {
        canvas.drawCircle(20, 20, 8, myPaint);
        canvas.drawCircle(40, 20, 8, myPaint);
        canvas.drawCircle(60, 20, 8, myPaint);
    }

    canvas.restore();
}

}

ngunha02
  • 1,719
  • 1
  • 19
  • 24
  • What exactly is the problem here? I see that you have StarView in your xml layout. And that you find it in `convertView`. Is the custom view not showing up? Is there an exception being thrown? – Vikram Sep 07 '13 at 23:53
  • Sorry for not being clear about the problem. The custom view is not showing up. My first thought was because it is another view so it can not be added into another view that way so i tried to you parent.addView() but it did not work. – ngunha02 Sep 08 '13 at 01:44
  • Adding the view (custom or otherwise) is not required. Specifying it in xml layout should be sufficient. Does the xml you posted reflect the exact contents of `trainings_custom_layer_listfragment.xml`? Or did you leave parts of it out? – Vikram Sep 08 '13 at 02:27
  • It actually reflects the exact contents. When i view the xml layout in graphical view, it does show the circle but when i run it in emulator, nothing does show. – ngunha02 Sep 08 '13 at 02:40
  • If it shows the exact contents, where's your container layout? What's holding an ImageView, 5 TextViews and your custom view? They cannot co-exist in open space. Let me ask you another thing. I understand that StartView isn't showing. But what is? Is anything showing at all in the list items? – Vikram Sep 08 '13 at 02:49
  • there is a relative layout outside of imageview, 5 textviews and a custom view. When i run the activity on emulator, itt only shows the imageview, 5 textviews, but the custom view. When i change my custom view class to extends TextView, and put setText("something"), the circle is drawn on top of the text something. – ngunha02 Sep 08 '13 at 03:07
  • Can you post the code for `StarView`? StarView does extend View currently, correct? – Vikram Sep 08 '13 at 06:02
  • I just update the code for custom view. And yup, it does. – ngunha02 Sep 08 '13 at 06:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37002/discussion-between-user2558882-and-jacky-nguyen) – Vikram Sep 08 '13 at 06:26

1 Answers1

0

Have you forgotten to implement onCreateView?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.main, null);
    return view;
}
  • I should make it more clear on my question that i am create a custom adapter. I am going to update it now. – ngunha02 Sep 07 '13 at 20:27