2017-07-01 5 views
-5

こんにちは友人は、ビューページにフラグメントを含むAndroidアプリケーションを開発していますが、今はJSONデータをfragment.myの質問に解析したいのですが、jsonデータのサイズとして断片数を設定し、私はインターネットで何時間もそれを探していましたが、失望の結果がありましたので、助けてください。事前に感謝します。JSONデータをフラグメントに解析する方法は?

[{"c_id":13,"category_name":"PUBLICATIONS","imagename":"http://goringr.com/church_project/churchimagesnew/publications.jpg","ctype":"church"},{"c_id":14,"category_name":"YOUTH ASSOCIATION","imagename":"http://goringr.com/church_project/churchimagesnew/youthassociation.jpg","ctype":"church"}] 

これは私のJSONデータであり、私が望むすべては、第2の断片でC_ID = 14を持つオブジェクトの最初のフラグメントと表示内容にC_ID = 13を持つオブジェクトの内容を表示することです。

MainActivity.java

package project1.test; 

import android.content.Intent; 
import android.support.design.widget.CollapsingToolbarLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 
public static final String data = "hello"; 
FragmentPagerAdapter adapterViewPager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //  Toolbar tool = (Toolbar)findViewById(R.id.tool); 
    //  setSupportActionBar(tool); 
    //  getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager); 
    adapterViewPager = new MyPagerAdapter(getSupportFragmentManager()); 
    vpPager.setAdapter(adapterViewPager); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.sample_actions, menu); 
    return true; 
} 

public static class MyPagerAdapter extends FragmentPagerAdapter { 
    private static int NUM_ITEMS = 3; 

    public MyPagerAdapter(FragmentManager fragmentManager) { 
     super(fragmentManager); 
    } 

    // Returns total number of pages 
    @Override 
    public int getCount() { 
     return NUM_ITEMS; 
    } 

    // Returns the fragment to display for that page 
    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 
       return FirstFragment.newInstance(0, "Fragment1"); 
      case 1: 
       return FirstFragment.newInstance(1, "Fragment2"); 
      case 2: 
       return SecondFragment.newInstance(2, "Fragment3"); 
      default: 
       return null; 
     } 
    } 

    // Returns the page title for the top indicator 
    @Override 
    public CharSequence getPageTitle(int position) { 
     return "Page " + position; 
    } 

} 

} 

FirstFragment

public class FirstFragment extends Fragment { 
// Store instance variables 
private String title; 
private int page; 

// newInstance constructor for creating fragment with arguments 
public static FirstFragment newInstance(int page, String title) { 
    FirstFragment fragmentFirst = new FirstFragment(); 
    Bundle args = new Bundle(); 
    args.putInt("someInt", page); 
    args.putString("someTitle", title); 
    fragmentFirst.setArguments(args); 
    return fragmentFirst; 
} 

// Store instance variables based on arguments passed 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setHasOptionsMenu(true); 
    page = getArguments().getInt("someInt", 0); 
    title = getArguments().getString("someTitle"); 
} 

// Inflate the view for the fragment based on layout XML 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

; 
    View view = inflater.inflate(R.layout.fragment_first, container, false); 
//  TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel); 
////  tvLabel.setText(page + " -- " + title); 


    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)view.findViewById(R.id.collapsing_toolbar1); 
    collapsingToolbarLayout.setTitle("black panther"); 

    return view; 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    // TODO Add your menu entries here 
    super.onCreateOptionsMenu(menu, inflater); 
    inflater.inflate(R.menu.sample_actions, menu); 
} 
} 

SecondFragment

public class SecondFragment extends Fragment { 
// Store instance variables 
private String title; 
private int page; 
Button tryit; 

// newInstance constructor for creating fragment with arguments 
public static SecondFragment newInstance(int page, String title) { 
    SecondFragment fragmentSecond = new SecondFragment(); 
    Bundle args = new Bundle(); 
    args.putInt("someInt", page); 
    args.putString("someTitle", title); 
    fragmentSecond.setArguments(args); 
    return fragmentSecond; 
} 

// Store instance variables based on arguments passed 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    page = getArguments().getInt("someInt", 0); 
    title = getArguments().getString("someTitle"); 
} 

// Inflate the view for the fragment based on layout XML 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_second, container, 
    false); 
    tryit = (Button) view.findViewById(R.id.tryit); 
    tryit.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getActivity(),ManagingProfile.class); 
      startActivity(intent); 
     } 
    }); 
//  TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel); 
//  tvLabel.setText(page + " -- " + title); 
    return view; 
} 
} 
+1

どのように断片が違うのか? –

+0

私は初心者私はちょうど私の月の経験....私はカスタムリストビューにjsonのデータを解析する方法を知っているが、それは断片のIMに少し混乱して来るとき。 –

答えて

0

することができますPARSこのような各フラグメントに

List<RidesData> ridesDatas; /* this is should be parcelable */ 
private void setupViewPager(ViewPager viewPager) { 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
    adapter.addFragment(FragmentAbout.newInstance(ridesDatas), "ABOUT"); 
    adapter.addFragment(FragmentPointOfInterest.newInstance(ridesDatas),"POI"); 

    viewPager.setAdapter(adapter); 
     } 

と設定constructure:このような電子

FragmentAbout.class

public static FragmentAbout newInstance(List<RidesData> ridesDatas) { 
     FragmentAbout fragmentAbout = new FragmentAbout(); 
     Bundle arg = new Bundle(); 
     arg.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) ridesDatas); 
     fragmentAbout.setArguments(arg); 
     return fragmentAbout; 
    } 

FragmentPointOfInterest.class

public static FragmentPointOfInterest newInstance(List<RidesData> ridesDatas) { 
     FragmentPointOfInterest fragmentPointOfInterest = new FragmentPointOfInterest(); 
     Bundle arg = new Bundle(); 
     arg.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) ridesDatas); 
     fragmentPointOfInterest.setArguments(arg); 
     return fragmentPointOfInterest; 
    } 

とJSONの各データを取得しますこのようなフラグメントの onViewCreated()メソッドで:

あなたはjsonに2ポジションのデータを持っています。

[0] = c_id=13 
[1] = c_id=14 

は、第1の断片に0位置設定:

RidesData data = ridesData.get(0); 

および第2の断片において1つのpostion設定:

RidesDataデータ= ridesData.get(1)。

@Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     ridesData = new ArrayList<>(); 
     ridesData = getArguments().getParcelableArrayList("data"); 
     RidesData data = ridesData.get(1);  
    } 
+0

ご回答いただきありがとうございましたが、これらのフラグメントは静的です。私の質問では動的なフラグメントが必要であることを申し訳ありません。ご使用のJSONオブジェクトにJSONオブジェクト(c_idが15かそれ以外のもの) addfragment()メソッドを使用して静的にフラグメントを作成せずに自動的に作成されたフラグメントに表示したいと思います。JSON配列に5つのJSONオブジェクトがあるかのように、私が望むのは、各JSONオブジェクトのコンテンツをそれぞれ表示する5つのフラグメントを自動的に作成する私の説明は明確です。何か助けていただければ幸いです。 –

+0

私は既に上記に言及しています。私の例ridesDataにはjsonデータが含まれています。私のrideDataオブジェクトの位置0 =最初のjsonオブジェクトデータ。ポジション1 = 2番目のオブジェクトデータ..メーターなしどれくらいのjsonオブジェクトを追加しますか?ポジションで取得できます。 –

+0

あなたはオブジェクトごとに複数のフラグメントを作成する必要はありません.Jsonオブジェクトの各位置を1つのフラグメントにフェッチできます。毎回jsonオブジェクトデータのperticular位置のみを表示します。 –

-1

jsonデータとビューホルダーを抽出してデータを印刷するには、カスタムアダプターを使用する必要があります。 これを参照してくださいlink

+0

これは、フラグメントの質問とは関係ありません。 – RileyManda

関連する問題