JSONデータを解析し、ナビゲーションドロワーヘッダーに名前を設定できました。ログインJavaクラス(ホームクラス)の後にホームアクティビティーのデータを解析しました。ナビゲーション・ドロワーのフラグメントがあり、それはJSONからの名前、アドレスを入力する必要があります。どのクラスのJSONデータを他のクラスで使用できるか別のCLassで使用されるJSONデータの解析?
Home.java
public class Home extends AppCompatActivity implements View.OnClickListener {
LinearLayout calendar, classSchedule, progressReport, profile, fee, dshboard, setting, logout, attendance;
android.support.v4.app.FragmentManager fragmentManager;
public static final String Navigation_URL = "http://192.168.100.5:84/api/academics/students/";
ImageView studentprofileimage;
TextView profilename, sprofilename;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
studentprofileimage = (ImageView) findViewById(R.id.avatar);//initilise student name
profilename = (TextView) findViewById(R.id.profilename);// student profile name
sprofilename = (TextView) findViewById(R.id.student_profilename);
dshboard = (LinearLayout) findViewById(R.id.dashboard_layout);
calendar = (LinearLayout) findViewById(R.id.calender_layout);
fee = (LinearLayout) findViewById(R.id.view_fee);
classSchedule = (LinearLayout) findViewById(R.id.class_schedule);
progressReport = (LinearLayout) findViewById(R.id.progress_report);
profile = (LinearLayout) findViewById(R.id.view_profile);
setting = (LinearLayout) findViewById(R.id.mainsetting);
logout = (LinearLayout) findViewById(R.id.mainlogout);
attendance = (LinearLayout) findViewById(R.id.class_attendance);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setVisibility(View.VISIBLE);
calendar.setOnClickListener(this);
classSchedule.setOnClickListener(this);
fee.setOnClickListener(this);
dshboard.setOnClickListener(this);
progressReport.setOnClickListener(this);
profile.setOnClickListener(this);
setting.setOnClickListener(this);
logout.setOnClickListener(this);
attendance.setOnClickListener(this);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.container, new Dashboard());
tx.commit();
SessionManagement session = new SessionManagement(Home.this);
session.checkLogin();
String master_id = session.getMasterId();
Log.d("TAG", "master_id:" + master_id);
makeJsonObjectRequest(Integer.parseInt(master_id));
Log.d("TAG", "master_id:" + master_id);
//master_id = session.clear();
}
StudentInformation studentInformation;
public void makeJsonObjectRequest(int stud_id) {
String URL = Navigation_URL + stud_id;
Log.d("TAG", "URL:" + URL);
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
studentInformation = new StudentInformation();
studentInformation.Name = jsonObject.getString("NAME");
profilename.setText(studentInformation.Name);
// sprofilename.setText(studentInformation.Name);
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Home.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.dashboard_layout) {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Dashboard())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.calender_layout) {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new CalenderFragment())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.view_fee) {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Fee())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.class_schedule) {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new FragmentClassSchedule())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.progress_report) {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new ProgressFragment())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.class_attendance) {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new AttendanceStudentFragment())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.view_profile) {
Bundle bundle1 = new Bundle();
bundle1.putSerializable("student_obj", studentInformation);
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new ProfileFragment())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.mainsetting) {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new SettingFragment())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.mainlogout) {
SessionManagement session = new SessionManagement(Home.this);
session.clear();
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else {
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Dashboard())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
}
}
StudentInformation.java
public class StudentInformation implements Serializable {
String PhoneNumber;
String DOB;
String ClassName;
String Gender;
public String Image;
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public Integer studentId;
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String Name;
public String getName() {
return Name;
}
public String getDOB() {
return DOB;
}
public void setDOB(String DOB) {
this.DOB = DOB;
}
public String getClassName() {
return ClassName;
}
public void setClassName(String className) {
ClassName = className;
}
public String getPhoneNumber() {
return PhoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
PhoneNumber = phoneNumber;
}
public void setName(String name) {
Name = name;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
Gender = gender;
}
}
ProfileFragment.java
public class ProfileFragment extends Fragment {
TextView profilename; // i want to set name on this textView
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_profile, container, false);
setHasOptionsMenu(true);
profilename = (TextView) view.findViewById(R.id.student_profilename);
// StudentInformation studentInformation = null;
// studentInformation = (StudentInformation) getActivity().getIntent().getSerializableExtra("");
// profilename.setText(studentInformation.getName());
if(this.getArguments().getSerializable("student_obj")!=null)
{
Serializable student_obj_serializable = getArguments().getSerializable("student_obj");
StudentInformation student_obj=(StudentInformation)student_obj_serializable;
//set all value here
profilename.setText(student_obj.getName());
}
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.dashboard, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I別のクラスで使用するデータを解析できませんか?
からデータを取得しますクリック時にフラグメントをプロファイルするには、断片のために 'setArguments(bundle)'を使ってバンドルのデータを単に渡します。共有プリファレンスでデータを保存し、必要なクラスにアクセスすることもできます。 – Piyush
またはグローバル変数 – Nilabja
を使用してみてください@Piyushいくつかのコードスニペットを提供できます – seon