2016-09-16 2 views
1

カスタムArrayListをアクティビティからフラグメントに渡そうとしていますが、コードに何が問題なのかわかりません。エラーはありませんが、私は期待される出力を得ることができません。 そして、私はほぼ3日間それを直すように努力して完全に失われています(はい、私は初心者です)。私はそれを解決するのを助けてください。目的は、別のアクティビティのためにフラグメントを使用したいということです。arraylistで分解可能<CustomClass>アクティビティからフラグメントに渡す値android

public class CityDetailsActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Create a list of City Details 
     ArrayList<CityDetails> cityDetail = new ArrayList<CityDetails>(); 
     cityDetail.add(new CityDetails("information", "near", "local food", R.drawable.img1, R.drawable.img1, R.drawable.img6)); 
     Bundle args = new Bundle(); 
     args.putParcelableArrayList("key", cityDetail); 
     CityInformationFragment myFragment = new CityInformationFragment(); 
     myFragment.setArguments(args); 

     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.fragment_main); 
     ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager); 
     FragmentPagerAdapter adapterViewPager; 
     adapterViewPager = new CityDetailsPagerAdapter(getSupportFragmentManager()); 
     vpPager.setAdapter(adapterViewPager); 
     // Give the TabLayout the ViewPager 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 
     tabLayout.setupWithViewPager(vpPager); 
    } 
} 

そして、私のカスタムParcelableクラスは、下の通りです:

は、ここに私の活動クラスの

public class CityDetails implements Parcelable { 
    private String mInformation; 
    private String mNearPlace; 
    private String mLocalFood; 
    private int mItemImageResourceId; 
    private int mNear_place_image_ResourceId; 
    private int mLocal_food_image_ResourceId; 

    public CityDetails(String information, String nearPlace, String localFood, int itemImageResourceId 
      , int near_place_image_ResourceId, int local_food_image_ResourceId) { 
     mInformation = information; 
     mNearPlace = nearPlace; 
     mLocalFood = localFood; 
     mItemImageResourceId = itemImageResourceId; 
     mNear_place_image_ResourceId = near_place_image_ResourceId; 
     mLocal_food_image_ResourceId = local_food_image_ResourceId; 

    } 

    @Override 
    public void writeToParcel(Parcel out, int flags) { 
     out.writeString(mInformation); 
     out.writeString(mNearPlace); 
     out.writeString(mLocalFood); 
     out.writeInt(mItemImageResourceId); 
     out.writeInt(mNear_place_image_ResourceId); 
     out.writeInt(mLocal_food_image_ResourceId); 

    } 

    private CityDetails(Parcel in) { 
     mItemImageResourceId = in.readInt(); 
     mNear_place_image_ResourceId = in.readInt(); 
     mLocal_food_image_ResourceId = in.readInt(); 
     mNearPlace = in.readString(); 
     mLocalFood = in.readString(); 
     mInformation = in.readString(); 
    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    // After implementing the `Parcelable` interface, we need to create the 
    // `Parcelable.Creator<MyParcelable> CREATOR` constant for our class; 
    // Notice how it has our class specified as its type. 
    public static final Parcelable.Creator<CityDetails> CREATOR 
      = new Parcelable.Creator<CityDetails>() { 

     // This simply calls our new constructor (typically private) and 
     // passes along the unmarshalled `Parcel`, and then returns the new object! 
     @Override 
     public CityDetails createFromParcel(Parcel in) { 
      return new CityDetails(in); 
     } 

     // We just need to copy this and change the type to match our class. 
     @Override 
     public CityDetails[] newArray(int size) { 
      return new CityDetails[size]; 
     } 
    }; 

    public String getInformation() { 
     return mInformation; 
    } 

    public String getNearPlace() { 
     return mNearPlace; 
    } 

    public String getLocalFood() { 
     return mLocalFood; 
    } 

    public int getItemImageResourceId() { 
     return mItemImageResourceId; 
    } 

    public int getNear_place_image_ResourceId() { 
     return mNear_place_image_ResourceId; 
    } 

    public int getLocal_food_image_ResourceId() { 
     return mLocal_food_image_ResourceId; 
    } 
} 

そして最後に、私の断片である:

public class CityInformationFragment extends Fragment { 

    public CityInformationFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.city_list, container, false); 

     Bundle bundle = getArguments(); 

     if ((bundle != null)) { 
      ArrayList<CityDetails> city = getArguments().getParcelable("key"); 

      // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The 
      // adapter knows how to create list items for each item in the list. 
      CityDetailsAdapter adapter = new CityDetailsAdapter(getActivity(), city); 

      // Find the {@link ListView} object in the view hierarchy of the {@link Activity}. 
      // There should be a {@link ListView} with the view ID called list, which is declared in the 
      // word_list.xml layout file. 
      ListView listView = (ListView) rootView.findViewById(R.id.city_list); 

      // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the 
      // {@link ListView} will display list items for each {@link Word} in the list. 
      listView.setAdapter(adapter); 
     } 

     return rootView; 
    } 

} 

EDIT: は、ここに私のFragmentPagerDapter

public class CityDetailsPagerAdapter extends FragmentPagerAdapter { 
final int PAGE_COUNT = 1; 
private String tabTitles[] = new String[] { "City Information"}; 
private Context context; 

public CityDetailsPagerAdapter(FragmentManager fm) { 
    super(fm); 
} 

@Override 
public Fragment getItem(int position) { 
    return new CityInformationFragment(); 
} 
@Override 
public CharSequence getPageTitle(int position) { 
    // Generate title based on item position 
    return tabTitles[position]; 
} 


@Override 
public int getCount() { 
    return PAGE_COUNT; 
} 

} されて、ここに私のCityDetailsAdapterです:

public class CityDetailsAdapter extends ArrayAdapter<CityDetails> { 
// View lookup cache 
private static class ViewHolder { 
    TextView cityInformation; 
    ImageView city_item_image; 
    TextView city_near_place_Information; 
    ImageView city_item_near_place_image; 
    TextView city_local_food_Information; 
    ImageView city_item_local_food_image; 

} 

public CityDetailsAdapter(Context context, ArrayList<CityDetails> cityDetails) { 
    super(context, 0, cityDetails); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Get the data item for this position 
    CityDetails currentCity = getItem(position); 
    // Check if an existing view is being reused, otherwise inflate the view 
    ViewHolder viewHolder; // view lookup cache stored in tag 
    if (convertView == null) { 
     // If there's no view to re-use, inflate a brand new view for row 
     viewHolder = new ViewHolder(); 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     convertView = inflater.inflate(R.layout.city_list_item, parent, false); 
        /*Find the TextView and ImageView and set them on the VIewHolder*/ 
     viewHolder.cityInformation = (TextView) convertView.findViewById(R.id.cityInformation); 
     viewHolder.city_near_place_Information = (TextView) convertView.findViewById(R.id.city_near_place_Information); 
     viewHolder.city_local_food_Information = (TextView) convertView.findViewById(R.id.city_local_food_Information); 
     viewHolder.city_item_image = (ImageView) convertView.findViewById(R.id.city_item_image); 
     viewHolder.city_item_near_place_image = (ImageView) convertView.findViewById(R.id.city_item_near_place_image); 
     viewHolder.city_item_local_food_image = (ImageView) convertView.findViewById(R.id.city_item_local_food_image); 

     // Cache the viewHolder object inside the fresh view 
     convertView.setTag(viewHolder); 
    } else { 
     // View is being recycled, retrieve the viewHolder object from tag 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
    // Populate the data into the template view using the data object 
    viewHolder.cityInformation.setText(currentCity.getInformation()); 
    viewHolder.city_item_image.setImageResource(currentCity.getItemImageResourceId()); 
    viewHolder.city_near_place_Information.setText(currentCity.getNearPlace()); 
    viewHolder.city_local_food_Information.setText(currentCity.getLocalFood()); 
    viewHolder.city_item_near_place_image.setImageResource(currentCity.getNear_place_image_ResourceId()); 
    viewHolder.city_item_local_food_image.setImageResource(currentCity.getLocal_food_image_ResourceId()); 

    return convertView; 
} 

は私が作っている間違いをできるだけ具体的に。初心者であるため、私はいくつかの概念を理解することができないかもしれませんが、私は試してみます。とにかく、これは私の最初の質問です。だから、私は問題を正しく伝えていないかもしれません。 あなたの助けが大歓迎です。おかげさまで

+0

あなたはプラグインを使ってパースブルコードを生成していますか? –

+0

いいえ、私は自分でクラスを作成しました(チュートリアルをオンラインで読んだ後に)。 –

+0

多分あなたは 'getParcelable'の代わりに' getParcelableArrayList'を使うべきです – matejko219

答えて

1

問題は、あなたのFragmentPagerAdapterクラスであると思われます。 getItem()メソッドによって返されたフラグメントを介して引数を渡すのではなく、渡された引数がないフラグメントオブジェクトを返します。

そこで

@Override 
public Fragment getItem(int position) { 
    CityInfomationFragment fragment = new CityInformationFragment(); 
    Bundle args = new Bundle(); 
    args.putParcelableArrayList("key", your_array_list); 
    fragment.setArguments(args); 
    return fragment; 
} 

活動からFragmentPagerAdapterクラスのコンストラクタにarray_listを渡すの下に方法を変更し、フィールド変数を使用してそれを保存してのgetItem()メソッドで参照

EDIT

アクティビティで作成したフラグメントインスタンスは、フラグメントトランザクションを実行しない限り、まったく効果がありません。そのフラグメントインスタンスをFragmentPagerAdaterに渡し、getItem()メソッドから返すことで、そのフラグメントをViewPagerに表示することができます。

+0

私は上記の答え@Abhishek Rajに同意しますあなたはあなたの 'Activity'に' CityInformationFragment'の新しいインスタンスを作成していくつかの引数を設定していますが、あなたのページャアダプタは、引数を設定せずに 'CityInformationFragment'の新しいインスタンスを作成するだけです。 –

+0

Samuel Robertに感謝します。特に「EDIT」は本当に役に立ちました。それを正しくするには3時間ほどかかりましたが、最後にあなたの答えが私を助けました。そして今、私は自信を持って複数の活動のために断片を再利用できます。ありがとう@マークキーン。 –

0

行の下にしてみ

ArrayList<CityDetails> city = getArguments().getParcelableArrayList("key"); 
+0

お返事ありがとうございますが、私はほぼ4日間試しています。今でも試してみましたが、それは助けにはなりませんでした。そして、これは私の紅茶ではないようです。たぶん私のアダプタクラスにいくつかの欠陥があるので、私はそれらのコードで質問を更新しています。何か問題があるかどうか確認してください。 –

+0

あなたは下のリンクを通過できますか?ここで私はそれを使用し、https://github.com/subbuboyapati/MovieMasti/blob/master/app/src/main/java/com/subbu/moviemasti/fragment/ReviewFragment.java –

+0

実際に私は同じフラグメントを再利用したいだから私は "明示的に名前を付けて他のアクティビティオブジェクトを書く"とは思っていません、私はちょうどそれが呼び出されたアクティビティからカスタムのArrayListを取得したいと思っています。ありがとう、しかし、今私は完全に失われて何も得られませんでした。 –

関連する問題