I have two Activities: 1. contains the listview (Activity1), 2. details of each row in the listview (Activity2).
When the user clicks on any row of the listview in Activity1, its respective details get displayed in Activity2 i.e. Activity2 is started. When the user edits some details in Activity2, all changes are saved to the server and when I return to Activity1 I am fetching the new(updated) list from the server and want to update the UI (listview) of Activity1. But this does not happen. I am doing the following to update the listview:
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "onStart", Toast.LENGTH_LONG).show();
PetService petService = FactoryMaker.getFactory(AppConstants.PET_SERVICE).getPetServiceImpl(AppConstants.PARSE_IMPL, this);
//pets = petService.getPetList();
//adapter = new PetListArrayAdapter(getApplicationContext(),R.layout.pet_list_item, pets);
//petListView.setAdapter(adapter);
pets = petService.getPetList();
adapter.clear();
adapter.addAll(pets);
adapter.notifyDataSetChanged();
if(pets.size()>0) {
retrieve.setVisibility(View.INVISIBLE);
}else{
retrieve.setVisibility(View.VISIBLE);
}
}
However if I set the adapter again(like the three lines I commented out), the listview gets updated as required but with notifyDataSetChanged(), its not working. I have followed all the questions for this problem but nothing seems to work. My adapter extends from ArrayAdapter. Please help!
My adapter implementation:
public class PetListArrayAdapter extends ArrayAdapter<Pet> {
private final String TAG="PetListArrayAdapter";
private List<Pet> pets = null;
private Context applicationContext =null;
public PetListArrayAdapter(Context context, int resource, List<Pet> petList) {
super(context, resource, petList);
pets = petList;
applicationContext = context;
}
@Override
public int getCount(){
int size = 0;
if(pets!=null) {
size = pets.size();
}
return size+1;
}
@Override
public int getItemViewType(int position) {
return (position == 0) ? 0 : 1;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "getView for position" + position);
int type = getItemViewType(position);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null) {
if(type==1) {
convertView = inflater.inflate(R.layout.pet_list_item, null);
TextView t = (TextView)convertView.findViewById(R.id.petName);
final Pet pet = pets.get(position-1);
Log.d(TAG, "pet:" + pet);
t.setText(pet.getName());
ImageView petImage =(ImageView)convertView.findViewById(R.id.petImage);
String imageUrl = pet.getThumbnailUrl();
//if(petImage.getDrawable()==null) {
if (imageUrl != null) {
Log.d(TAG, "Loading Image for position" + position);
new ImageDownloader(petImage).execute(imageUrl);
}
/*}else{
Log.d(TAG, "Image present for position" + position);
}*/
}else if (type==0){
convertView = inflater.inflate(R.layout.add_pet, null);
}
}
return convertView;
}
}