从tabhostView中的asynctask启动新活动

时间:2012-06-05 13:57:05

标签: android android-asynctask android-activity android-tabhost

我的代码有问题:

我正在开发一款需要您登录的应用。该应用包含一个加载屏幕,然后是一个带有4个标签的tabView。最后一个选项卡是一个允许您登录的活动。我在这里设置了EditText Views和一个Login按钮。

登录活动到目前为止完成了代码:

package com.appsoweb.kvodeventer;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class KVOMeldingen extends Activity {

       public static final JSONObject jsonResult = null;
       Button bLogin, bCreateAccount, bResetPassword;
       EditText etUsername, etPassword;
       static String Username;
       static String Password;

       public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.meldingen);
             final EditText etUsername = (EditText) findViewById(R.id.etUsername);
             final EditText etPassword = (EditText) findViewById(R.id.etPassword);
             Button bLogin = (Button) findViewById(R.id.bLogin);
             Button bCreateAccount = (Button) findViewById(R.id.bCreateAccount);
             Button bResetPassword = (Button) findViewById(R.id.bResetPassword);

             bLogin.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                           if (etUsername.length() <= 0) {
                                  etUsername.setError("Veld mag niet leeg zijn");
                           } else if (etPassword.length() <= 0) {
                                  etPassword.setError("Veld mag niet leeg zijn");
                           } else {
                                  Username = etUsername.getText().toString();
                                  Password = etPassword.getText().toString();
                           }
                           LoginTask NDLT = new LoginTask();
                           NDLT.execute();
                    }
             });

             bCreateAccount.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                           // Doe iets hier.......

                    }

             });

             bResetPassword.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                           // Doe iets hier........

                    }

             });

       }

       public static String getUsername() {
             return Username;
       }

       public static String getPassword() {
             return Password;
       }

       class LoginTask extends AsyncTask<Void, Void, JSONObject> {
             ProgressDialog waitingDialog;

             @Override
             protected void onPreExecute() {
                    waitingDialog = new ProgressDialog(KVOMeldingen.this);
                    waitingDialog.setMessage("Laden...");
                    waitingDialog.show();
                    super.onPreExecute();
             }

             @Override
             protected JSONObject doInBackground(Void... params) {

                    JSONObject json = JsonFunctionLogin
                                  .getJsonLoginResult("http://api.crossalertdeventer.nl/login.json");
                    return json;

             }

             @Override
             protected void onPostExecute(JSONObject json) {
                    super.onPostExecute(json);
                    if (waitingDialog.isShowing()) {
                           waitingDialog.dismiss();
                           Log.d("iets gebeurt", "gedaan");
                    }
                    try {

                           String LoginResult = json.getString("login");
                           String UserIdResult = json.getString("user_id");
                           Log.d("LoginResult", LoginResult);
                           Log.d("LoginUserId", UserIdResult);
                           json = null;
                           if (LoginResult == "succes"){
                                  // Open activity for listview.
                           } else {
                                  // Don't show items and give error message.
                           }


                    } catch (Exception e) {
                           Log.e("KVOMeldingen", "error" + e.getMessage());

                    }

             }
       }


}

您可以看到身份验证在后台进行。

现在你在postExecute方法上看到了If语句块:如果用户成功登录,我想打开一个活动,从网上加载json并将其放在列表项中。

现在我不知道该怎么做。因为如果我使用intent,则会打开新的listActivity,但不会在我的Starting Activity的TabView中。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我做了类似的事情,所以试试吧。将活动传递给AsyncTask类并将其保存为成员变量。在onPostExecute()方法中,从主活动中调用方法以打开新活动或显示错误消息。它对我有用!! ; - )

一些代码:

public LoginTask(Activity parentActivity) {
        mParentActivity = parentActivity;
    }

@Override
protected void onPostExecute(JSONObject json) {
    ...
    if (LoginResult == "succes"){
        mParentActivity.openNewActivity(json);
    } else {
        mParentActivity.showErrorMessage("ERROR");
    }
    ...
}