2017-03-14 16 views
0

私はリストビュー、行、2つのテキストと1つの削除ボタンを持っています。しかし時には削除された別のアイテムはクリックされません。私はリストに正しいIDをチェックしました。私はRetrofitを使用して、私はちょうど私が削除ボタンをクリックし、ı使用notifyDataSetChanged();リストは(setFavoriteMerchant())スクロールが間違った位置になったときのリストビュー

public class FavoriteMerchantActivity extends AppCompatActivity implements IButtonCliclListener { 
 

 
    FavoriteMerchantAdapter favoriteMerchantAdapter; 
 

 
    @InjectView(R.id.ListFavoriteMerchant) 
 
    ListView ListFavoriteMerchant; 
 

 
    AlertDialogHelper alertDialogHelper; 
 

 
    List<ItemsFavoriteMerchant> list; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_favorite_merchant); 
 
     ButterKnife.inject(this); 
 
     alertDialogHelper = new AlertDialogHelper(this); 
 

 
     getFavoriteMerchant(); 
 
    } 
 

 
    @Override 
 
    public void onClick(View v) { 
 
     int itemIndex = (int) v.getTag(); 
 
     setFavoriteMerchant(itemIndex); 
 
     alertDialogHelper.showAlertSuccess("Removed from your favorites!"); 
 

 
    } 
 

 
    void getFavoriteMerchant() { 
 

 
     Call<FavoriteMerchant> call = ToolApi.getApi().getFavoriteMerchant(BaseService.TOKEN); 
 
     call.enqueue(new Callback<FavoriteMerchant>() { 
 
      @Override 
 
      public void onResponse(Response<FavoriteMerchant> response, Retrofit retrofit) { 
 

 
       if (response.body() != null) { 
 
        FavoriteMerchant favoriteMerchant = response.body(); 
 

 
        Integer errorCode = favoriteMerchant.getStatus().getErrorCode(); 
 

 
        if (errorCode == 0) { 
 

 
         list = favoriteMerchant.getItems(); 
 

 
         favoriteMerchantAdapter = new FavoriteMerchantAdapter(FavoriteMerchantActivity.this, alertDialogHelper, favoriteMerchant, list, getApplicationContext()); 
 
         ListFavoriteMerchant.setAdapter(favoriteMerchantAdapter); 
 
        } 
 

 
       } else { 
 
        startActivity(getIntent()); 
 
        alertDialogHelper.showAlertError("Connection error..."); 
 
       } 
 

 
      } 
 

 
      @Override 
 
      public void onFailure(Throwable t) { 
 
       startActivity(getIntent()); 
 
       alertDialogHelper.showAlertError("Connection error..."); 
 
      } 
 
     }); 
 
    } 
 

 
    void setFavoriteMerchant(final int index) { 
 

 
     Call<SetFavoriteMerchant> call = ToolApi.getApi().setFavoriteMerchant(BaseService.TOKEN, index, true); 
 
     call.enqueue(new Callback<SetFavoriteMerchant>() { 
 
      @Override 
 
      public void onResponse(Response<SetFavoriteMerchant> response, Retrofit retrofit) { 
 

 
       for (ItemsFavoriteMerchant item : list) { 
 
        if (item.getID() == index) { 
 
         list.remove(item); 
 
         favoriteMerchantAdapter.notifyDataSetChanged(); 
 

 
        } 
 
       } 
 
      } 
 

 
      @Override 
 
      public void onFailure(Throwable t) { 
 
       alertDialogHelper.showAlertError("connection error..."); 
 
      } 
 
     }); 
 

 
    }

public class FavoriteMerchantAdapter extends BaseAdapter implements View.OnClickListener { 
 

 
    List<ItemsFavoriteMerchant> itemsFavoriteMerchant; 
 
    FavoriteMerchant favoriteMerchant; 
 
    Context context; 
 
    AlertDialogHelper alertDialogHelper; 
 
    private IButtonCliclListener iButtonCliclListener; 
 

 
    public FavoriteMerchantAdapter(IButtonCliclListener iButtonCliclListener, AlertDialogHelper alertDialogHelper, FavoriteMerchant favoriteMerchant, List<ItemsFavoriteMerchant> itemsFavoriteMerchant, Context context) { 
 
     this.favoriteMerchant = favoriteMerchant; 
 
     this.context = context; 
 
     this.itemsFavoriteMerchant = itemsFavoriteMerchant; 
 
     this.alertDialogHelper = alertDialogHelper; 
 
     this.iButtonCliclListener = iButtonCliclListener; 
 
    } 
 

 
    @Override 
 
    public int getCount() { 
 
     return itemsFavoriteMerchant.size(); 
 
    } 
 

 
    @Override 
 
    public Object getItem(int position) { 
 
     return itemsFavoriteMerchant.get(position); 
 
    } 
 

 
    @Override 
 
    public long getItemId(int position) { 
 
     return 0; 
 
    } 
 

 
    @Override 
 
    public View getView(int position, View convertView, ViewGroup parent) { 
 
     FavoriteMerchantAdapter.ViewHolder viewHolder; 
 
     String CDnPath = favoriteMerchant.getCDNPath(); 
 
     ItemsFavoriteMerchant item = itemsFavoriteMerchant.get(position); 
 
     if (convertView == null) { 
 
      LayoutInflater layoutInflater = LayoutInflater.from(context); 
 
      viewHolder = new FavoriteMerchantAdapter.ViewHolder(); 
 
      convertView = layoutInflater.inflate(R.layout.item_favorite_merchant, null); 
 

 
      viewHolder.FavoriteMerchantPlaceDesc = (TextViewGothamBook) convertView.findViewById(R.id.FavoriteMerchantPlaceDesc); 
 
      viewHolder.FavoriteMerchantLocationDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantLocationDesc); 
 
      viewHolder.FavoriteMerchantCashDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantCashDesc); 
 
      viewHolder.FavoriteMerchantDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantDesc); 
 
      viewHolder.FavoriteMerchantLogo = (ImageView) convertView.findViewById(R.id.FavoriteMerchantLogo); 
 
      viewHolder.FavoriteMerchantDeleteButton = (ImageButton) convertView.findViewById(R.id.im_btn_deletemerchant); 
 
      viewHolder.FavoriteMerchantDeleteButton.setTag(item.getID()); 
 
      viewHolder.FavoriteMerchantDeleteButton.setOnClickListener(this); 
 
      convertView.setTag(item.getID()); 
 
      //convertView.setTag(viewHolder); 
 
     } else { 
 

 
      viewHolder = (FavoriteMerchantAdapter.ViewHolder) convertView.getTag(); 
 

 
     } 
 
    /* String CDnPath = favoriteMerchant.getCDNPath(); 
 
     ItemsFavoriteMerchant item = itemsFavoriteMerchants.get(position);*/ 
 

 
     String ImageUrl = item.getCoverPageImageURL(); 
 
     if (ImageUrl != null && !ImageUrl.isEmpty()) 
 
      Picasso.with(context) 
 
        .load(CDnPath + ImageUrl) 
 
        .transform(new CircleTransform()) 
 
        .into(viewHolder.FavoriteMerchantLogo); 
 

 

 
     String MerchantPlaceDesc = item.getName(); 
 
     if (MerchantPlaceDesc != null && !MerchantPlaceDesc.isEmpty()) 
 
      viewHolder.FavoriteMerchantPlaceDesc.setText(MerchantPlaceDesc); 
 

 
     String MerchantocationDesc = (String) item.getDistrict(); 
 
     if (MerchantocationDesc != null && !MerchantocationDesc.isEmpty()) 
 
      viewHolder.FavoriteMerchantLocationDesc.setText(MerchantocationDesc); 
 

 
     String MerchantCashDesc = ""; 
 
     if (MerchantCashDesc != null && !MerchantCashDesc.isEmpty()) 
 
      viewHolder.FavoriteMerchantCashDesc.setText(MerchantCashDesc); 
 

 
     return convertView; 
 
    } 
 

 
    @Override 
 
    public void onClick(View v) { 
 
     this.iButtonCliclListener.onClick(v); 
 
    } 
 
    public static class ViewHolder { 
 

 
     ImageView FavoriteMerchantLogo; 
 
     TextViewGothamBook FavoriteMerchantPlaceDesc; 
 
     TextViewGothamMedium FavoriteMerchantLocationDesc; 
 
     TextViewGothamMedium FavoriteMerchantCashDesc; 
 
     TextViewGothamMedium FavoriteMerchantDesc; 
 
     ImageButton FavoriteMerchantDeleteButton; 
 

 
    } 
 

 
}

答えて

1
if (convertView == null) { 
     LayoutInflater layoutInflater = LayoutInflater.from(context); 
     viewHolder = new FavoriteMerchantAdapter.ViewHolder(); 
     convertView = layoutInflater.inflate(R.layout.item_favorite_merchant, null); 

     viewHolder.FavoriteMerchantPlaceDesc = (TextViewGothamBook) convertView.findViewById(R.id.FavoriteMerchantPlaceDesc); 
     viewHolder.FavoriteMerchantLocationDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantLocationDesc); 
     viewHolder.FavoriteMerchantCashDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantCashDesc); 
     viewHolder.FavoriteMerchantDesc = (TextViewGothamMedium) convertView.findViewById(R.id.FavoriteMerchantDesc); 
     viewHolder.FavoriteMerchantLogo = (ImageView) convertView.findViewById(R.id.FavoriteMerchantLogo); 
     viewHolder.FavoriteMerchantDeleteButton = (ImageButton) convertView.findViewById(R.id.im_btn_deletemerchant); 
     viewHolder.FavoriteMerchantDeleteButton.setTag(item.getID()); 
     viewHolder.FavoriteMerchantDeleteButton.setOnClickListener(this); 
     convertView.setTag(item.getID()); 
     //convertView.setTag(viewHolder); 
    } 

your setting tags only when convert view is null.

convertView.setTag(item.getID()); // this call should be made to all getview calls. 

Solution : (check comments)

+0

ありがとうございます...私はこれを行いましたが、削除ボタンをクリックすると、アプリが停止しました。エラー:java.util.ConcurrentModificationException。私に何ができる? – Jenni

+0

この行:else { viewHolder =(FavoriteMerchantAdapter.ViewHolder)convertView.getTag(); } – Jenni

+0

はい、私はこの行の 'if(item.getID()== index){' FavoriteMerchantActivityクラス – Jenni

関連する問題