我想从片段发送到另一个片段的对象是“post”。 DemandFragment由列表视图组成,列表视图由作为帖子对象的项组成。
我需要将listview中的所选项目(在本例中为postArrayList.get(position))发送到SelectedPostFragment。
我尝试过捆绑但这不起作用......
有人知道如何解决这个问题吗?
DemandFragment:
public class DemandFragment extends Fragment {
ListView lv;
ArrayAdapter adapter;
ArrayList<Post> postArrayList;
private EditText editSearch;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_demand, container, false);
if(rootView != null){
lv = (ListView) rootView.findViewById(R.id.listDemand);
editSearch = (EditText) rootView.findViewById(R.id.search_post);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// clicked on item show post
Post selectedPost = postArrayList.get(position);
Bundle bundle = new Bundle();
bundle.putParcelable("data", (Parcelable) selectedPost);
FragmentManager fm = getActivity().getFragmentManager();
Fragment fragment = new rang.afterflight.fragments.SelectedPostFragment();
fragment.setArguments(bundle);
fm.beginTransaction().replace(R.id.content_main, fragment).commit();
}
});
}
searchPost();
return rootView;
}
public void searchPost(){
editSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
postArrayList = new ArrayList<Post>();
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> postList, ParseException e) {
if (e == null) {
for (ParseObject object : postList) {
Post newPost = new Post();
newPost.setAirportParse((String) object.get("airport"));
newPost.setDateParse((String) object.get("date"));
newPost.setTimeParse((String) object.get("time"));
newPost.setPersonsParse((String) object.get("persons"));
newPost.setAddressParse((String) object.get("address"));
newPost.setFlightnrParse((String) object.get("address"));
newPost.setUsername((String) object.get("username"));
newPost.setImageFile((ParseFile) object.get("profilepic"));
postArrayList.add(newPost);
}
adapter = new ListViewAdapter(getActivity(), R.layout.item_cardview, postArrayList);
lv.setAdapter(adapter);
}
}
});
}
}
SelectedPostFragment:
public class SelectedPostFragment extends Fragment {
TextView airportPost, datePost, timePost, personsPost, addressPost,
flightnrPost, postedbyPost, contactPost;
ImageView iv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_selectedpost, container, false);
airportPost = (TextView) rootView.findViewById(R.id.airport_post);
datePost = (TextView) rootView.findViewById(R.id.date_post);
timePost = (TextView) rootView.findViewById(R.id.time_post);
personsPost = (TextView) rootView.findViewById(R.id.persons_post);
addressPost = (TextView) rootView.findViewById(R.id.address_post);
flightnrPost = (TextView) rootView.findViewById(R.id.flightnr_post);
postedbyPost = (TextView) rootView.findViewById(R.id.postedby_post);
contactPost = (TextView) rootView.findViewById(R.id.contact_post);
Post selectedPost = getArguments().getParcelable("object");
String s = (String) selectedPost.get("airport");
Log.d("AIRPORT NAME", s);
return rootView;
}
}
发表:
@ParseClassName("Post")
public class Post extends ParseObject implements Serializable {
public Post(){
super();
}
public String getId(){
return getString("objectId");
}
public void setId(String id){
put("objectId", id);
}
//////////
public String getUsername(){
return getString("username");
}
public void setUsername(String username){
put("username", username);
}
public String getAirportParse(){
return getString("airport");
}
public void setAirportParse(String airport){
put("airport", airport);
}
//////////
public String getDateParse(){
return getString("date");
}
public void setDateParse(String date){
put("date", date);
}
//////////
public String getTimeParse(){
return getString("time");
}
public void setTimeParse(String time){
put("time", time);
}
//////////
public String getPersonsParse(){
return getString("persons");
}
public void setPersonsParse(String persons){
put("persons", persons);
}
//////////
public String getAddressParse(){
return getString("address");
}
public void setAddressParse(String address){
put("address", address);
}
public String getFlightnrParse(){
return getString("flightnr");
}
public void setFlightnrParse(String flightnr){
put("flightnr", flightnr);
}
public Bitmap getImageFile(){
Bitmap bmp = null;
ParseFile image = getParseFile("profilepic");
if(image != null){
try {
byte[] data = image.getData();
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (ParseException e) {
e.printStackTrace();
}
}
return bmp;
}
public void setImageFile(ParseFile file) {
if (file != null) {
put("profilepic", file);
}
}
}
答案 0 :(得分:1)
我相信您的Post
课程也需要实施Parcelable
,并将其传递到putParcelable()
的片段之间。
退房:Parcelable。
这也是一个很棒的example。
基本实施:
public class Post extends ParseObject implements Serializable, Parcelable {
...
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeString(getId());
out.writeSring(getUsername());
...
}
public static final Parcelable.Creator<Post> CREATOR
= new Parcelable.Creator<Post>() {
public Post createFromParcel(Parcel in) {
return new Post(in);
}
public Post[] newArray(int size) {
return new Post[size];
}
};
private Post(Parcel in) {
// Items must be read in the order they were written.
setId(in.readString());
setUsername(in.readString());
...
}
}
尝试使用大小,希望它有所帮助。
答案 1 :(得分:0)
将活动用作传输机制。
为要实现的活动创建一个接口,活动在实例化时传递给该碎片。每当您想传输数据时,请调用该接口的回调。协调活动需要与之交互的片段,然后将数据发布到将链接到片段的另一个回调中。
如果所有这些都太复杂,那么只需使用Otto并享受在任何地方发送事件,而不必担心在配置更改时分离/附加接口/侦听器。