0
It’s taking too long to load a few items, it takes several seconds, and to load a lot of items, it never loads. I used the same XML reading algorithm, with a simple list and loaded very fast, but in my custom Listview it takes a long time, I tried using Viewholder, but it didn’t 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 someone help me?
It would not be better to use a string array to make Listview options?
– mutlei
I did it now, switched to Adapter and view to Segment[] items; and gave anyway.
– War Lock
Any ideas? Someone?
– War Lock
It puts a bonus on the question. It is the lawful means of attracting attention.
– mutlei
I didn’t understand, how so?
– War Lock
Put a bonus/reward/bonus on your question. It will cost you reputation, but it’s worth more :D
– mutlei
Let’s go continue this discussion in chat.
– War Lock
Have you considered using Asynctask?
– Luídne
I do not know how to use, if you can show me how to answer as I would be in my context I would very much appreciate
– War Lock