onitemclick随机

时间:2017-10-15 13:31:15

标签: java android listview

我在点击期间随机收到此错误,偶尔也不会发生这种情况。

java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知。确保不从后台线程修改适配器的内容,而只是从UI线程修改。确保适配器在内容更改时调用notifyDataSetChanged()。

import android.content.*;
import android.os.*;
import android.support.v4.app.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;


import java.util.*;
import org.json.*;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.theappguru.fixturesync.Settings;

import static com.google.android.gms.internal.zzahg.runOnUiThread;


public class menufragment extends Fragment {


    private String TAG = MainActivity.class.getSimpleName();
    public ListView list;
    public ArrayList<menufragmentinfos> countries = new ArrayList<menufragmentinfos>();
    public menufragmentadapter adapter;
    private MainActivity mActivity;
    private FirebaseAnalytics mFirebaseAnalytics;

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);





    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(com.theappguru.fixturesync.R.layout.menufragment, container, false);
    }

    private class Wrapper
    {

        String Title = "Undefinded";

    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        LinearLayout loading = (LinearLayout) getView().findViewById(com.theappguru.fixturesync.R.id.loading);
        loading.setVisibility(View.VISIBLE);

        mFirebaseAnalytics = FirebaseAnalytics.getInstance(getActivity());

        list = (ListView) getView().findViewById(com.theappguru.fixturesync.R.id.list);
        adapter = new menufragmentadapter(this);

        list.setAdapter(adapter);


        list.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                String type = countries.get(position).type;
                String data = countries.get(position).data;
                String link = countries.get(position).link;
                String availability = countries.get(position).availability;
                String date = countries.get(position).date;
                String name = countries.get(position).name;




                if (type.equals("fixture")){
                    mActivity = (MainActivity) getActivity();

                    Bundle bundle = new Bundle();
                    bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
                    bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, type);
                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

                    String url = link;
                    String username = "";
                    String password = "";
                    Settings settings = mActivity.getSettings();
                    settings.putString(Settings.PREF_LASTURL, url);

                    SharedPreferences msharedpref = getActivity().getSharedPreferences("calendar" , 0);
                    SharedPreferences.Editor editor = msharedpref.edit();
                    editor.putString("link", url);
                    editor.putString("name", name);
                    editor.putString("type", "");
                    editor.apply();


                    mActivity.setSource(url, null, username, password);
                    mActivity.test();}


                if (type.equals("menu")){

                    if (availability.equals("not available")){
                        Toast.makeText(getActivity().getApplicationContext(),
                                "Fixtures Not Published Yet Please Check Back After "+date,
                                Toast.LENGTH_LONG)
                                .show();
                    }
                    else {

                        try {


                            Bundle bundle1 = new Bundle();
                            bundle1.putString("message", countries.get(position).data);
                            menufragment fragInfo = new menufragment();
                            fragInfo.setArguments(bundle1);

                            FragmentManager fm = getFragmentManager();
                            FragmentTransaction fragmentTransaction = fm.beginTransaction();
                            fragmentTransaction.add(com.theappguru.fixturesync.R.id.changeFragment, fragInfo);
                            fragmentTransaction.addToBackStack("tag");
                            fragmentTransaction.commit();


                        }
                        catch (Exception e) {
                            Log.e("menu", e.toString());
                        }

                    }
                }

                // Making a request to url and getting response




            }
        });










        new GetContacts().execute();




        super.onViewCreated(view, savedInstanceState);
    }




    private class GetContacts extends AsyncTask<Void, Void, Wrapper> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();


        }

        @Override
        protected Wrapper doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            Wrapper w = new Wrapper();
            String Title = "a";




            String jsonStr = getArguments().getString("message");





            Log.e(TAG, "Response from url: " + jsonStr);



            if (jsonStr != null) {
                try {





                    countries.clear();

                    JSONObject data_array=new JSONObject(jsonStr);

                    JSONArray phone = data_array.getJSONArray("data");








                    for (int i = 0 ; i < phone.length() ; i++) {
                        JSONObject obj = new JSONObject(phone.get(i).toString());

                        menufragmentinfos add = new menufragmentinfos();
                        add.name = obj.getString("name");

                        String type = obj.getString("type");
                        String availability = obj.optString("availability");
                        String date = obj.optString("date");


                        add.type = type;
                        add.availability = availability;
                        add.date = date;
                        if (type.equals("menu")){
                            add.data = (obj.toString());
                        }
                        if (type.equals("fixture")){
                            add.link = obj.getString("link");
                        }









                        countries.add(add);


                    }



                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.toString());



                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");


            }

            return w;
        }


        @Override
        protected void onPostExecute(Wrapper w) {
            super.onPostExecute(w);

            runOnUiThread(new Runnable()
            {
                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                }});



            LinearLayout loading = (LinearLayout) getView().findViewById(com.theappguru.fixturesync.R.id.loading);
            loading.setVisibility(View.GONE);


        }


    }







    @Override
    public void onPause() {
        runOnUiThread(new Runnable()
        {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }});
        super.onPause();

    }

    /** Called when returning to the activity */
    @Override
    public void onResume() {

        runOnUiThread(new Runnable()
        {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }});
        super.onResume();



    }

    /** Called before the activity is destroyed */
    @Override
    public void onDestroy() {

        super.onDestroy();


    }





}

适配器

import android.content.*;
import android.view.*;
import android.widget.*;

public class menufragmentadapter extends BaseAdapter {

    private RelativeLayout background;

    menufragment main;

    menufragmentadapter(menufragment main)
    {
        this.main = main;
    }

    @Override
    public int getCount() {
        return  main.countries.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolderItem {
        TextView name;


    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolderItem holder = new ViewHolderItem();
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) main.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(com.theappguru.fixturesync.R.layout.menufragmentcells, null);

            holder.name = (TextView) convertView.findViewById(com.theappguru.fixturesync.R.id.name);


            convertView.setTag(holder);
        }
        else
        {




            holder = (ViewHolderItem) convertView.getTag();
        }


        holder.name.setText(this.main.countries.get(position).name);














        return convertView;


    }



}

日志

  

15 14:22:11.977 16877-16877 / com.theappguru.fixturesync E / AndroidRuntime:FATAL EXCEPTION:main                                                                               过程:com.theappguru.fixturesync,PID:16877                                                                               java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知。确保不从后台线程修改适配器的内容,而只是从UI线程修改。确保适配器在其内容更改时调用notifyDataSetChanged()。 [在ListView(2131361839,类android.widget.ListView)中使用Adapter(类com.theappguru.fixturesync.ui.menufragmentadapter)]                                                                                   在android.widget.ListView.layoutChildren(ListView.java:1623)                                                                                   在android.widget.AbsListView.onLayout(AbsListView.java:2205)                                                                                   在android.view.View.layout(View.java:17564)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5654)                                                                                   在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)                                                                                   在android.view.View.layout(View.java:17564)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5654)                                                                                   在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                   在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                   在android.view.View.layout(View.java:17564)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5654)                                                                                   在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)                                                                                   在android.view.View.layout(View.java:17564)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5654)                                                                                   在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                   在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                   在android.view.View.layout(View.java:17564)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5654)                                                                                   在com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)                                                                                   在android.view.View.layout(View.java:17564)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5654)                                                                                   在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                   在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                   在com.android.internal.policy.DecorView.onLayout(DecorView.java:725)                                                                                   在android.view.View.layout(View.java:17564)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5654)                                                                                   在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2406)                                                                                   在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2126)                                                                                   在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1286)                                                                                   在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6536)                                                                                   在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:871)                                                                                   在android.view.Choreographer.doCallbacks(Choreographer.java:683)                                                                                   在android.view.Choreographer.doFrame(Choreographer.java:619)                                                                                   在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:857)                                                                                   在android.os.Handler.handleCallback(Handler.java:751)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                   在android.os.Looper.loop(Looper.java:154)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:6247)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:872)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

1 个答案:

答案 0 :(得分:-1)

您的doInBackground方法正在修改适配器的内容(清除列表并再次填充它),同时您的适配器也引用相同的变量。对适配器内的国家/地区列表使用不同的引用,并将其填充到PostExecute方法。