gpt4 book ai didi

java - 我如何获取在另一个类的方法中获取值的数组

转载 作者:行者123 更新时间:2023-12-01 11:38:24 24 4
gpt4 key购买 nike

在mainActivity中我制作了计时器,您每3秒单击按钮显示可用内存的值并写入数组。写入完成后,第二个按钮“查看图表”保持可单击状态,我们单击并转换为下一个 Activity (第二课)。 在第二堂课中创建带有线条的 Canvas ,并且必须创建可以及时显示可用内存的图表。 但是我不能将第一个类中的数组带到第二堂课。

我需要在 run() 方法中将数组“Masiv”传送到我的第二个类(class)。

头等舱

package foxstrot.p3;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;


public class MainActivity extends ActionBarActivity {
private Timer timer;
private MyTimerTask myTimerTask;
private Intent i;
long[] Masiv = new long[50];
int k;


private long freeMemory;

private Canvas canvas;
private G.DrawView d;

private int t = 0;
private long j;
private TextView tv;
private long r;
private Button b2;
MyTimerTask myTimerTask1 = new MyTimerTask();
int ii = 0;
ArrayList <Integer> AL = new ArrayList<Integer>() ;





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
b2 = (Button)findViewById(R.id.b2);


b2.setClickable(false);
//myTimerTask1.getMas();


//actManager.getMemoryInfo(memInfo);
//freeMemory = memInfo.availMem;
//tv.setText("freeMemory: " + freeMemory);
//Canvas canvas = new Canvas();
/*G g = new G();
G.DrawView d = g.new DrawView(this);*/






}
public void start(View v) {

timer = new Timer();
myTimerTask = new MyTimerTask();
timer.schedule(myTimerTask, 3000, 3000);



}

public void chart(View v){
i = new Intent(this,G.class);
startActivity(i);
}







@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}



class MyTimerTask extends TimerTask {




@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
long getFreeMemory(){
ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
freeMemory = memInfo.availMem;
return freeMemory;
}


runOnUiThread(new Runnable() {

@Override
public void run() {



r = getFreeMemory();
k = (int)r/100000;
//setMas(k)
AL.add(k);

tv.setText("Free memory : " + k);
t++;
if(t == 5){
cancel();
b2.setClickable(true);

}






}
});
}

public void setMas(long p){

//if(ii==5){}

//Log.d("LOG_TAG", "SetMy_Array[0]: " + k + "SetMy_Array[1]: " + k);
ii++;
}






/*public long getMas(){
Log.d("LOG_TAG", "GetMy_Array[0]: " + k + "GetMy_Array[1]: " + k);
return k;

}*/
}

}

二等

package foxstrot.p3;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/**
* Created by Admin on 14.04.2015.
*/

public class G extends Activity {

int ii;




private long[] My_Array ;
private MainActivity mainActivity = new MainActivity();
//private MainActivity.MyTimerTask myTimerTask = mainActivity.new MyTimerTask();
//private long[] G_mas = myTimerTask.getMas();
ArrayList<Integer> AL = new ArrayList<Integer>() ;
Intent intent;


private boolean drawGraph = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawView(this));
intent = getIntent();

//AL = intent.getIntegerArrayListExtra("Joker");

//Log.d("LOG_TAG","Al: " + AL.get(0) + "AL: " + AL.get(0));


//My_Array = myTimerTask.getMas();
//Log.d("LOG_TAG","My_Array[0]: " + myTimerTask.getMas() + "My_Array[1]: " + myTimerTask.getMas());

/*for(int i=0;i<G_mas.length;i++){
max = Math.max(max,G_mas[i]);
min = G_mas[i];
min = Math.min(min, G_mas[i]);
}*/





}


public class DrawView extends View{
Paint p;
Rect rect;
int uN = 0;
int uT = 0;
int y1 = 0;
int x1 = 0;
int n = 2000;
int t = 0;





public DrawView(Context context){
super(context);
p = new Paint();
rect = new Rect();
}

@Override
public void onDraw(Canvas canvas) {

final int cHeight = canvas.getHeight();
final int cWidth = canvas.getWidth();


Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
paint.setTextSize(30);
canvas.drawLine(80,0,80,cHeight-60,paint);
canvas.drawLine(80,cHeight-60,cWidth,cHeight-60,paint);
for(int i=0;i<50;i++){
canvas.drawText(uT + "" , 10 , 25 + y1, paint);

uT = uT + 3;
y1 = y1 + (cHeight/50);
}

for(int i=0;i<9;i++){
canvas.drawText("|"+ uN, 75 + x1, cHeight - 25 , paint);
uN = uN + 1000;
x1 = x1 + (cWidth/9)-25;

/*int joker = 10;
canvas.drawPoint(20+ joker,20,paint);
joker = joker + 40;*/
}
paint.setStrokeWidth(10);
/*for(int i=0;i<My_Array.length;i++){

long xc = (My_Array[i] * (cWidth-80)) / 8000 ;
int yc = (t * (cHeight-60))/147;
t = t + 3;
canvas.drawPoint(xc, yc, paint);
Log.d("LOG_TAG","xc: " + xc + "yc: " + yc);

}*/



/*Paint paint2 = new Paint();
paint2.setColor(Color.RED);
canvas.drawPoint(cWidth, cHeight, paint2);*/







}


}




}

最佳答案

只需使用putExtra()getLongArrayExtra()即可。

文档:putExtra(String name, long[] value)getLongArrayExtra(String name)

在MainActivity.java中:

public void chart(View v){
i = new Intent(this,G.class);
i.putExtra("foxstrot.p3.masiv", Masiv);
startActivity(i);
}

在 G.java 中:

private boolean drawGraph = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawView(this));
intent = getIntent();

My_Array = intent.getLongArrayExtra("foxstrot.p3.masiv");
//...................

关于java - 我如何获取在另一个类的方法中获取值的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29764123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com