我是 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;
}
}
}