Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

java - How TO Change Activity To Fragment

I want To change my Activity to Fragment I tried hard but I am unable to Change my Activity To Fragment. Can Anyone Please Tell me How to do this? Where To Edit I am new On Android. please tell me And Thanks In Advance

this Is My Activity:

import android.app.Activity;

   import android.app.ProgressDialog;
   import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.content.SharedPreferences.Editor;
   import android.os.Bundle;
  import android.os.StrictMode;

import android.view.View;
import android.view.View.OnClickListener;
 import android.view.LayoutInflater;
import android.view.ViewGroup;
 import android.view.Window;
 import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 import android.widget.Toast;

 import com.example.phonebook.models.UserModel;
import com.example.phonebook.services.ImageLoader;

public class Welcome extends Activity {
private ProgressDialog pDialog;
Button editprofile;
JSONParser jsonParser = new JSONParser();

ImageView Image;
UserModel user = (UserModel) getIntent().getSerializableExtra("User");
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads().detectDiskWrites().detectNetwork()
    .penaltyLog().build());
   setContentView(R.layout.welcome);


    requestWindowFeature(Window.FEATURE_NO_TITLE);

     user = (UserModel) getIntent().getSerializableExtra("User");
Button editprofile = (Button)findViewById(R.id.btn_edit);
    ImageView image = (ImageView)findViewById(R.id.imageView1);
    TextView userfullname = (TextView) findViewById(R.id.userfullname);
    TextView tv_age = (TextView) findViewById(R.id.age);
    TextView tv_gender = (TextView) findViewById(R.id.gender);
    TextView tv_intrseted = (TextView) findViewById(R.id.intrestedin);


    int loader = R.drawable.loader;

    String UserfullName = user.getUser_Full_Name();
    String image_url = user.getUser_Image();
    String Age = user.getUser_Age();
    String Gender = user.getGender();
    String IntrestedIn = user.getIntrest_In();
    userfullname.setText(UserfullName);
    tv_age.setText(Age);
    tv_gender.setText(Gender);
    tv_intrseted.setText(IntrestedIn);
    Button logout=(Button)findViewById(R.id.logout);
    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SharedPreferences sharedPreferences =getSharedPreferences(MainActivity.MyLOGINDATA, Context.MODE_PRIVATE);

                      Editor editor = sharedPreferences.edit();
                      editor.clear();
                      editor.commit();
                      moveTaskToBack(true); 
                      Intent i=new Intent(getApplicationContext(),MainActivity.class);
                      startActivity(i);
                      finish();

        }
    });

    editprofile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent e = new Intent(getApplicationContext(),
                    EditProfile.class);
            e.putExtra("userInfo", user);
            finish();
            startActivity(e);

        }
    });

    // ImageLoader class instance
    ImageLoader imgLoader = new ImageLoader(getApplicationContext(),
            Welcome.this);

    image.setTag(image_url);

// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView 
    imgLoader.DisplayImage(image_url, Welcome.this, image);
}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just understand some steps then you can easily convert a Activity to Fragment now and also in future..:

First of all instead of extending Activity,just extend Fragment..

Ex: public class Welcome extends Fragment{

Then override onCreateView()..

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
.....
}

Then inflate the layout through LayoutInflater and asign to a View for further use in subview inilialization..

like: View mView = inflater.inflate(R.layout.welcome, null);

Then initialize all sub views with the help of main view..like:

 ImageView image = (ImageView) mView.findViewById(R.id.imageView1);
 TextView userfullname = (TextView) mView.findViewById(R.id.userfullname);

Now do all your tasks same like activity here..

The important thing.. Use getActivity() in the place of context..

Ex: Toast.maketext(getActivity(), "...", Toast.LENGTH_LONG).show();

For more information ,just visit Fragment in developers block..

Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...