简体   繁体   中英

Copy data from one object from one class to another boject in another class in Android

I have an application in android in which I'm accessing data from server which is accessed using a separate class LongOperation which extends AsyncTask. Now I get the data in the LongOperation Class, but I'm not able to copy this data in another classes object where I need to use it to store in a sqlitedb. I have tried directly writing the data to sqlitedb, but given that the class LongOperation is not an activity, I cannot pass context in super(new MainActivity(), DATABASE_NAME, null, DATABASE_VERSION); as required by sqlitedb. The object in which data is fetched is accessed using a class library. I call the LongOperation class from button click in my MainActivity class which is also an activity, but the context from here doesn't work. Following are the related excerpts which depict the calling and data fetch procedure.

MainActivity button Press code(in separate file)

import LibPack.UpdateData;
public static UpdateData upd;

public void onClick(View arg0) {
            upd = new UpdateData();
            LongOperation.getUserData = true;
            lo = new LongOperation();
            lo.execute("");
        }

LongOperation class code(in separate file)

import LibPack.UpdateData;

public static class LongOperation extends AsyncTask<String, Void, String> {
    public static UpdateData upd;
    @Override
    protected String doInBackground(String... params) {
        if (getUserData) {
            upd  = *data fetched from server*
        }
}

So how do I return data from this class back to the MainActivity? Or if there is some other way to store the data in a file so that I can access it via other class? The data in object is of string[] format and key value pair. The problem here is caused due to the class LongOperation being a non-activity class, hence there is no way to pass context while using file writer or sharedpreferences.

Edit 1:

MainActivity

package com.sam.vehicle;


import com.example.vehicle.R;
import com.sam.vehicle.CommonData.LongOperation;

import LibPack.UpdateData;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;


public class MainActivity extends Activity implements AsyncResponse{
    Button b;
    public static LongOperation lo = new LongOperation();
    public static String resp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lo.delegate = this;
        b = (Button) findViewById(R.id.btnProceed);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                upd = new UpdateData();
                CommonData.getUserData = true;
                System.out.println("Before execute");**//prints fine**
                lo.execute("");
                System.out.println("At main extract");**//prints with previous sysout**
                System.out.println("At main extract resp = "+resp);**//prints = null**
                Intent ii = new Intent(arg0.getContext(), LoginActivity.class);
                ii.putExtra("number", phoneNumber.getText().toString());        
                startActivity(ii);
            }
        });
}
    @Override
    public void processFinish(String output) {
        // TODO Auto-generated method stub
        System.out.println("Executed processFinish in main. output = "+output);**//doesn't print
        resp = output;
    }
}

AsyncTask Class:

package com.sam.vehicle;

import com.example.vehicle.R;


public class CommonData {

    public static boolean getUserData = false;
    public static String resp;
        public static class LongOperation extends AsyncTask<String, Void, String> {
            public AsyncResponse delegate = null;

            @Override
            protected void onPostExecute(String result) {
            System.out.println("In commondata onpostexe");**//doesn't print**
              delegate.processFinish(result);
            }
        @Override
        protected String doInBackground(String... params) {

            if (getUserData) {
                sendToServer = false;
                try {
                upd  = (UpdateData) stringToObject(resp);
                for (int i = 0; i < upd.pass.length; i++) {
                    System.out.println("upd.usn["+i+"]"+upd.usn[i]);
                    System.out.println("upd.pass["+i+"]"+upd.pass[i]);
                }**//prints fine here**
                System.out.println("in commondata resp="+resp);**//prints fine here**
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("resp from common return = "+resp);**//doesn't print**
            return resp;
        }
}

Although the code snippet is not optimal way of using AsyncThread, if I have to answer specific to your question then your variable is public static. Why don't you use something like following.

protected String doInBackground(String... params) {
    if (getUserData) {
        MainActivity.upd  = *data fetched from server*
    }

是我见过的最想完成的事情的最佳方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM