简体   繁体   English

片段的自定义ListView只是在旋转Android Phone时出现

[英]Custom ListView in Fragment just appear when I rotate the Android Phone

I have created an Fragment to be the main feed of my app. 我创建了一个Fragment作为我应用程序的主要供稿。 The list data(ArrayList) is loaded from the main activity through the inner interface in fragment that Activity implements. 列表数据(ArrayList)是通过Activity实现的片段中的内部接口从主活动加载的。

My problems: 我的问题:

  1. My custom ListView just is shown when I rotate de phone. 我的自定义ListView仅在旋转电话时显示。
  2. OnCreateView and OnActivityCreate is called twice when I rotate. 旋转时会两次调用OnCreateView和OnActivityCreate。
  3. If ListView is shown after I rotate the phone and I click in, i get next error: 如果在旋转手机并单击后显示了ListView,则会收到下一个错误:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. java.lang.IllegalStateException:适配器的内容已更改,但ListView没有收到通知。 Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. 确保不从后台线程修改适配器的内容,而仅从UI线程修改。 Make sure your adapter calls notifyDataSetChanged() when its content changes. 确保适配器在其内容更改时调用notifyDataSetChanged()。 [in ListView(2131034209, class android.widget.ListView) with Adapter(class com.fb.newcomersapp.CustomListAdapter)] [在ListView(2131034209,类android.widget.ListView)和Adapter(类com.fb.newcomersapp.CustomListAdapter)中]

That is my main fragment methods: 那是我的主要片段方法:

public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    listView = (ListView) getView().findViewById(R.id.event_list_view);
    mCallback.parseQueryEvents();
    ArrayList<Event> eventList = mCallback.getEventList();
    Log.d("FeedActivity", eventList.size()+"");
    CustomListAdapter adapter = new CustomListAdapter(getActivity(), eventList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            mCallback.openEvent(position);

        }
    });

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.fragment_feed, container, false);

    textView = (TextView) view.findViewById(R.id.tv_header);
    textView.setText(mCallback.getLocal()); 
    return view;
}

The main activity methods: 主要活动方法:

public void parseQueryEvents () {
    eventList = new ArrayList<Event>();
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
    query.orderByAscending("startDate") ;
    query.whereGreaterThanOrEqualTo("endDate", new Date()) ;
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> parseEventList, ParseException e) {
            if (e == null) {
                ParseObject temp ;
                Iterator<ParseObject> i = parseEventList.iterator() ;
                while(i.hasNext()) {
                    temp = i.next();
                    eventList.add(new Event(temp)) ;
                }
            } else {
                Log.d("FeedActivity", e.getMessage());
            }
        }
    });
}

public ArrayList<Event> getEventList() {
    // TODO Auto-generated method stub
    return eventList;
}

Log: 日志:

02-04 13:36:06.844: D/GPS(7237): GPS 02-04 13:36:07.110: D/FeedActivity(7237): 0 02-04 13:36:07.145: D/GPS(7237): GPS 02-04 13:36:07.409: D/FeedActivity(7237): 0 02-04 13:36:07.420: I/Choreographer(7237): Skipped 39 frames! 02-04 13:36:06.844:D / GPS(7237):GPS 02-04 13:36:07.110:D / FeedActivity(7237):0 02-04 13:36:07.145:D / GPS(7237): GPS 02-04 13:36:07.409:D / FeedActivity(7237):0 02-04 13:36:07.420:I / Choreographer(7237):跳过39帧! The application may be doing too much work on its main thread. 该应用程序可能在其主线程上做太多工作。 02-04 13:36:07.451: W/View(7237): requestLayout() improperly called by android.widget.ListView{42079068 IFED.VC. 02-04 13:36:07.451:W / View(7237):android.widget.ListView {42079068 IFED.VC不正确地调用了requestLayout()。 ......ID -360,0-0,778 #7f050041 app:id/list_slidermenu} during layout: running second layout pass ......在布局过程中ID -360,0-0,778#7f050041 app:id / list_slidermenu}:运行第二个布局遍

So, anyone can help me? 那么,有人可以帮助我吗? Thx :D x

I ran into this problem a while ago when dealing with orientation changes. 前一段时间,我在处理方向变化时遇到了这个问题。 Sorry for not giving a full answer atm, but basically save the adapter (as well as the ListView if you'd like) as a class variable. 抱歉,没有提供完整的答案atm,但基本上将适配器(如果需要,还可以将ListView)保存为类变量。 Then, inside onActivityCreated, put the lines about creating and setting the adapter in a if statement checking if the adapter is null or not. 然后,在onActivityCreated内部,将有关创建和设置适配器的行放在if语句中,以检查适配器是否为null。 So, something like this... 所以像这样

private CustomListAdapter mAdapter; // Add this variable

public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    listView = (ListView) getView().findViewById(R.id.event_list_view);
    mCallback.parseQueryEvents();
    ArrayList<Event> eventList = mCallback.getEventList();
    Log.d("FeedActivity", eventList.size()+"");

    // Add this conditional
    if(mAdapter == null) {
       CustomListAdapter adapter = new CustomListAdapter(getActivity(), eventList);
       listView.setAdapter(adapter);
    }

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            mCallback.openEvent(position);

        }
    }
}

I believe the issue originates somewhere along the line where it's trying to save us all some time by caching variables (like views with ids such as the ListView) when recreating the Activity+Fragment- however, in the midst of all that, setting a new adapter causes an issue. 我相信问题出在某个地方,它在重新创建Activity + Fragment时试图通过缓存变量(例如具有ID的视图(例如ListView)来缓存),从而节省了我们所有的时间,但是,在所有这些设置中,适配器导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM