0

I have a class DrawView.java which draw a rectangle and I want to make a ListView of rectangle. I tried to draw the rectangle in getView() method of List adapter but I failed.

Can anyone help me in this issue?

My class which draw the rectangle is:

    public class DrawView extends View {
    Paint paint = new Paint();

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

    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        canvas.drawRect(30, 30, 180, 180, paint);
        paint.setStrokeWidth(0);
        paint.setColor(Color.CYAN);
        canvas.drawRect(33, 60, 177, 177, paint );
        paint.setColor(Color.YELLOW);
        canvas.drawRect(33, 33, 177, 60, paint );

    }

}

and my getView() method of custom adapter is:

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

        convertView = mInflater.inflate(R.layout.news_list_item,null);
        DrawView rl = (DrawView)convertView.findViewById(R.id.drawview);
        DrawView draw = new DrawView(c);
        rl=draw;
        return convertView;
}

My XML file is news_list_item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="60dp"
    android:id="@+id/mainLayout"
     >

     <com.example.horizontalscrollview.DrawView
        android:id="@+id/drawview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
      </com.example.horizontalscrollview.DrawView>



</LinearLayout>
user3265443
  • 495
  • 1
  • 7
  • 23
  • 1
    I think you need to define a layout for item. This layout will contain your DrawView, and you will inflate layout in your getView method – Liem Vo Apr 09 '14 at 14:22
  • @LiemVo I updated my getView() function but still getting nothing on my screen. Can you help me out – user3265443 Apr 10 '14 at 06:54
  • Please try this tutorial how to customer base adapter for your list view http://stackoverflow.com/questions/16333754/how-to-customize-listview-using-baseadapter – Liem Vo Apr 10 '14 at 06:59
  • They are defining their custom layout in xml file and I am defining dynamically. SO I think there is no need to inflate. Here 'c' is my Context object from MainActivity. Even now I am not figuring out my mistake – user3265443 Apr 10 '14 at 07:06
  • I am not getting any rectangles on my screen and my application is crashing – user3265443 Apr 10 '14 at 07:13
  • Can you show me your crash message – Liem Vo Apr 10 '14 at 07:33
  • I updated my getView() and xml file. The application is crashing by inflateException – user3265443 Apr 10 '14 at 07:45
  • Please post crash message – Liem Vo Apr 10 '14 at 07:49
  • android.view.InflateException: Binary XML file line #9: Error inflating class com.krish.horizontalscrollview.DrawView at android.view.LayoutInflater.createView(LayoutInflater.java:596) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at com.krish.horizontalscrollview.CustomListAdapter.getView(CustomListAdapter. – user3265443 Apr 10 '14 at 07:58
  • (CustomListAdapter.java:109) at com.krish.horizontalscrollview.CenterLockHorizontalScrollview.fillViewWithAdapter(CenterLockHorizontalScrollview.java:49) at com.krish.horizontalscrollview.CenterLockHorizontalScrollview.setAdapter(CenterLockHorizontalScrollview.java:28) 0at com.krish.horizontalscrollview.HorizontalScrollViewActivity.onCreate(HorizontalScrollViewActivity.java:68) at android.app.Activity.performCreate(Activity.java:5158) 0 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:21 – user3265443 Apr 10 '14 at 08:00
  • There's something wrong with your inflation clearly. Can you post the part of the LogCat that starts with 'Caused By: ', also I think you need only initialize convertView if it is null. Once that is done, call `convertView.invalidate()` – W.K.S Apr 10 '14 at 08:11
  • 04-10 13:42:59.276: E/AndroidRuntime(26265): Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet] 04-10 13:42:59.276: E/AndroidRuntime(26265): at java.lang.Class.getConstructorOrMethod(Class.java:460) 04-10 13:42:59.276: E/AndroidRuntime(26265): at java.lang.Class.getConstructor(Class.java:431) 04-10 13:42:59.276: E/AndroidRuntime(26265): at android.view.LayoutInflater.createView(LayoutInflater.java:561) – user3265443 Apr 10 '14 at 08:13
  • Do you make sure DrawView is properly? – Liem Vo Apr 10 '14 at 08:25
  • Ya, when I am calling the DrawView from MainActivity, it is displaying the rectangle – user3265443 Apr 10 '14 at 08:28
  • In class DrawView, onDraw() function takes the top left and bottom right coordinates of rectangle to draw it. But in listView the points where rectangles is to draw are changing. So is there any problem in this? – user3265443 Apr 10 '14 at 08:43

1 Answers1

1

You need to create a list adapter by extending BaseAdapter. Your getView method is not properly using the convertView parameter. I recommend you watch The World of ListView, which covers proper use of ListView.

UPDATE

  1. Do not inflate a layout every time; only do so when convertView is null. The whole point of ListView is to recycle its views when they scroll off screen.
  2. I see no reason for you to create a new instance of DrawView in getView(). It should be in the XML layout you are inflating.
  3. The statement r1=draw; will not change the layout of the row in any way. All you've done is change the reference r1 to point to a different View object (the new DrawView), but that view is not actually attached to the layout in any way. If you want to add it to the layout, you need to call addView() on some other view which you intend to be the container for the new DrawView.

If you haven't done so yet, please watch The World of ListView (link above) and follow the coding patterns presented.

Karakuri
  • 36,506
  • 12
  • 75
  • 103
  • I updated my getView() function of my custom adapter. Can you help me out, I am still getting nothing on my screen. – user3265443 Apr 10 '14 at 06:55