如何将现有的Activity类更改为片段类

时间:2012-07-19 17:12:26

标签: android android-widget android-fragments android-fragmentactivity

我正在尝试更新现有的Android应用程序,以便在手机和平​​板电脑上运行。所以我想将Activity类更改为fragment。以下是我的活动课程。

Individual Activity class

package FXPAL.Unity.Android;

import FXPAL.Unity.Android.Person.CalendarEntry;
import FXPAL.Unity.Android.Person.IM;
import FXPAL.Unity.Android.Person.Person;
import FXPAL.Unity.Android.Person.Status;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class IndividualViewActivity extends Activity {


private static final String DB_TAG = "IndividualView";

//private static final String DB_TAG_PING = "ShowPingView";

private TextView displayName, presence, phoneNum, cellNum, emailAddr, btLocation, calInfo, status;
private ImageView userImage;
private Person personToDisplay;
private String phoneNumber, cellNumber, username, emailAddress;

private TableRow fxpalIMrow, skypeIMrow, msLiveIMrow, gtalkIMrow;
private TableLayout imTable;
private TextView fxpalIMstatus, skypeIMstatus, msLiveIMstatus, gtalkIMstatus, pingText;
private Button nudgeButton, pingButton, clearPingButton, calendarButton;
private LinearLayout pingLayout, pingNudgeButtonLayout;
protected String numToCall, numtype;
private DatabaseHelper db;

//private int pingID = -1;

private UnityMobileApp appCtx;


private static final int DIALOG_CALL_OFFICE_ID = 0;
private static final int DIALOG_CALL_CELL_ID = 1;
protected static final int DIALOG_VIEW_CALENDAR = 2;

private static final int REFRESH_MENU_ID = 0;
protected static final String OFFICE = "office";
private static final String CELL = "cell";

protected static final int MESSAGE_CONNECTION_ERROR_TOAST = 0;
//private static final String DEBUG_TAG = "unity.IndividualViewActivity";

private final Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MESSAGE_CONNECTION_ERROR_TOAST:
            Toast.makeText(IndividualViewActivity.this, Consts.CONNECTION_ERROR_MESSAGE, Toast.LENGTH_SHORT).show();
            break;
        }
    }
};
private SharedPreferences prefs;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //doBindService();
    setContentView(R.layout.individual_view);

    db = new DatabaseHelper(this);
    appCtx = (UnityMobileApp) getApplication();
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    //Setup View      
    displayName = (TextView)findViewById(R.id.individualName);        
    presence = (TextView)findViewById(R.id.individualPresence);
    userImage = (ImageView)findViewById(R.id.individualUserImage);
    phoneNum = (TextView)findViewById(R.id.individualPhone);
    cellNum = (TextView)findViewById(R.id.individualCellPhone);
    emailAddr = (TextView)findViewById(R.id.individualEmail);
    btLocation = (TextView)findViewById(R.id.individualLocation);
    calInfo = (TextView)findViewById(R.id.individualCalendarInfo);
    status = (TextView)findViewById(R.id.individualStatus);

    fxpalIMrow = (TableRow)findViewById(R.id.im_fxpal);
    skypeIMrow = (TableRow)findViewById(R.id.im_skype);
    msLiveIMrow = (TableRow)findViewById(R.id.im_ms_live);
    gtalkIMrow = (TableRow)findViewById(R.id.im_gtalk);

    fxpalIMstatus = (TextView)findViewById(R.id.im_status_fxpal); 
    skypeIMstatus = (TextView)findViewById(R.id.im_status_skype);
    msLiveIMstatus = (TextView)findViewById(R.id.im_status_ms_live);
    gtalkIMstatus = (TextView)findViewById(R.id.im_status_gtalk);

    imTable = (TableLayout)findViewById(R.id.individualIMStatus);

    nudgeButton = (Button)findViewById(R.id.individual_nudge_button);
    pingButton = (Button) findViewById(R.id.individual_ping_button);
    calendarButton = (Button)findViewById(R.id.individualViewCalendarButton);

    pingText = (TextView)findViewById(R.id.individualPingText);;
    clearPingButton = (Button) findViewById(R.id.individualClearPingButton);
    pingLayout = (LinearLayout)findViewById(R.id.individualPingInfo);
    pingNudgeButtonLayout = (LinearLayout)findViewById(R.id.individual_nudge_ping_layout);

    calendarButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            showDialog(DIALOG_VIEW_CALENDAR);
            db.viewEvent(DB_TAG + ":CalendarDialog", username);

        }
    });



    nudgeButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {               
            startActivity(new Intent(IndividualViewActivity.this, SendNudgeActivity.class)
                .putExtra(Consts.EXTRA_USERNAME, personToDisplay.getUsername())
                .putExtra(Consts.EXTRA_DISPLAY_NAME, personToDisplay.getDisplayName()));
        }
    });

    pingButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {               
            startActivity(new Intent(IndividualViewActivity.this, SendPingActivity.class)
                .putExtra(Consts.EXTRA_USERNAME, personToDisplay.getUsername())
                .putExtra(Consts.EXTRA_DISPLAY_NAME, personToDisplay.getDisplayName()));
        }
    });
}

public void onPause(){
    super.onPause();
}

public void onDestroy(){
    super.onDestroy();
    //doUnbindService();
    db.close();
}

public void onResume(){
    super.onResume();

    Intent mIntent = getIntent();
    username = mIntent.getStringExtra(Consts.EXTRA_USERNAME);

    if(username.equalsIgnoreCase(prefs.getString(Consts.PREF_USERNAME, "")))
        pingNudgeButtonLayout.setVisibility(View.GONE);
    else
        pingNudgeButtonLayout.setVisibility(View.VISIBLE);

    personToDisplay = appCtx.getEveryone().get(username);
    updateView();
    updateInfo();

    if (personToDisplay == null || personToDisplay.getCalendar() == null ||
            personToDisplay.getCalendar().getCalendar().size() == 0)
        calendarButton.setEnabled(false);
    else
        calendarButton.setEnabled(true);        


        db.viewEvent(DB_TAG, username);
        pingLayout.setVisibility(View.GONE);
    //}
}

private void updateInfo(){
    ServerHelper.RequestListener listener = new ServerHelper.RequestListener() {
        public void onCompleted(ServerHelper.Request request) {
            if (request.isUnauthorized())
                startActivity(new Intent(IndividualViewActivity.this, LoginActivity.class));
            else if (request.isSuccess()) {
                Person.PersonHelper.getEveryoneAsync(IndividualViewActivity.this, appCtx.everyone, null, request.getResponseString(),
                        new Runnable() {
                    public void run () {
                        personToDisplay = appCtx.everyone.get(username);
                        updateView();
                    }
                });
            }
        }
    };
    ServerHelper.appCtx = appCtx;
    ServerHelper.getStatus (prefs, listener);
}

private void updateView(){
    if (personToDisplay == null)
        return;
    displayName.setText(personToDisplay.getDisplayName());
    presence.setText(personToDisplay.getPresenceState()); 

    Status mStatus = personToDisplay.getStatus();
    if( mStatus!= null && mStatus.toString().length()>0){
        status.setVisibility(View.VISIBLE);
        status.setText(mStatus.toString());
    }

    userImage.setImageBitmap(personToDisplay.getRoundedImage(Consts.LARGE_USER_IMAGE_SIZE, Consts.LARGE_USER_IMAGE_BORDER));

    phoneNumber = personToDisplay.getPhoneNum();
    if(phoneNumber.length()>0){
        phoneNum.setText(OFFICE + ": " + phoneNumber);
        phoneNum.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                numToCall = phoneNumber;
                numtype = OFFICE;
                showDialog(DIALOG_CALL_OFFICE_ID);
            }
        });}
    else
        phoneNum.setVisibility(View.GONE);


    cellNumber = personToDisplay.getCellPhoneNum();
    if(cellNumber.length()>0){
    cellNum.setText(CELL + ": " + cellNumber);
    cellNum.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            numToCall = cellNumber;
            numtype = CELL;
            showDialog(DIALOG_CALL_CELL_ID);
        }
    });}
    else
        cellNum.setVisibility(View.GONE);

    emailAddress = personToDisplay.getEmailAddress();
    emailAddr.setText(emailAddress);
    emailAddr.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            db.communicate(DB_TAG, "email", username);
            Intent emailIntent = new Intent();
            emailIntent.setAction(android.content.Intent.ACTION_SENDTO);
            emailIntent.setData(Uri.parse("mailto:" + emailAddress));
            startActivity(Intent.createChooser(emailIntent, "Select Email Client"));
        }
    });
    if(personToDisplay.hasLocation()){
        btLocation.setVisibility(View.VISIBLE);
        btLocation.setText(personToDisplay.getLocation().toString());
    }

    CalendarEntry usefulCalInfo = personToDisplay.getCalendar().getMostUseful();
    if(usefulCalInfo != null){
        calInfo.setVisibility(View.VISIBLE);
        calInfo.setText("Calendar: " + usefulCalInfo.toString());
    }

    if(personToDisplay.hasIM()){
        imTable.setVisibility(View.VISIBLE);
    }
    for(IM mIM : personToDisplay.getIMList().values()){
        switch(mIM.getId()){
        case(IM.FXPAL_ID):
            fxpalIMrow.setVisibility(View.VISIBLE);
            fxpalIMstatus.setText(mIM.getStatus());
        break;
        case(IM.SKYPE_ID):
            skypeIMrow.setVisibility(View.VISIBLE);
            skypeIMstatus.setText(mIM.getStatus());
        break;
        case(IM.MS_LIVE_ID):
            msLiveIMrow.setVisibility(View.VISIBLE);
            msLiveIMstatus.setText(mIM.getStatus());
        break;
        case(IM.GTALK_ID):
            gtalkIMrow.setVisibility(View.VISIBLE);
            gtalkIMstatus.setText(mIM.getStatus());
        break;
        default:

        }
    }

}

protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;

    switch(id) {
    case DIALOG_CALL_OFFICE_ID: 

    case DIALOG_CALL_CELL_ID:

        dialog = CommDialog.getCallDialog(displayName.getText().toString(), numToCall,     numtype, IndividualViewActivity.this, db);
        break;
    case DIALOG_VIEW_CALENDAR:
        return CalendarListDialog.getDialog(username, IndividualViewActivity.this, appCtx);
    default:
        dialog = null;
    }
    return dialog;
}

public boolean onCreateOptionsMenu(Menu menu){

    menu.add(Menu.NONE, REFRESH_MENU_ID, Menu.NONE, "Refresh")
    .setIcon(R.drawable.ic_menu_refresh)
    .setAlphabeticShortcut('r');

    return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {

    case REFRESH_MENU_ID:
        refresh();
        break;
    }
    return (super.onOptionsItemSelected(item));
}

void refresh() {
    // update the location and report it to the server
    ReportingService.startLocationRequest(appCtx, true);
    // this may still retrieve the old location
    updateInfo();
}
  }

请帮帮我们。我很长时间以来一直坚持这个问题。

此致 勒凯什

0 个答案:

没有答案