Saturday, 31 August 2013

android: getView() calls items already deleted

android: getView() calls items already deleted

I have some troubles with my ListView. I've made my own Adapter to display
my custom item, with his getView() method. Basically i inserted a certain
number of items at the lunching of the app and then I made my own Thread
to load data from Internet, in order to update the old contents. The
problem is the follow: when i try to clear all the old items (to replace
them with the new data of the thread) then i got a null point exception,
probably caused by getView that call and already deleted item position (or
at least is my interpretation). Follow the code:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.risultati);
myList = new ArrayList<RisultatiClass>();
populateList();
populateListView();
}
private void populateListView() {
// TODO Auto-generated method stub
adapter = new MyListAdapter();
myListView = (ListView) findViewById(R.id.listViewRisultati);
myListView.setAdapter(adapter);
}
private void populateList() {
Thread thread = new Thread()
{
@Override
public void run() {
String str = ConnectionOperations.getUrlHtml("http://gradio...");
String[] colonne = str.split("!");
String[] sq1 = colonne[0].split(",");
String[] sq2 = colonne[1].split(",");
String[] gf1 = colonne[2].split(",");
String[] gf2 = colonne[3].split(",");
String[] ts = colonne[4].split(",");
Log.w("myApp", ""+myList.size()+"");
myList.clear();
adapter.notifyDataSetChanged();
Log.w("myApp", ""+myList.size()+"");
for (int i=0; i<sq1.length; i++){
myList.add(i,new
RisultatiClass(sq1[i],sq2[i],gf1[i],gf2[i],ts[i]));
}
}
};
thread.start();
// TODO Auto-generated method stub
myList.add(new RisultatiClass("Roma AA","Lazio","2","3","Mag 21:00"));
myList.add(new RisultatiClass("Napoli AA","Milan","0","5","Mag 21:00"));
myList.add(new RisultatiClass("Napoli","Milan","0","5",""));
myList.add(new RisultatiClass("Napoli","Milan","0","5",""));
myList.add(new RisultatiClass("Napoli","Milan","0","5",""));
myList.add(new RisultatiClass("Napoli","Milan","0","5",""));
}
//definisco l'adapter
private class MyListAdapter extends ArrayAdapter<RisultatiClass>{
public MyListAdapter(){
super(Risultati.this,R.layout.risultati_row,myList);
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
//make sure we have a view to work with
View itemView = convertView;
if(itemView==null){
itemView = getLayoutInflater().inflate(R.layout.risultati_row,
parent,false);
}
//find the items
RisultatiClass currentItem = myList.get(position);
//fill the view
TextView sq1 = (TextView) itemView.findViewById(R.id.risultatiSq1);
sq1.setText(currentItem.getSq1());
TextView sq2 = (TextView) itemView.findViewById(R.id.risultatiSq2);
sq2.setText(currentItem.getSq2());
TextView ris1 = (TextView) itemView.findViewById(R.id.risultatiRis1);
ris1.setText(currentItem.getRis1());
TextView ris2 = (TextView) itemView.findViewById(R.id.risultatiRis2);
ris2.setText(currentItem.getRis2());
TextView timeStamp = (TextView)
itemView.findViewById(R.id.risultatiTimeStamp);
timeStamp.setText(currentItem.getTimeStamp());
return itemView;
}
Do you guys see any error? this is the log: 08-31 19:00:57.620:
E/AndroidRuntime(1416): java.lang.IndexOutOfBoundsException: Invalid index
3, size is 3 08-31 19:00:57.620: E/AndroidRuntime(1416): at
java.util.ArrayList.get(ArrayList.java:308)

No comments:

Post a Comment