It's taking too long to load few items, it takes several seconds, and to load many items, it never loads. I used the same XML reading algorithm, with a simple list and loaded very fast, however in my custom ListView it takes a long time, I tried to use ViewHolder, but it did not work. Check out my classes:
Adapter
public class AdapterSegmento extends BaseAdapter {
private ArrayList<Segmento> itens;
private Activity myContext;
private LayoutInflater mLayoutInflater;
public AdapterSegmento(Context context, ArrayList<Segmento> itens) {
//Itens que preencheram o listview
this.itens = itens;
//responsavel por pegar o Layout do item.
}
/**
* Retorna a quantidade de itens
*
* @return
*/
public int getCount() {
return itens.size();
}
/**
* Retorna o item de acordo com a posicao dele na tela.
*
* @param position
* @return
*/
public Segmento getItem(int position) {
return itens.get(position);
}
/**
* Sem implementação
*
* @param position
* @return
*/
public long getItemId(int position) {
return position;
}
static class ViewHolder{
TextView tvName;
TextView tvDescription;
ImageView imagem;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view; //trying to reuse a recycled view
ViewHolder holder = null;
if (vi == null) {
//The view is not a recycled one: we have to inflate
vi = mLayoutInflater.inflate(R.layout.empresa_item, parent, false);
holder = new ViewHolder();
holder.tvName = (TextView) vi.findViewById(R.id.nomeEmpresa);
holder.tvDescription = (TextView) vi.findViewById(R.id.subtitulo);
// holder.imagem = (ImageView) vi.findViewById(R.id.imagemSegmento);
vi.setTag(holder);
} else {
// View recycled !
// no need to inflate
// no need to findViews by id
holder = (ViewHolder) vi.getTag();
}
Segmento item = itens.get(position);
holder.tvName.setText(item.getNome());
holder.tvDescription.setText(item.getSubtitulo());
return vi;
}
}
My View
public class SegmentoView extends Util implements OnItemClickListener {
private ListView listView;
private AdapterSegmento adapterEmpresa;
private ArrayList<Segmento> itensEmpresa;
XmlReader reader;
String URL;
ProgressDialog dialog;
@SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// carrega o layout onde contem o ListView
setContentView(R.layout.empresa_lista);
ActionBar bar = getActionBar();
bar.hide();
//Carregar XML
reader = new XmlReader("http://www.itcuties.com/feed/");
itensEmpresa = new ArrayList<Segmento>();
try {
for (int i = 0; i < reader.getItems().size(); ++i) {
Segmento segmento = new Segmento();
segmento.setNome(reader.getItems().get(i).getNome());
segmento.setLink(reader.getItems().get(i).getLink());
itensEmpresa.add(segmento);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Cria o adapter
adapterEmpresa = new AdapterSegmento(this, itensEmpresa);
// Define o Adapter
listView.setAdapter(adapterEmpresa);
// Cor quando a lista é selecionada para ralagem.
listView.setCacheColorHint(Color.TRANSPARENT);
}
}
Can anyone help me?