2013-07-23 76 views
0

我的程序從存儲在SD存儲器和圖形中的txt文件中讀取數據。但是,只有一次,當我想選擇另一個txt,然後再次按下圖形按鈕時,該過程不起作用。我使用EsFileExplorer選擇文件。任何建議或錯誤,你可以找到?按鈕在achartengine中的圖形問題

XML看起來是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/chart" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.37" 
    android:orientation="vertical" > 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.01" > 

    <Button 
     android:id="@+id/btnGraficar" 
     android:layout_width="140dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Graficar" /> 

    <Button 
     android:id="@+id/btnOpen" 
     android:layout_width="156dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.54" 
     android:text="Abrir Archivo" /> 

</LinearLayout> 

<EditText 
    android:id="@+id/etFilePath" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:focusable="false" 
    android:hint="Nombre del Archivo" > 

    <requestFocus /> 
</EditText> 

活動是

public class LineChart extends Activity { 
public static final String TYPE = "type"; 
private static final String TAG = "MSG"; 

protected static final int REQUEST_CODE_PICK_FILE_TO_OPEN = 1; 


private XYMultipleSeriesDataset mDataset; 
private XYMultipleSeriesRenderer mRenderer = getDemoRenderer(); 
private GraphicalView mChartView; 

private Button btnGraficar; 
private Button btnSeleccionarArchivo; 
private EditText txtArchivo; 

@SuppressWarnings("deprecation") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_line_chart); 
    Log.v(TAG, "Inicio"); 



    btnGraficar = (Button) findViewById(R.id.btnGraficar); 
    btnSeleccionarArchivo = (Button) findViewById(R.id.btnOpen); 
    txtArchivo = (EditText) findViewById(R.id.etFilePath); 

    btnGraficar.setEnabled(false); 
    btnGraficar.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      generarGrafica(); 
     } 



    }); 



    btnSeleccionarArchivo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      openFile(); 
     } 
    }); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

if (resultCode != RESULT_OK || data == null) { 
     return ; 
    } 


    Uri uri = data.getData(); 

    if(uri.toString().length() > 0){ 
     switch (requestCode) { 
      case REQUEST_CODE_PICK_FILE_TO_OPEN: 
       // obtain the filename 
       if (uri != null) { 
        txtArchivo.setText(uri.getPath()); 
        Toast.makeText(this, getString(R.string.open_message) + " " + uri.getPath(), 0).show(); 
       }  
       break; 

     } 


     btnGraficar.setEnabled(true); 

    } 
} 




private void openFile(){ 
    try{ 
     Intent intent = getIntent("com.estrongs.action.PICK_FILE",getString(R.string.open_button)); 
     startActivityForResult(intent, REQUEST_CODE_PICK_FILE_TO_OPEN); 
    } catch (ActivityNotFoundException e) { 
     //Toast.makeText(this, R.string.no_filemanager_installed, 0).show(); 
    } 
} 

private Intent getIntent(String action,String btnTitle){ 

    Intent intent = new Intent(action); 

    if (btnTitle != null) 
     intent.putExtra("com.estrongs.intent.extra.BUTTON_TITLE", btnTitle); 

    return intent ; 
} 


private void generarGrafica(){ 
    setRendererStyling(); 


    mDataset = getDemoDataset(); 

    if (mChartView == null) { 
     LinearLayout layout = (LinearLayout) findViewById(R.id.chart); 
     mChartView = ChartFactory.getLineChartView(this, mDataset, 
       mRenderer); 
     mRenderer.setSelectableBuffer(100); 
     layout.addView(mChartView, new LayoutParams(
       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    } 
     else 
     mChartView.repaint(); 
    } 

private void setRendererStyling() { 

    mRenderer.setAxisTitleTextSize(16); 
    mRenderer.setChartTitleTextSize(20); 
    mRenderer.setXLabels(30); 
    mRenderer.setYLabels(30); 
    mRenderer.setLabelsTextSize(20); 
    mRenderer.setLegendTextSize(20); 
    mRenderer.setXTitle("Tiempo"); 
    mRenderer.setYTitle("Amplitud"); 
    mRenderer.setMargins(new int[] { 20, 30, 15, 0 }); 
    mRenderer.setZoomButtonsVisible(true); 
    mRenderer.setPointSize(20); 
    mRenderer.setShowGrid(true); 
    mRenderer.setGridColor(Color.BLACK); 


} 


private XYMultipleSeriesDataset getDemoDataset() 
{ 
    String archivo = txtArchivo.getText().toString(); 
    ReadFileSDCardActivity r = new ReadFileSDCardActivity(archivo); 
    Log.v(TAG, "Leyo los datos"); 


    double[] seriesFirstY = r.getArreglo(); 

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 

    XYSeries firstSeries = new XYSeries("ECG 1"); 
    for (int i = 0; i < seriesFirstY.length ; i++) 
     firstSeries.add(i, seriesFirstY[i]); 
    dataset.addSeries(firstSeries); 
    Log.v(TAG, "creo el data set"); 


    return dataset; 
} 

private XYMultipleSeriesRenderer getDemoRenderer() { 
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); 
    renderer.setMargins(new int[] { 20, 30, 15, 0 }); 
    XYSeriesRenderer r = new XYSeriesRenderer(); 
    r.setColor(Color.BLUE); 
    //r.setPointStyle(PointStyle.POINT); 

    r.setFillPoints(true); 

    // r.setDisplayChartValues(true);// Que despliegue los numeros 
    // r.setChartValuesTextSize(25); //Tamaño de los numeros 
    renderer.addSeriesRenderer(r); 
    /* r = new XYSeriesRenderer(); 
    r.setPointStyle(PointStyle.POINT); 
    r.setColor(Color.RED); 
    r.setDisplayChartValues(true); 
    r.setChartValuesTextSize(30); 
    r.setFillPoints(true); 
    renderer.addSeriesRenderer(r);*/ 
    renderer.setAxesColor(Color.GRAY); 
    renderer.setLabelsColor(Color.GREEN); 
    Log.v(TAG, "Creo el render"); 

    return renderer; 
} 

}

,並從txt文件中讀取數據的活動

public class ReadFileSDCardActivity { 

public ArrayList<Double> arreglo = new ArrayList<Double>(); 
int contador= 0; 


public ReadFileSDCardActivity(String archivo) 
{ 


    File file = new File(archivo); 

    if(file.exists()) // check if file exist 
    { 
      //Read text from file 
     StringBuilder text = new StringBuilder(); 

     try 
     { 

      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line; 


      while ((line = br.readLine()) != null) 
      { 
       arreglo.add(Double.parseDouble(line)); 
       // text.append(line); 
       //text.append("\n"+ "," + line); 
       contador++; 
      } 

     } 
     catch (IOException e) 
     { 
      //You'll need to add proper error handling here 
     } 
     //Set the text 
     // tv.setText(text); 

    } 
    else 
    { 
     //tv.setText("Sorry file doesn't exist!!"); 
    } 

} 

public double[] getArreglo() { 
    // TODO Auto-generated method stub 
    double[] arreglo1 = new double[arreglo.size()]; 
    for(int i=0 ; i<arreglo.size();i++) 
    { 
     arreglo1[i]= arreglo.get(i); 
// text.append("\n"+ "," + contador); 
    } 

    return arreglo1; 
} 
    } 

回答

0

您在每次按下按鈕時都打電話給getDemoDataset()。這意味着你正在創建一個新的dataset,但你永遠不會告訴你的圖表你創建了一個新的dataset

我建議從第二個按鈕按下,您可以重複使用現有的dataset並修改其內容,而不是創建一個您未使用的新實例。