I'm having a very slow scrolling on my listview which has some text and an image thumbnail, the information is obtained by a json webservice. This is my class that creates the listview:
public class NoticiasFragment extends Fragment{
private static final String TAG = "NoticiasFragment";
private String resultado;
private List<HashMap<String, String>> mAndroidMapList = new ArrayList<>();
private ListView mListView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.noticias_tab, container, false);
/*ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBar.setTitle("Noticias");
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));*/
Noticia noticia = new Noticia();
try {
resultado = noticia.execute("some-url").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
armarListaNoticias(resultado, inflater, container, view);
return view;
}
protected void armarListaNoticias(String resultado, LayoutInflater inflater, ViewGroup container, View view){
mListView = (ListView) view.findViewById(R.id.listaNoticias);
try {
JSONArray jArray = new JSONArray(resultado);
String titulo = null;
for(int i=0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<>();
map.put("Thumbnail", jObject.getString("thumbnail_path"));
titulo = jObject.getString("preview_title");
if(titulo.length() > 58){
titulo = titulo.substring(0, 58) + "...";
}
map.put("Titulo", titulo);
map.put("Titulo_completo", jObject.getString("full_title"));
map.put("Thumbnail_path", jObject.getString("thumbnail_path"));
map.put("Descripcion", jObject.getString("full_text"));
map.put("Resumen", "Resumen");
mAndroidMapList.add(map);
}
} catch (JSONException e) {
}
ListAdapter adapter = new SimpleAdapter(getActivity(), mAndroidMapList, R.layout.noticia_row,
new String[] { "Titulo", "Resumen" },
new int[] { R.id.titulo, R.id.resumen }){
@Override
public View getView(int position, View convertView, ViewGroup parent){
// Get the current item from ListView
View view = super.getView(position,convertView,parent);
Bitmap bmp = null;
ImageView imageView = (ImageView)view.findViewById(R.id.thumbnail);
DescargaImagenes descargaImagenes = new DescargaImagenes();
try {
bmp = descargaImagenes.execute("image-url" + mAndroidMapList.get(position).get("Thumbnail")).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bmp);
//CAMBIAR FUENTE
TextView myTextView = (TextView)view.findViewById(R.id.titulo);
Typeface typeFace=Typeface.createFromAsset(getActivity().getAssets(),"fonts/TitilliumWeb-Regular.ttf");
myTextView.setTypeface(typeFace);
myTextView = (TextView)view.findViewById(R.id.resumen);
typeFace=Typeface.createFromAsset(getActivity().getAssets(),"fonts/TitilliumWeb-Regular.ttf");
myTextView.setTypeface(typeFace);
if(position % 2 == 1)
{
// Set a background color for ListView regular row/item
view.setBackgroundColor(Color.parseColor("#7F1010"));
}
else
{
// Set the background color for alternate row/item
view.setBackgroundColor(Color.parseColor("#9C1F1F"));
}
return view;
}
};
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
HashMap registro = mAndroidMapList.get(position);
Intent i = new Intent(getActivity(), DetalleNoticia.class);
i.putExtra("datos", registro);
startActivity(i);
}
});
}
}
I didn't know how to set the image, so what I thought, was downloading and setting the image with my list that already has the image path to download on the getView()
method. I'm still new to Android so that was the first thing I could think of.
How do I improve performance?