我正在尝试使用自定义适配器在listview中显示一个Chartengine饼图,它正在执行而没有任何错误但不显示图表..请帮助。
我有一个方法openChart()..这是创建图表和返回视图,我通过持有人在获取视图中设置。
代码
public View getView(int position, View convertView, ViewGroup arg2) {
.....
convertView = mInflater.inflate(R.layout.chart,null);
holder.vw=(ViewGroup) convertView.findViewById(id.chart_container);
//Log.w("In View", );
holder.vw.addView(openChart());
convertView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
return convertView;
}
....
private View openChart(){
// Pie Chart Section Names
String[] code = new String[] {
"Eclair & Older", "Froyo", "Gingerbread", "Honeycomb",
"IceCream Sandwich", "Jelly Bean"
};
// Pie Chart Section Value
double[] distribution = { 3.9, 12.9, 55.8, 1.9, 23.7, 1.8 } ;
// Color of each Pie Chart Sections
int[] colors = { Color.BLUE, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.RED,
Color.YELLOW };
// Instantiating CategorySeries to plot Pie Chart
CategorySeries distributionSeries = new CategorySeries("PIe");
for(int i=0 ;i < distribution.length;i++){
// Adding a slice with its values and name to the Pie Chart
distributionSeries.add(code[i], distribution[i]);
}
// Instantiating a renderer for the Pie Chart
DefaultRenderer defaultRenderer = new DefaultRenderer();
for(int i = 0 ;i<distribution.length;i++){
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
seriesRenderer.setColor(colors[i]);
seriesRenderer.setDisplayChartValues(true);
// Adding a renderer for a slice
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
defaultRenderer.setChartTitle("Android version distribution as on October 1, 2012 ");
defaultRenderer.setChartTitleTextSize(20);
defaultRenderer.setZoomButtonsVisible(true);
defaultRenderer.setBackgroundColor(Color.GRAY);
defaultRenderer.setDisplayValues(true);
// Getting a reference to LinearLayout of the MainActivity Layout
//LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
// Creating a Line Chart
mChart = ChartFactory.getPieChartView(mContext, distributionSeries , defaultRenderer);
// Adding the Line Chart to the LinearLayout
//chartContainer.addView(mChart);
// Creating an intent to plot bar chart using dataset and multipleRenderer
// Intent intent = ChartFactory.getPieChartIntent(getBaseContext(), distributionSeries , defaultRenderer, "AChartEnginePieChartDemo");
// Start Activity
// startActivity(intent);
Log.w("In Chart", mChart.toString() +"");
return mChart;
}
答案 0 :(得分:0)
我刚才经历过同样的问题。在我的情况下,我必须给图表视图一个固定的高度。
您还可以为图表视图的父级之一指定固定高度,然后在图表视图中使用match_parent
或wrap_content
。
看我的xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="88dp">
<LinearLayout
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="@dimen/list_item_margin"
android:layout_marginRight="@dimen/list_item_margin"
android:layout_alignBottom="@id/image"
android:layout_alignTop="@id/image"
android:layout_toRightOf="@id/image"/>
<!-- other views... -->
</RelativeLayout>
这可能是因为ListView希望最小化其项目的高度。如果这是真的,那么你的图表就在那里,但它的高度为零。