简体   繁体   English

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

[英]How I can get array which values were get in method of another class

In mainActivity I made timer,which you click in button each 3 seconds show value of available memory and write in array.And after writting finished, the second button "see chart" stay clickable,we click and transwer to the next activity(second class). 在mainActivity中,我设置了计时器,每3秒钟单击一次按钮,将显示可用内存的值并写入数组。写入完成后,第二个按钮“ see chart”保持可单击状态,我们单击并转到下一个活动(第二类) )。 In second class creates canvas with lines, and must be create chart which can show available memory in time. 在第二类中,创建带有线条的画布,并且必须创建可以及时显示可用内存的图表。 But I can't carry array in first class to second class. 但是我不能将数组从头等舱运送到二等舱。

I need carry array "Masiv " at method run() to my second class. 我需要在方法run()中将数组“ Masiv”携带到第二个类。

First 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;

    }*/
}

} }

Second class 二等

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);*/







            }


        }




    }

Just use putExtra() and getLongArrayExtra() . 只需使用putExtra()getLongArrayExtra()

Documentation: putExtra(String name, long[] value) and getLongArrayExtra(String name) 文档: putExtra(字符串名称,long []值)getLongArrayExtra(字符串名称)

In MainActivity.java: 在MainActivity.java中:

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

In G.java: 在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");
        //...................

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用另一个类的 Get 方法来打印这个数组? - How can I use a method Get, of another class to print this array? 如何获取对另一个方法的类引用数组 - How to get an array of class references to another method 如何从另一个类的函数在图上绘制点,该函数返回一个填充有要绘制的值的数组 - How can I plot points on a graph from function in another class which returns an array filled with values to plot 如何从另一个类的静态方法获取抽象类的子类的实例? - How can I get an instance of a subclass of abstract class from a static method of another class? 如何获取已经在另一个 class 中设置的实例值? - How can I get a value of instance which is already set in another class? 如何从另一个数组中的数组中获取数据,该数组在 Java 中存储值? - How can I get the data from an array inside another array that stores values in Java? 如何在我选择的另一个活动(回收站视图)中获取所有值 - How can i get all valuese in an another Activity (Recycler View), whose were chosen by me 如果我设置一个具有从另一个类定义的2个参数的类方法,我如何获得在另一个类上定义的值? - If I set up a class method with 2 parameters defined from the other class, how can I get the values defined on the other class? 如何让我的方法在另一种方法中搜索变量? - How can I get my method to search another method for a variable? 如何从另一个类打印数组和方法 - How can I print an array and a method from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM