简体   繁体   English

将数据传递到Android中的自定义视图

[英]Passing data to custom view in Android

I'm fairly new to Android and am trying to create a custom view to display a pie chart. 我刚接触Android,正在尝试创建自定义视图以显示饼图。 I'm OK with Java and have written the pie chart code in iOS/Swift before. 我可以使用Java,并且之前已在iOS / Swift中编写了饼图代码。

My problem is that the data doesn't appear to get passed as I had expected, or perhaps that the onDraw function is getting called before the data is set somehow from the superclass. 我的问题是数据似乎没有按照我的预期传递,或者在从超类以某种方式设置数据之前调用了onDraw函数。

I'd like to know the best method for passing data to be used in the onDraw function. 我想知道传递数据的最佳方法,以便在onDraw函数中使用。 So far, with a bit of help from posts like Android Custom View Constructor I've got the following code: 到目前为止,在Android Custom View Constructor之类的帖子的帮助下,我得到了以下代码:

public PieChart(Context _context) {
    this(_context, null);
}
public PieChart(Context _context, AttributeSet _attrs) {
    this(_context, _attrs, null);
}
public PieChart(Context _context, AttributeSet _attrs, ArrayList<ChartData> _data) {
    super(_context, _attrs);
    if(this.isInEditMode()) {
        data = new ArrayList<ChartData>();
        data.add(new ChartData("Column A", 50));
        data.add(new ChartData("Column B", 75));
        data.add(new ChartData("Column C", 60));
    } else {
        data = _data;
    }
    context = _context;
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    pieRectangle = new RectF(0,0,0,0);
    ...
}

Which is called from my activity like this: 从我的活动中这样调用:

// create the chart
PieChart pieChart = new PieChart(this, null, categoriesChartData);
// add the chart to the linearLayout
linearLayout.addView(pieChart);

And my PieChart.onDraw starts like this: 我的PieChart.onDraw开始像这样:

protected void onDraw(Canvas canvasChart) {
    Log.d(TAG, "Size of data = " + data.size()); // crashes here
    ...
}

Note that in the interface designer, the pie chart looks great, so I know the onDraw method works, it's simply that the data isn't making it there when the app is ran. 请注意,在界面设计器中,饼形图看起来很棒,所以我知道onDraw方法有效,只是因为在运行应用程序时数据不在此处。

Rather than adding in extra functions and calls, as I don't have a lot of experience in Android, I'd like to ask the community what the problem is and what the best approach would be here. 我不想增加额外的功能和调用,因为我在Android方面没有很多经验,而是想向社区询问问题出在哪里以及最好的方法是什么。

Many thanks in advance! 提前谢谢了!

Since you mentioned interface designer, I assume that you also added your view to XML layout. 既然您提到了界面设计器,那么我假设您还将视图添加到了XML布局中。 In that case Android View framework will use this constructor (by convention): 在这种情况下,Android View框架将使用此构造函数(按照惯例):

public PieChart(Context _context, AttributeSet _attrs)

That is the reason why your data is null . 这就是您的datanull的原因。 For views you have to create a setter method for your data instead of passing it as a constructor parameter. 对于视图,您必须为数据创建一个setter方法,而不是将其作为构造函数参数传递。

Furthermore, if you already declared your view in XML layout, there is no need to create it manually - that will just add yet another view into layout. 此外,如果您已经在XML布局中声明了视图,则无需手动创建它-这只会在布局中添加另一个视图。

Since using the constructor is not the ideal one; 由于使用构造函数不是理想的方法; you can build a method that accepts an array of datas, in your case it is ArrayList<CharData> or List<CharData> . 您可以构建一个接受数据数组的方法,在您的情况下为ArrayList<CharData>List<CharData>

public PieChart(Context _context) {
    this(_context, null);
}
public PieChart(Context _context, AttributeSet _attrs) {
    super(_context, _attrs);
    if(this.isInEditMode()) {
        data = new ArrayList<ChartData>();
        data.add(new ChartData("Column A", 50));
        data.add(new ChartData("Column B", 75));
        data.add(new ChartData("Column C", 60));
    }

    context = _context;
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    pieRectangle = new RectF(0,0,0,0);
    ...
}

protected void onDraw(Canvas canvasChart) {
    if(data != null){
       Log.d(TAG, "Size of data = " + data.size());
       // more codes here
    }
}

public void setValues(List<CharData> d){
   data = d;
}

The final usecase would be like: 最终用例将是:

PieChart pieChart = new PieChart(this, null);
linearLayout.setValues(categoriesChartData);
linearLayout.addView(pieChart);

我认为data为null,因为this.isInEditMode()返回false。您应该先检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM