如何在android中的另一个类中创建一个iner类的实例?

时间:2013-12-12 06:48:05

标签: android

我正在开发一个Android app.in我的主要活动我已经使用了MainActivity中的另一个类,它由AssyncTask扩展。我只想在另一个班级中调用该Assync任务。我该怎么做?我尝试了很多方法......这是我的主要活动类

public class MainActivity extends Activity {

public MainActivity() {

}

EditText userName;
EditText passWord;
ListView list;
TextView fname;
TextView lname;
TextView stime;
TextView etime;
TextView date;
ImageButton login;

public static ArrayList<HashMap<String, String>> oslist;

// URL to get JSON Array
// private static String url = "http://api.learn2crack.com/android/jsonos/";

public static String confirm;
String firstName;

// JSON Node Names
/*
 * static final String TAG_OS = "android"; static final String TAG_VER =
 * "ver"; static final String TAG_NAME = "name"; static final String TAG_API
 * = "api";
 */

static final String TAG_DATA = "d";
static final String TAG_OBJ_1 = "Appointment";
static final String TAG_OBJ_2 = "PatientProfile";
static final String TAG_PROP_FNAME = "FirstName";
static final String TAG_PROP_LNAME = "LastName";
static final String TAG_STIME = "StartTime";
static final String TAG_ETIME = "EndTime";
static final String TAG_DATE = "Date";
static final String TAG_HID = "PatientPersonId";
static final String TAG_HIDApp = "AppointmentId";
static final String TAG_IMG = "ImageURL";

// static final String TAG_API = "NickName";
JSONArray androids = null;
JSONObject duplicate;
public static JSONArray dupArray;

public JSONArray getJsonArray() {
    return dupArray;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loging);

    if (android.os.Build.VERSION.SDK_INT > 9)

    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();

        StrictMode.setThreadPolicy(policy);

    }

    oslist = new ArrayList<HashMap<String, String>>();
    login = (ImageButton) findViewById(R.id.imageButton1);
    new ArrayList<HashMap<String, String>>();

    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // if login success then call below
            userName = (EditText) findViewById(R.id.editUserName);
            passWord = (EditText) findViewById(R.id.editPassWord);

            String name = userName.getText().toString().trim();
            String passwd = passWord.getText().toString().trim();

            CheckLogin check = new CheckLogin();
            confirm = check.AuthenticateUser(name, passwd);

            if (!(confirm.equals("-1"))) {
                new JSONParse().execute();
            }

            else {

                Toast.makeText(getBaseContext(),
                        "Check Username or Password", Toast.LENGTH_LONG)
                        .show();
            }

        }
    });

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    finish();
}

public class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;

    public JSONParse() {

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        fname = (TextView) findViewById(R.id.fname);
        lname = (TextView) findViewById(R.id.lname);
        stime = (TextView) findViewById(R.id.stime);
        etime = (TextView) findViewById(R.id.etime);
        date = (TextView) findViewById(R.id.blank);
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected JSONObject doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        JsonParser jParser = new JsonParser();

        // Getting JSON from URL
        JSONObject json = null;
        try {

            JSONObject parm = new JSONObject();
            // parm.put("PersonID", confirm);
            parm.put("caregiverPersonId", confirm);
            json = jParser.getJSONFromUrl(parm, url);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // Toast.makeText(getApplicationContext(),
            // "Hi "+firstName+"You Don't Have Any Requests!",
            // Toast.LENGTH_LONG).show();
        }
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        // TODO Auto-generated method stub
        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            // JSONObject jobs = json.getJSONObject(TAG_DATA);

            String emptyjson = json.getString(TAG_DATA);

            if (emptyjson.equals("[]")) {

                startActivity(new Intent(MainActivity.this, Empty.class));
            } else {

                androids = json.getJSONArray(TAG_DATA);

                // JSONObject appt= new JSONObject(json.getString("d"));

                for (int i = 0; i < androids.length(); i++) {
                    JSONObject c = androids.getJSONObject(i);

                    // Storing JSON item in a Variable

                    JSONObject job = c.getJSONObject(TAG_OBJ_2);

                    String url = job.getString("ImageURL");

                    firstName = job.getString(TAG_PROP_FNAME);
                    String lastName = job.getString(TAG_PROP_LNAME);

                    JSONObject job_two = c.getJSONObject(TAG_OBJ_1)
                            .getJSONObject("DayTimeSlot");

                    String start = job_two.getString(TAG_STIME);
                    String end = job_two.getString(TAG_ETIME);

                    /*
                     * String ackwardDate = job_two.getString(TAG_DATE);;
                     * Calendar calendar = Calendar.getInstance(); String
                     * ackwardRipOff = ackwardDate.replace("/Date(",
                     * "").replace(")/", ""); Long timeInMillis =
                     * Long.valueOf(ackwardRipOff);
                     * calendar.setTimeInMillis(timeInMillis); String
                     * theDate = (calendar.getTime().toGMTString());
                     */
                    String Ldate = job_two.getString(TAG_DATE);
                    String ackwardRipOff = Ldate.replace("/Date(", "")
                            .replace(")/", "");
                    Long Ldat = Long.valueOf(ackwardRipOff);
                    Date date = new Date(Ldat);
                    String strdate = (String) DateFormat.format("MM/dd/yy",
                            date);

                    JSONObject toHid = c.getJSONObject(TAG_OBJ_1);

                    String Hid = toHid.getString(TAG_HID);

                    // String date = dateConvert(Ldate);
                    // long Ldate = job_two.getLong("Date");

                    // String strdate = (String)
                    // DateFormat.format("MM/dd/yy h:mmaa", date);

                    JSONObject job_three = c.getJSONObject(TAG_OBJ_1);
                    String AppoinmentID = job_three.getString(TAG_HIDApp);

                    // Adding value HashMap key => value

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_PROP_FNAME, firstName);
                    map.put(TAG_PROP_LNAME, lastName);
                    map.put(TAG_STIME, start);
                    map.put(TAG_ETIME, end);
                    map.put(TAG_DATE, strdate);
                    map.put(TAG_HID, Hid);
                    map.put("ImageURL", url);
                    map.put("AppointmentId", AppoinmentID);

                    oslist.add(map);

                    // duplicate = new JSONObject();

                    Intent reult = new Intent(MainActivity.this,
                            ViewList.class);
                    // reult.putStringArrayListExtra("map", oslist);
                    // startActivity(reult);

                    reult.putExtra("arraylist", oslist);
                    startActivityForResult(reult, 500);

                }

                dupArray = new JSONArray();
                dupArray.put(oslist);
            }

        } catch (JSONException e) {
            e.printStackTrace();
            // Intent reult = new Intent(MainActivity.this,ViewList.class);
            // reult.putStringArrayListExtra("map", oslist);
            // startActivity(reult);

        }

    }
}

public String dateConvert(String d) {

    Date dt = new Date();
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    String strDate = df.format(dt);

    return strDate;

}

}

2 个答案:

答案 0 :(得分:0)

内部课程

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

you can find more on inner class here

答案 1 :(得分:0)

如果它是静态类,您可以调用MainActivity.JSONParse或查看此答案Can i Create the object of a activity in other class?,看看如果不是静态的话,该怎么做。