我想顯示一個使用自定義適配器的listview裏面的一個Chachartengine餅圖,它正在執行沒有任何錯誤但不顯示圖表..請幫助。 我有一個方法openChart()..這是創建圖表和返回視圖,我在通過持有人獲取視圖設置。
代碼使用ListView顯示一個Chartengine Adpater
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;
}
爲什麼如果你不使用它,你的else語句中有'holder =(Holder)convertView.getTag();'?如果它不爲null,你也不會更新你的convertView。 – hoomi 2014-08-30 10:16:49
我有複製粘貼它..我會刪除它。 – 2014-08-30 17:46:43