-1

我正在开发一个应用程序,其中我有一个共同的活动,即BaseActivity,我正在将它扩展到我的应用程序中的所有其他活动。我在 BaseActivity 中运行了几个 AsyncTask 以反映所有其他活动。我的查询是我需要从我的一项活动中将值传递给这个 BaseActivity,比如MainActivity

我尝试了类,接口概念,但不适合我。建议我,如何实现这一目标。如果需要我的任何参考代码,请评论它,我会发布它。

已编辑

我需要将此计数值传递给BaseActivity。我不能使用 Intent,因为我正在导航到另一个活动,比如MainActivity2

MainActivity 这是我的 AsyncTask 代码

public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {

    @Override

    protected Integer doInBackground(String... params) {

        Integer result = 0;

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String response = streamToStringCount(httpResponse.getEntity().getContent());
                parseResultCount(response);
                result = 1;
            } else {
                result = 0;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }

        return result;
    }

    @Override

    protected void onPostExecute(Integer result) {

        //  isRefreshing = false;


        //layout.setRefreshing(false);

        super.onPostExecute(result);

    }
}


String streamToStringCount(InputStream stream) throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
    String line;
    String result = "";
    while ((line = bufferedReader.readLine()) != null) {

        result += line;
    }
    if (null != stream) {

        stream.close();
    }
    return result;
}


private void parseResultCount(String result) {
    Detail_Item item;
    try {
        JSONObject posts = new JSONObject(result);


        //rstatus1=posts.optString("count");
        countValue=posts.optInt("count");
        countValue1=countValue;
        Log.i("countvalue", String.valueOf(countValue));

    }

    catch (JSONException e) {

        e.printStackTrace();

    }
}
4

5 回答 5

1

将值存储在 SharedPreferences 中并从应用程序中的任何活动中检索它们。

于 2016-05-26T06:56:53.743 回答
1

在你的 AsyncTask 定义

public interface ResultListener {
    void onResultReady(Integer result);
}

创建接受 ResultListener 参数的构造函数

private ResultListener mRl = null;
public AsyncHttpTask (ResultListener rl) {
    mRl=rl;
}

在 postExecute (它在主线程上运行,你知道)调用监听器

 protected void onPostExecute(Integer result) {
       super.onPostExecute(result);
       if (mRl!=null)
          mRl.onResultReady(result);
 }

现在,如果您引用了一些实现 ResultListener 的活动,您需要传递结果,您可以通过以下方式启动任务:

AsyncHttpTask.ResultListener myActivity ;
...
(new AsyncHttpTask(myActivity)).execute(myParameters);

您的问题现在归结为您的活动/片段如何开始和协调,以便您可以myActivity在适当的地方获得适当的参考。

于 2016-05-26T07:34:56.120 回答
0

在您当前的 Activity 中,创建一个新的 Intent:

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);

然后在新的 Activity 中,检索这些值:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}

参考链接

于 2016-05-26T07:04:38.023 回答
0

根据我的理解,您希望将一些值从 MainActivity(扩展 BaseActivity)传递给 BaseActivity。

只需在 BaseActivity 中创建一个方法,例如,

protected void setYourValue(Object obj){
    ..........
}

从您的 MainActivity 只需调用此方法并设置您想要的任何内容。

setYourValue("This is my value");

如果您的 MainAcitivity 不是从 BaseActivity 扩展的,

 public static void setYourValue(Object obj){
    ..........
 }

在 MainActivity 中,

BaseActivity.setYourValue("This is my value");

请对实施共享偏好进行一些研究。

于 2016-05-26T06:56:44.073 回答
0

您可以通过定义 public static 来实现这一点,因为 this 或子类的所有实例都将共享它。这是一个非常丑陋的解决方案,我不推荐。最好使用附加功能

Intent 意图 = new Intent(context, SecondActivity.class); intent.putExtra("Key", Value); 开始活动(意图)

更多信息请参阅文档https://developer.android.com/guide/components/intents-filters.html

于 2016-05-26T07:05:49.290 回答