2017-03-08 2 views
0

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別のクラスで使用するデータを解析できませんか?

+2

からデータを取得しますクリック時にフラグメントをプロファイルするには、断片のために 'setArguments(bundle)'を使ってバンドルのデータを単に渡します。共有プリファレンスでデータを保存し、必要なクラスにアクセスすることもできます。 – Piyush

+0

またはグローバル変数 – Nilabja

+0

を使用してみてください@Piyushいくつかのコードスニペットを提供できます – seon

答えて

1

保存モデルクラス内のすべてのデータとsetArguments(バンドル)を使用して、フラグメントにそのモデルクラスのオブジェクトを渡す

モデルクラス:

public class StudentInformation implements Serializable { 


     private String PhoneNumber; 
     private String DOB; 
     private String ClassName; 
     private String Gender; 
     private String Image; 


     public String getPhoneNumber() { 
      return PhoneNumber; 
     } 

     public void setPhoneNumber(String phoneNumber) { 
      PhoneNumber = phoneNumber; 
     } 

     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 getGender() { 
      return Gender; 
     } 

     public void setGender(String gender) { 
      Gender = gender; 
     } 

     public String getImage() { 
      return Image; 
     } 

     public void setImage(String image) { 
      Image = image; 
     } 
    } 

makeJsonObjectRequest:プロフィール]ボタンをクリックしたときに、

//declare global 

    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"); 

         studentInformation.setName(jsonObject.getString("NAME")); 

         profilename.setText(studentInformation.getName()); 
         //  sprofilename.setText(studentInformation.Name); 

         //parse all data here and set all info to studentInformation 



        } 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); 

    } 

if (id == R.id.view_profile) { 

     Bundle bundle1 = new Bundle(); 
     bundle1.putSerializable("student_obj", studentInformation); 

     Fragment fragment = new ProfileFragment(); 
     fragment.setArguments(bundle1); 
     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, fragment); 
     fragmentTransaction.commit(); 

      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

    } 

ProfileFragment:あなたは別の活動からJSONデータを送信したい場合は

@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; 
} 
+0

@seonどこのコードでデータを解析しますか? – user2025187

+0

ホームクラスで、私は名前だけを解析したので、 – seon

+0

エラーが出る java.lang.NullPointerException:仮想メソッド 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)'を呼び出そうとしていますヌルオブジェクト参照 – seon

0

そのクラスを作成し、シリアル化可能にそれを拡張し、クラス内のすべてのパラメータ変数を作成します。 次に、クラスのオブジェクトを作成し、すべてのJsonデータをオブジェクトに格納します。送信するオブジェクトが複数ある場合は、作成した型クラスの配列を作成し、それをIntent経由で送信します。 you'r directyが起こった場合

+0

私はそれがデータが解析されるクラスのオブジェクトを作成することによって直接textViewにそれを設定することはできません – seon

+0

はいできます。あなたはそれを正しくやっていた。インテント –

0

はいあなたはとても主な活動の時に、SQL Liteでそれを格納モデルにデータを保存し得るが、モデル上の任意の操作が間違ったデータを取得することができますが、その後のナビゲーション引き出し

+0

を使用してクラスオブジェクトを別のアクティビティに直接送信することはできません。ホームクラスのオブジェクトをフラグメントに作成し、データを持つ関数を使用して直接設定します。 – seon

+0

makeYsonObjectRequestメソッドで解析後に保存できます。データを取得する時間 –

+0

私はこれを使用しています // StudentInformation studentInformation = null; // studentInformation =(StudentInformation)getActivity()。getIntent()。getSerializableExtra( ""); // profilename.setText(studentInformation.getName()); しかし、そのエラーが表示されている場合は – seon

関連する問題