简体   繁体   English

将数据从一个对象从一个类复制到Android中另一个类的另一个项目

[英]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. 我在android中有一个应用程序,我正在其中访问服务器的数据,该数据是使用扩展AsyncTask的单独类LongOperation访问的。 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. 现在,我在LongOperation类中获得了数据,但是我无法将此数据复制到另一个需要使用它存储在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); 我曾尝试直接将数据写入sqlitedb,但鉴于类LongOperation不是活动,因此我无法在super(new MainActivity(),DATABASE_NAME,null,DATABASE_VERSION)中传递上下文; as required by sqlitedb. 根据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. 我通过在我的MainActivity类中单击按钮来调用LongOperation类,这也是一个活动,但是此处的上下文不起作用。 Following are the related excerpts which depict the calling and data fetch procedure. 以下是描述调用和数据获取过程的相关摘录。

MainActivity button Press code(in separate file) MainActivity按钮按代码(在单独的文件中)

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) LongOperation类代码(在单独的文件中)

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? 那么,如何将此类中的数据返回给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. 对象中的数据具有string []格式和键值对。 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. 这里的问题是由于LongOperation类是非活动类引起的,因此在使用文件编写器或sharedpreferences时无法传递上下文。

Edit 1: 编辑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: AsyncTask类别:

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. 尽管代码片段不是使用AsyncThread的最佳方式,但是如果我必须针对您的问题进行回答,那么您的变量就是public static。 Why don't you use something like following. 您为什么不使用类似以下的内容。

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

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

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

相关问题 将大多数属性从一个 class object 复制到另一个 class - Copy most attributes from one class object to another class 将一个类从一个应用程序复制到另一个应用程序后,Android发出ClassNotFoundException异常 - ClassNotFoundException android after copy a class from one app to another one 将 java 对象/类从一个类加载器复制到另一个类加载器 - Copy java object/class from one classloader to another classloader 有没有办法将一个对象从一个类复制到另一个完全不同的类中的对象? - Is there a way to copy one object from one class into another object from an entirely different class? 从一类到另一类的数据访问 - Data accessing from one class to another class 如何将一个对象字段数据复制到同一类的另一对象? - How it copy one object fields data to another object of the same class? 将值从一个类传递到另一个android - passing values from one class to another android 将一个对象从一个类转到另一个类? - Gettin an object from one class to another? Java-将静态arrayList从一个类复制到另一个类的HashMap - Java - copy static arrayList from one class to a HashMap in another class 将一个 class 中字段的所有值复制到另一个 class - Copy all values from fields in one class to another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM