0

我是 Android 新手,遇到片段问题。我有六个不同的片段。在其中一个中,我有一个使用库“通用图像加载器”的来自 url 的图像库。当我更改片段并返回时,问题就来了。每次我更改并返回时,图像列表都会翻倍和翻倍。我花了几个小时解决这个问题,但找不到解决方案。

非常感谢!

public static class DummySectionFragmentNews extends ListFragment {


        ArrayList<String> titulos = new ArrayList<String>();

        ArrayList<String> Imagenes = new ArrayList<String>();


        String[] titulosFinal;

        String[] ImagenesFinal;


        ListView list;
        ImageLoader loader = ImageLoader.getInstance();

        final Context context = this.getActivity();
        DisplayImageOptions op;

        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragmentNews() {
        }

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

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

            String noticias = "";
            SharedPreferences prefs = this.getActivity().getSharedPreferences("ApplicationInfo",Context.MODE_PRIVATE);
            noticias = prefs.getString("Noticias", "");

            try {
                JSONArray news = new JSONArray(noticias);
                //System.out.println("JSON NOTICIAS :" + news.length());

                for (int i = 0; i < news.length(); i++) {

                    JSONObject jsonObject = news.getJSONObject(i);
                    String titulo = jsonObject.getString("title");

                    String imagen = jsonObject.getString("image");


                    titulos.add(titulo);

                    Imagenes.add(imagen);


                  }



            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            titulosFinal = new String[titulos.size()];
            titulosFinal = titulos.toArray(titulosFinal);



            ImagenesFinal = new String[Imagenes.size()];
            ImagenesFinal = Imagenes.toArray(ImagenesFinal);




            loader.init(ImageLoaderConfiguration.createDefault(this.getActivity()));

            op = new DisplayImageOptions.Builder()
                    .showStubImage(R.drawable.ic_stub)
                    .showImageForEmptyUri(R.drawable.ic_empty)
                    .displayer(new RoundedBitmapDisplayer(20))
                    .build();


            list = (ListView) getActivity().findViewById(android.R.id.list);
            ((ListView) list).setAdapter(new ItemAdapter());
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //tartImagePagerActivity(position);

                  Intent i=new Intent(context,SeparateView.class);
                  i.putExtra("pos",position+"");
                  startActivity(i);
                }
            });
          }

         public void onBackPressed() {
                  onBackPressed();
         }

         class ItemAdapter extends BaseAdapter {

                private class ViewHolder {
                    public TextView text;
                    public ImageView image;
                }

                @Override
                public int getCount() {
                    return ImagenesFinal.length;
                }

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

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

                @Override
                public View getView(final int position, View convertView, ViewGroup parent) {
                    View v = convertView;
                    final ViewHolder holder;

                    System.out.println("Hello " + position);

                    if (convertView == null) {
                        v = getActivity().getLayoutInflater().inflate(R.layout.image_list_layout, parent, false);
                        holder = new ViewHolder();
                        holder.text = (TextView) v.findViewById(R.id.text);
                        holder.image = (ImageView) v.findViewById(R.id.image);
                        v.setTag(holder);
                    } else {
                        holder = (ViewHolder) v.getTag();
                    }

                    holder.text.setText(titulosFinal[position]);
                    loader.displayImage(ImagenesFinal[position], holder.image, op, null);

                    return v;
                }
            }

    }
4

2 回答 2

0

将下面的行放在onActivityCreated()后面super.onActivityCreated(savedInstanceState);

Imagenes = new ArrayList<String>();

评论我的结果

于 2013-10-16T11:52:01.353 回答
0

改变你的方法

public void onActivityCreated(Bundle savedInstanceState) 

public void onCreate(Bundle savedInstanceState) 

它应该修复它。

onActivityCreated每次在 Fragment 之间切换时都会调用,因此每次都会将图片添加到列表中。onCreate一开始只调用一次。在此处查看有关 Fragment 生命周期的更多信息:http: //developer.android.com/guide/components/fragments.html

于 2013-10-16T11:55:53.723 回答