我的程序从存储在 SD 内存和图形中的 txt 文件中读取数据。但它只使该过程一次,当我想选择另一个 txt 并再次按下图形按钮时,它不起作用。我使用 EsFileExplorer 来选择文件。您能找到任何建议或错误吗?
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;
}
}