0

I need to create a layout which consists of a list of rectangles of different width and color. So I decided to create a custom view, which draw a rectangle and create a subclass of CursorAdapter, which will fill the ListView with my rectangles, setting their size and color with the data from Cursor.

I tried to create view in newView() method and add it to root, but it's not working.

Do I need to create some layout and inflate it in newView()? And how should I set the size and color of rectangles?

I read this answer and it has nothing in common with my question. Please read the question before you mark it as answered.

UPDATE

 @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    //View is created programmatically
    //Set color depending on context type.
    View view = LayoutInflater.from(context).inflate(R.layout.record_item_layout, parent, false);
    Log.i(LOG_TAG, "(newView)new view have been created");

    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    Log.i(LOG_TAG, "(bindView) binding to out view");

    int colorId = cursor.getInt(cursor.getColumnIndex(TrackerContract.UserRecordsEntry.COLUMN_CONTEXT_ID));

    RecordView recordView = (RecordView) view;

    //These layout params we need to set height to MAX, and width depending on record duration.
    recordView.setLayoutParams(new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT));
    recordView.setColorId(colorId);
    recordView.invalidate();

    //do something?
}
<?xml version="1.0" encoding="utf-8"?>

<mypackage.RecordView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:id="@+id/record_view"
    custom:colorId="5"/>
Community
  • 1
  • 1
RexSplode
  • 1,475
  • 1
  • 16
  • 24
  • We need to see some code. – Kristy Welsh Nov 16 '15 at 15:12
  • In addition, please explain, in detail, what "it's not working" means. Bear in mind that **you don't "add it to root"** in `newView()` -- `ListView` is responsible for doing that. "Do I need to create some layout and inflate it in newView()?" -- presumably. "And how should I set the size and color of rectangles?" -- do that in `bindView()` – CommonsWare Nov 16 '15 at 15:14
  • depends on your needs but `SimpleCursorAdapter` + `SimpleCursorAdapter.ViewBinder` works good in common cases – Selvin Nov 16 '15 at 15:34

1 Answers1

0

Kind of solved the problem myself. The easiest way inflate the custom view in newView() method, is to create a layout containing that item. In my case

<?xml version="1.0" encoding="utf-8"?>

<mypackage.RecordView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:id="@+id/record_view"
    custom:colorId="5"/>

After that i could change the view size and color in bindView() method.

RexSplode
  • 1,475
  • 1
  • 16
  • 24