2014-08-30 41 views
0

我想顯示一個使用自定義適配器的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; 

    } 
+0

爲什麼如果你不使用它,你的else語句中有'holder =(Holder)convertView.getTag();'?如果它不爲null,你也不會更新你的convertView。 – hoomi 2014-08-30 10:16:49

+0

我有複製粘貼它..我會刪除它。 – 2014-08-30 17:46:43

回答

0

我經歷了同樣的問題去前一陣子。在我的情況下,我必須給圖表視圖一個固定的高度。 您還可以給圖形視圖的父母之一提供固定高度,然後在圖形視圖上使用match_parentwrap_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希望儘量減少其項目的高度。如果這是真的,你的圖表就在那裏,但它的高度爲零。

+0

謝謝..它的作品:) – 2014-09-16 16:59:31

+0

我很高興它的作品。 :) 你介意標記我的答案是正確的嗎? :) – Adogeon 2014-09-16 20:27:10