adapter.notifyDataSetChange()在从另一个活动返回后不更新列表视图

时间:2016-06-03 11:06:57

标签: android listview adapter notifydatasetchanged

我有两个活动:1。包含listview(Activity1),2。listview(Activity2)中每行的详细信息。

当用户点击Activity1中列表视图的任何行时,其各自的详细信息将显示在Activity2中,即Activity2已启动。当用户在Activity2中编辑一些细节时,所有更改都保存到服务器,当我返回到Activity1时,我从服务器获取新的(更新的)列表并想要更新Activity1的UI(列表视图)。但这不会发生。我正在执行以下操作来更新列表视图:

@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);
    }
}

但是,如果我再次设置适配器(就像我注释掉的三行),listview会根据需要更新,但是使用notifyDataSetChanged(),它不起作用。我已经关注了这个问题的所有问题,但似乎没有任何效果。我的适配器从ArrayAdapter扩展。请帮忙!

我的适配器实现:

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;
   }
}

4 个答案:

答案 0 :(得分:0)

尝试

pets.clear();
pets.addAll(petService.getPetList());
adapter.notifyDataSetChanged();

我猜适配器列表会丢失其与实际列表的链接。

答案 1 :(得分:0)

在简历中添加您的提取代码

#NOSONAR

答案 2 :(得分:0)

  1. 首先,使用startActivityForResult(intent,requesCode)而不是startActivity(intent)方法启动Activity2。
  2. 覆盖/实现Activity1的onActivityResult(int requestCode,int resultCode,Intent data)并执行这些代码。这将确保onBackPressed您的列表得到更新

    pets.clear(); pets.addAll(petService.getPetList()); adapter.notifyDataSetChanged();

  3. 所以你的代码看起来像

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //...some codes
    
        pets.clear();
        pets.addAll(petService.getPetList());
        adapter.notifyDataSetChanged();
    
    }
    

答案 3 :(得分:0)


我用这个适配器进行了测试,它在我这边工作。然而, 我没有附加图像,但它应该工作正常。

public class CustomArrayAdapter extends ArrayAdapter<Pet> {
    private final String TAG = "PetListArrayAdapter";

    private List<Pet> pets = null;

    public CustomArrayAdapter(Context context, List<Pet> petList) {
         super(context, 0, petList);
         pets = petList;
    }

    @Override
    public int getCount() {
        int size = 0;
        if (pets != null) {
            size = pets.size() + 1;
        }
        return size;
    }

    @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);
         ViewHolder holder;
         int type = getItemViewType(position);
         LayoutInflater inflater = LayoutInflater.from(getContext());
         if (convertView == null) {
             holder = new ViewHolder();
             if (type == 1) {
                 convertView = inflater.inflate(R.layout.list_item_layout, null);
                 holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
                 holder.tvDescription = (TextView) convertView.findViewById(R.id.tv_descriotion);
                 final Pet pet = pets.get(position - 1);
                 Log.d(TAG, "pet:" + pet);

                convertView.setTag(holder);
             } else if (type == 0) {
                  convertView = inflater.inflate(R.layout.list_add_layout, null);
                  holder.tvName = (TextView) convertView.findViewById(R.id.tv_add);
             }
        } else {
             holder = (ViewHolder) convertView.getTag();
        }
        if (holder != null) {
             if (type == 1) {
                 holder.tvName.setText(pets.get(position - 1).getName());
                 if (holder.tvDescription != null) {
                    holder.tvDescription.setText(pets.get(position - 1).getDescription());
                 }
             } else if (type == 0) {
                 holder.tvName.setText("Add new pet");
             }
         }

         return convertView;
     }
     static class ViewHolder {
         TextView tvName;
         TextView tvDescription;
     }
 }

我必须补充一点,你需要有一个合适的持有人来处理添加宠物布局和 宠物的布局。我在这里添加的只是你的起点。 我会创建包含一个textview或没有任何内容的基本持有者,并使用特定类型的特定持有者,并且只要它是类型1或0,就会转换为权利持有者。