我将一些数据修改为asynctask。我正在使用androdidplot,因为我希望看到我在代码中生成的点,但我正在努力,因为我看不到它们。
我在此链接中检查了问题:Access UI elements from Asynctask 它指出将 XYPlot 和 LineAndPointFormatter 声明为类的成员并将它们初始化为onCreate。之后,我只是从onPostExecute绘图。我检查了LOG是否正在生成数据并且没有问题。有人可以帮我或给我一个线索吗?
这是我的代码:
public class MainActivity extends ActionBarActivity {
Button startStopButton;
boolean started = false;
TextView tv1; //not ussed so far
GenerateSamples samplesTask;
XYPlot plot;
ArrayList<Integer> Vector = new ArrayList<Integer>();
public int DataY;
XYSeries series ;
LineAndPointFormatter seriesFormat ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startStopButton = (Button) this.findViewById(R.id.StartStopButton);
tv1 = (TextView) findViewById(R.id.tv1); //Not used so far
plot = (XYPlot) findViewById(R.id.Grafica);
plot.setDomainStep(XYStepMode.INCREMENT_BY_VAL,20); //x
plot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 5000); //y
plot.getGraphWidget().getGridBackgroundPaint().setColor(Color.rgb(255, 255, 255));
plot.getGraphWidget().getDomainGridLinePaint().setColor(Color.rgb(255, 0, 0) );
plot.getGraphWidget().getRangeGridLinePaint().setColor(Color.rgb(255, 0, 0));
plot.setRangeBoundaries(-20000, 20000, BoundaryMode.FIXED); //y
plot.setDomainBoundaries(0, 100, BoundaryMode.FIXED); //x
series = new SimpleXYSeries( Vector, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "signal") ;
seriesFormat = new LineAndPointFormatter(Color.rgb (127, 255, 0 ), 0x000000, 0x000000,null ) ;
/** Listener when the button is clicked. */
startStopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (started) {
started = false;
startStopButton.setText("Start");
samplesTask.cancel(true);
} else {
started = true;
startStopButton.setText("Stop");
samplesTask = new GenerateSamples();
samplesTask.execute();
}
}
});
}
/**
* Called when the button is pressed
*/
public class GenerateSamples extends AsyncTask<Void, Integer, Void > {
public int[] generateRandom() {
int[]random = new int [20] ;
Random rand = new Random();
for (int i = 0; i <= random.length-1; i++)
//random[i]= rand.nextInt(32761);
random[i] = rand.nextInt(32761 + 1 + 32762) - 32761 ;
//random[i] = rand.nextInt(30 + 1 +30) - 30 ;
return random;
}
@Override
protected Void doInBackground(Void... arg0) {
//It returns a boolean. It needs a void = nothing to do with the task.
//It can get data from a method into the class without the need of getting it from the argument
//of the asynctask class.
int[] point ;
point = generateRandom();
for (int i = 0; i <= point.length-1; i++) {
point[i] = point[i]+5;
longTask();
publishProgress(point[i]);
if( isCancelled() )
break;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
//It returns a void = it returns nothing. It needs an integer to do the task ;
DataY= values[0];
Vector.add(DataY);
Log.d("Samples log", "samples: " + DataY );
}
@Override
protected void onPostExecute(Void result) {
plot.addSeries(series,seriesFormat) ;
Toast.makeText(MainActivity.this, "Finished Task!",
Toast.LENGTH_SHORT).show();
}
@Override
protected void onCancelled() {
Toast.makeText(MainActivity.this, "Cancelled Task!",
Toast.LENGTH_SHORT).show();
}
}
private void longTask() {
try {
Thread.sleep(100); //100 milliseconds
} catch (InterruptedException e) {
}
}
}
这是xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ap="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:weightSum="1"
>
<com.androidplot.xy.XYPlot
android:id="@+id/Grafica"
android:layout_width="fill_parent"
android:layout_height="400dp"
androidPlot.graphWidget.marginLeft="20dp"
ap:rangeTickLabelTextSize="11sp"
ap:domainTickLabelTextSize="11sp"
ap:rangeOriginTickLabelTextSize="11sp"
ap:domainOriginTickLabelTextSize="11sp"
/>
<Button android:text="Start"
android:id="@+id/StartStopButton"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
</Button>
<TextView android:text="" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1" />
</RelativeLayout>
答案 0 :(得分:0)
在OnPostExecute中,试试这个:
{{1}}