1

I need to add LineChart(using MpAndroidChart) dynamically in LinearLayout.

I have declared an arrayList,named list.

 val list = arrayListOf<ABC>()
       ....

 for (i in list) {
   chart[] = LineChart(activity)
 }

What is the value I should put inside [] ? Let say the list's saiz is 2, I need to have 2 chart in LinearLayout.

How should I initialize LineChart?

Example

chart[i] = LineChart(activity) ???

LineChart

public class LineChart extends BarLineChartBase<LineData> implements LineDataProvider {

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

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

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

    @Override
    protected void init() {
        super.init();

        mRenderer = new LineChartRenderer(this, mAnimator, mViewPortHandler);
    }

    @Override
    public LineData getLineData() {
        return mData;
    }

    @Override
    protected void onDetachedFromWindow() {
        // releases the bitmap in the renderer to avoid oom error
        if (mRenderer != null && mRenderer instanceof LineChartRenderer) {
            ((LineChartRenderer) mRenderer).releaseBitmap();
        }
        super.onDetachedFromWindow();
    }
}

2 Answers 2

2

Your question is a little unclear, but I'm following correctly, you can do it in one line like this:

val chart = Array(list.size){ LineChart(list[it]) }

Or:

val chart = list.map{ LineChart(it) }.toTypedArray()

(The latter creates a temporary list, which may be slightly less efficient; but it iterates through the list instead of indexing, which could be faster if the list isn't random-access.)

Sign up to request clarification or add additional context in comments.

7 Comments

There is an error in line LineChart(it). Error:Type mismatch. Required: Context! Found: ABC
Perhaps if you posted a minimal, complete, verifiable example (MCVE), we could write some code to fit in with it…?
I need to have 2 charts. How should I initialize the array?
any idea for this?
That's still just code fragments. But I think the rough equivalent would be fun initializePlayers(int playerCount) = Array(playerCount){ Player(it) }. (Note that both Java and Kotlin conventions start functions and variables with a lower-case letter.) That Array constructor takes two params: a size, and a lambda to construct each array element, given its index. So I think that does all you need.
|
0

This is my answer

for (i in 0 unti list.size) {
   chart[i] = LineChart(activity)
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.