1

在我的Listfragment 中,我可以从在线服务器(1,2,3,4)加载我的JSON结果,但是当我按下主页按钮并返回我的应用程序时......现在我的Listview 有一个重复的列表 (1,2,3, 4,1,2,3,4),有时在旋转屏幕时会发生这种情况。

我读了很多......并且有很多帖子,但情况不同,所以这是我的情况......

我的用户界面... ViewPager > ListFragment

ListFragment:加载器、AsyncTaskLoader、BaseAdapter、JSON、HashMap

不使用/不是我的情况: Cursors、SQLite、ContentProvider、Activity、ListActivity、String[]

是的...我将 httpclient 更改为 httpurlconnection :)

并且变得更好......现在当我从另一个 Activity 回到我的 ListFragment 时,我选择了一个项目......我的应用程序崩溃!!!,LogCat 说: “适配器的内容已更改,但 ListView 没有收到通知. 确保您的适配器的内容不是从后台线程修改的,而只是从 UI 线程修改的”。

我想尝试使用runOnUiThread(),但显然不适用于 ListFragment,但 ListActivity ......不是我的情况。

简历:我觉得这两个问题是因为我解决不了好,onLoadingBackground()和onLoadingFinish()的过程……我不确定,我看不到!:S

而且...我正在使用android:configChanges="orientation|screenSize"并且...我感觉很糟糕,这是作弊!xD

以防万一...我没有使用 Volley,因为我想了解更多有关 JAVA/Android 逻辑的信息,并且因为如果我对 Volley 有任何疑问...回答问题的人会更少:S

我不想问这个,但是......几个小时前我读了一篇文章,上面写着:“我被困了一个多小时”。

什么???而且我被困在这三个多星期了,因为我正在尝试很多方法!!!:S(对不起我的英语不好......我正在努力,我会说西班牙语)

public class miNewFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Model>> {

    static ParseadorJSON_func ParseadorJSON_objeto = new ParseadorJSON_func();
    static ArrayList<HashMap<String, String>> arrayListaCompleta;
    private static String urlPagina2 = "http://www.domain.com/json/mi_json.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CATEGORIAS = "categorias";
    private static final String TAG_PID = "idItem";
    private static final String TAG_NAME = "name";
    static JSONArray jsonCategorias = null;
    private ListView miListView;
    myBaseAdapter miAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.categorias, container, false);
        miListView = (ListView)rootView.findViewById(android.R.id.list);
        arrayListaCompleta = new ArrayList<HashMap<String, String>>();
        Log.e("onCreateView = ", "miListView > arrayListacompleta > return rootView");
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        myBaseAdapter miAdapter = new myBaseAdapter(getActivity());
        miListView.setAdapter(miAdapter);
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(0, null, this);
        Log.e("onActivityCreated = ", "myBaseAdapter > setAdapter > getLoaderManager");
    }

    @Override
    public Loader<List<Model>> onCreateLoader(int arg0, Bundle arg1) {
        Log.e("Loader/onCreateLoader = ", "myAsyncTaskLoader");
        return new myAsyncTaskLoader(getActivity());
    }

    @Override
    public void onLoadFinished(Loader<List<Model>> arg0, List<Model> data) {
        miAdapter = new myBaseAdapter(getActivity());
        miListView.setAdapter(miAdapter);
        miAdapter.notifyDataSetChanged();
        Log.e("Loader/onLoadFinished = ", "myBaseAdapter > setAdapter > notifyDataSetChanged");
    }

    @Override
    public void onLoaderReset(Loader<List<Model>> arg0) {
        Log.e("Loader/onLoaderReset = ", "Nothing");
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.e("onListItemClick = ", "Nothing");

    }



    public class myBaseAdapter extends BaseAdapter {    
        private Context mContext;
        public myBaseAdapter(Context c) {
            mContext = c;
        }
        public int getCount() {
            return arrayListaCompleta.size();
        }
        public Object getItem(int position) {
            return null;
        }
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                holder = new Holder();
                LayoutInflater ltInflate = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
                convertView = ltInflate.inflate(R.layout.categorias_list_item, null);
                holder.h_categTag = (TextView) convertView.findViewById(R.id.categTag);
                holder.h_categIdItem = (TextView) convertView.findViewById(R.id.categIdItem);
                convertView.setTag(holder); 
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.h_categTag.setText( arrayListaCompleta.get(position).get("name").toString() );
            holder.h_categIdItem.setText( arrayListaCompleta.get(position).get("idItem").toString() );
            return convertView;
        }
        class Holder {
            TextView h_categTag;
            TextView h_categIdItem;
        }
    }

    public static class myAsyncTaskLoader extends AsyncTaskLoader<List<Model>> {
        List<Model> mModels;
        public myAsyncTaskLoader(Context context) {
            super(context);
        }
        @Override
        public List<Model> loadInBackground() {
            Log.e("Async/loadInBackground = ", "Creating Array from JSON");  
            List<NameValuePair> arrayDelServidor = new ArrayList<NameValuePair>();
            JSONObject jsonCompleto = ParseadorJSON_objeto.makeHttpRequest(urlPagina2, "GET", arrayDelServidor);
            try {
                int success = jsonCompleto.getInt(TAG_SUCCESS);
                if (success == 1) { 
                    jsonCategorias = jsonCompleto.getJSONArray(TAG_CATEGORIAS);
                    for (int i = 0; i < jsonCategorias.length(); i++) {
                        JSONObject jsonFila = jsonCategorias.getJSONObject(i);
                        String id = jsonFila.getString(TAG_PID);
                        String name = jsonFila.getString(TAG_NAME);
                        HashMap<String, String> mapaDatos = new HashMap<String, String>();
                        mapaDatos.put(TAG_PID, id);
                        mapaDatos.put(TAG_NAME, name);
                        arrayListaCompleta.add(mapaDatos);
                    }  
                } else {
                    //nada
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public void deliverResult(List<Model> listOfData) {
            if (isReset()) {
                Log.e("Async/deliverResult = ", "isReset()");
                if (listOfData != null) {
                    onReleaseResources(listOfData);
                }
            }
            List<Model> oldApps = listOfData;
            mModels = listOfData;
            if (isStarted()) {
                Log.e("Async/deliverResult = ", "isStarted()");
                super.deliverResult(listOfData);
            }
            if (oldApps != null) {
                Log.e("Async/deliverResult = ", "oldApps");
                onReleaseResources(oldApps);
            }
        }

        @Override
        protected void onStartLoading() {
            if (mModels != null) {
                deliverResult(mModels);
                Log.e("Async/onStartLoading = ", "mModels != null");
            }
            if (takeContentChanged()) {
                forceLoad();
                Log.e("Async/onStartLoading = ", "takeContentChanged()");
            }
            if (mModels == null) {
                forceLoad();
                Log.e("Async/onStartLoading = ", "mModels == null");
            }
        }

        @Override
        protected void onStopLoading() {
            cancelLoad();
            Log.e("Async/onStopLoading = ", "cancelLoad()");
        }

        @Override
        public void onCanceled(List<Model> apps) {
            super.onCanceled(apps);
            onReleaseResources(apps);
            Log.e("Async/onCanceled = ", "onReleaseResources(apps)");
        }

        @Override
        protected void onReset() {
            super.onReset();
            onStopLoading();
            if (mModels != null) {
                onReleaseResources(mModels);
                mModels = null;
                Log.e("Async/onReset = ", "onReleaseResources(mModels)");
            }
        }

        protected void onReleaseResources(List<Model> apps) {
            Log.e("Async/onReleaseResources = ", "Entró pero no hizo nada");
        }

    }
}

如果有人需要这些代码...

final class Model {
    private String name;
    private String id;
    public Model(String name, String id) {
        this.name = name;
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

        public static class ParseadorJSON_func {
        InputStream is = null;
        JSONObject jObj = null;
        String json = "";
        public ParseadorJSON_func() {
        }

        public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> arrayDesdeServidor) {
            try {
                if(method == "POST"){
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setEntity(new UrlEncodedFormEntity(arrayDesdeServidor));
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }else if(method == "GET"){
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    String paramString = URLEncodedUtils.format(arrayDesdeServidor, "utf-8");
                    url += "?" + paramString;
                    HttpGet httpGet = new HttpGet(url);
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }          

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
            return jObj;
        }
    }

.

编辑:loadInBackground()

当我从 Activity 返回到此 Listfragment 并选择了一个项目时,应用程序崩溃了,因为我的 loadInBackground 更改了适配器的内容并且没有更新内容。现在,如果其他情况...应用程序不会崩溃,因为它检查适配器是否为空,然后运行 ​​loadInBackground 与否,但是...

我认为我这样做的方式......也许不是最合适的方式,我还没有到达那里,但我认为当我想要更新或添加更多结果时......这种方式可能行不通。任何帮助/示例?

        @Override
        public List<Model> loadInBackground() {
            Log.e("Async/loadInBackground = ", "Creating Array from JSON");
            //NEW if else...
            if(  miAdapter != null  ){
                adaptadorEstado = "No es Null";
            }else{
                adaptadorEstado = "Si es Null";

                List<NameValuePair> arrayDelServidor = new ArrayList<NameValuePair>();
                JSONObject jsonCompleto = ParseadorJSON_objeto.makeHttpRequest(urlPagina2, "GET", arrayDelServidor);
                try {
                    int success = jsonCompleto.getInt(TAG_SUCCESS);
                    if (success == 1) { 
                        jsonCategorias = jsonCompleto.getJSONArray(TAG_CATEGORIAS);
                        for (int i = 0; i < jsonCategorias.length(); i++) {
                            JSONObject jsonFila = jsonCategorias.getJSONObject(i);
                            String id = jsonFila.getString(TAG_PID);
                            String name = jsonFila.getString(TAG_NAME);
                            HashMap<String, String> mapaDatos = new HashMap<String, String>();
                            mapaDatos.put(TAG_PID, id);
                            mapaDatos.put(TAG_NAME, name);
                            arrayListaCompleta.add(mapaDatos);
                        }  
                    } else {
                        //nada
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }//end new if else
            Log.e("Async/loadInBackground = ", "Adaptador > "+adaptadorEstado);
            return null;
        }

.

新: onConfigurationChanged()

因为我一直在作弊,所以android:configChanges="orientation|screenSize"我尝试了,onConfigurationChanged()但它只在它configChanges停留在清单上时才有效/响应。所以我认为这个问题与我尝试解决 loadInBackground() 的方式有关,我不确定。

另一方面......我读了很多关于onCreate我的MainActivity的make,一个if来检查是否savedInstanceState为null,然后使用'getSupportFragmentManager().beginTransaction().findFragmentByTag()'之类的东西,当我尝试...事实证明这件事不起作用,因为在我的 MainActivity 上我有miPagerAdapter = new funcionPagerAdapter(getSupportFragmentManager());,我认为也许在我的情况下......它用另一种方式解决......但我不知道如何!:S

//In my ListFragment...
@Override
public void onConfigurationChanged(Configuration congfValues) {
    super.onConfigurationChanged(congfValues);
    Log.e("onConfigurationChanged = ", "Si entró");
    if(  miAdapter != null  ){
        Log.e("onConfigurationChanged = ", "Adaptador Lleno");
    }else{
        miAdapter = new myBaseAdapter(getActivity());
        miListView.setAdapter(miAdapter);
        Log.e("onConfigurationChanged = ", "Adaptador estaba Vacío (ya no)");
    }
}


//In my MainActivity (onCreate)
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_viewpager);

    Fragment_categorias miFragCategoria;
    if (savedInstanceState!= null) {
        miFragCategoria = (Fragment_categorias) getSupportFragmentManager().findFragmentByTag("tagCategoria");
    } else {
        miFragCategoria = new Fragment_categorias();
        getSupportFragmentManager().beginTransaction().add(R.id.(i dont know), miFragCategoria, "tagCategoria").commit(); 
    }

    //viewpager
    miPagerAdapter = new funcionPagerAdapter(getSupportFragmentManager());
    miViewPager = (ViewPager) findViewById(R.id.vpContenedor);
    miViewPager.setOffscreenPageLimit(5);
    miViewPager.setAdapter(miPagerAdapter);
}
4

0 回答 0