2017-01-31 149 views
0

我有gridview與3個單元格,每個單元格將具有ImageView和ImageButton以從圖庫或相機添加圖像。當替換單個圖像時,所有其他ImageView在Gridview中被替換

但是,當我試圖設置選定的圖像點擊單元格的網格視圖它取代所有的單元格,而不是一個單一的單元格。

這裏是gridview的

public class Adapter_For_GridView extends RecyclerView.Adapter<Adapter_For_GridView.ViewHolder>{ 

    public static final String TAG="===GridView Pics==="; 
    ViewClickCallback viewClickCallback; 
    Context context; 
    List<UserGrid> userGridList; 
    UserGrid userGridWrapper; 
    String ImageData; 

    public Adapter_For_GridView(ViewClickCallback viewClickCallback,Context context) { 
     this.viewClickCallback=viewClickCallback; 
     this.context=context; 
    } 

    @Override 
    public Adapter_For_GridView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.user_profile_photo_single_item,parent,false); 
     ViewHolder viewHolder=new ViewHolder(view); 
     Log.d(TAG,"On Create View Holder "); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(Adapter_For_GridView.ViewHolder holder, int position) { 
     userGridWrapper=userGridList.get(position); 

     Log.d(TAG,"On Bind View Holder- Value of isUpload "+userGridWrapper.isUpload()); 
     Log.d(TAG,"On Bind View Holder Image Data "+ImageData); 
     Log.d(TAG,"On Bind View Holder Position "+position+" "+"User Grid Wrapper "+userGridWrapper); 
     Log.d(TAG,"On Bind View Holder User Grid Wrapper Get Image Path "+userGridWrapper.getImagePath()); 

     if (ImageData!=null){ 
      Log.d(TAG,"Image Data is Not Null "+ImageData); 
      userGridWrapper.setUpload(true); 
      if (userGridWrapper.isUpload()){ 
       holder.imageButton.setImageResource(R.drawable.ic_clear_black_24dp); 
       Picasso.with(context).load(ImageData) 
         .into(holder.imageView); 
      } 
     } 
     else{ 
      holder.imageButton.setImageResource(R.drawable.ic_add_black_24dp); 
     } 


    } 

    @Override 
    public int getItemCount() { 
     Log.d(TAG,"Value at GetItemCount of GridList "+userGridList.size()); 
     if (userGridList!=null){ 
      return userGridList.size(); 
     } 
     return 2; 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     ImageView imageView; 
     ImageButton imageButton; 


     public ViewHolder(View itemView) { 
      super(itemView); 
      Log.d(TAG,"On View Holder"); 
      itemView.setOnClickListener(this); 
      imageView= (ImageView) itemView.findViewById(R.id.User_Pics); 
      imageButton= (ImageButton) itemView.findViewById(R.id.imageButton); 
      imageButton.setOnClickListener(this); 

     } 

     @Override 
     public void onClick(View v) { 
      Log.d(TAG,"Image Button Clicked "+getAdapterPosition()+" Get Layout Position "+getLayoutPosition()); 
      if (viewClickCallback!=null){ 
       Log.d(TAG,"ViewClickcalleback is not null "); 
       viewClickCallback.viewClicked(getAdapterPosition()+1); 
      } 

     } 
    } 

    public void setUserGridList(List<UserGrid> userList){ 
     this.userGridList=userList; 
    } 

    public void setImagePath(String path,int position){ //GETTING IMAGE HERE FROM ONACTIVITYRESULT TO THIS ADAPTER 
     this.ImageData=path; 
     Log.d(TAG,"Position of Grid "+position); //POSITION OF CLICKED ITEM 
     Log.d(TAG,"Set Image Path "+path); //IMAGE PATH OF SELECTED IMAGE 
     notifyDataSetChanged(); 
     } 
    } 

從logcat中onactivityresult

01-31 18:13:47.917 9146-9146/com.example.com.pro_working1 D/===User Profile===: User Profile On Activity Result Calling 25 -1 
01-31 18:13:47.927 9146-9146/com.example.com.pro_working1 D/===User Profile===: On Activity Result Calling--Request Code 25 
01-31 18:13:47.927 9146-9146/com.example.com.pro_working1 D/===User Profile===: Intent Data Intent { dat=content://media/external/images/media/19276 typ=image/jpeg flg=0x1 } 
01-31 18:13:47.927 9146-9146/com.example.com.pro_working1 D/===User Profile===: On Activity Result Position 0 
01-31 18:13:48.687 9146-9146/com.example.com.pro_working1 D/===User Profile===: Image Name content://media/external/images/media/19276 
01-31 18:13:48.687 9146-9146/com.example.com.pro_working1 D/===User Profile===: Image /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg 
01-31 18:13:48.687 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Position of Grid 0 
01-31 18:13:48.687 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Set Image Path /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg 
01-31 18:13:48.777 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3 
01-31 18:13:48.777 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3 
01-31 18:13:48.777 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3 
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder- Value of isUpload false 
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Image Data /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg 
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Position 0 User Grid Wrapper [email protected] 
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder User Grid Wrapper Get Image Path null 
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Image Data is Not Null /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder- Value of isUpload false 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Image Data /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Position 1 User Grid Wrapper [email protected] 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder User Grid Wrapper Get Image Path null 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Image Data is Not Null /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder- Value of isUpload false 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Image Data /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Position 2 User Grid Wrapper [email protected] 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder User Grid Wrapper Get Image Path null 
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Image Data is Not Null /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg 

回答

0

這是因爲使用的是單varibale 的ImageData用於存儲所選圖像的路徑發生的適配器。因此,當在gridview中重新創建視圖時,此varibale值將相同,這將使下面的代碼片段中的第一個條件成立。

if (ImageData!=null){ 
    Log.d(TAG,"Image Data is Not Null "+ImageData); 
    userGridWrapper.setUpload(true); 
    if (userGridWrapper.isUpload()){ 
     holder.imageButton.setImageResource(R.drawable.ic_clear_black_24dp); 
     Picasso.with(context).load(ImageData) 
       .into(holder.imageView); 
    } 
} 
else{ 
    holder.imageButton.setImageResource(R.drawable.ic_add_black_24dp); 
} 

現在,由於它已經輸入了第一個條件,所以在檢查其值之前將上傳值設置爲true。這將再次結束true。因此它使用相同的數據填充所有視圖。

解決方案

更改以下方法:

public void setImagePath(String path,int position){ //GETTING IMAGE HERE FROM ONACTIVITYRESULT TO THIS ADAPTER 
this.ImageData=path; 
userGridList.get(position).setUpload(true); 
Log.d(TAG,"Position of Grid "+position); //POSITION OF CLICKED ITEM 
Log.d(TAG,"Set Image Path "+path); //IMAGE PATH OF SELECTED IMAGE 
notifyDataSetChanged(); 
} 

也改變了條件檢查用於填充圖像:

if (userGridWrapper.isUpload()){ 
    holder.imageButton.setImageResource(R.drawable.ic_clear_black_24dp); 
    Picasso.with(context).load(ImageData) 
      .into(holder.imageView); 

} 
else{ 
holder.imageButton.setImageResource(R.drawable.ic_add_black_24dp); 
} 
+0

那麼我該怎麼做。我也有包裝類與'字符串imageData和布爾isUpload'的setter和getter方法。我能做些什麼,使'的ImageData String'只提供給單擊的單元格 – Ritu

+0

您可以創建**的ImageData ** varibale在包裝類,相同isUpload,然後通過使用位置從列表中得到它的設置varibale到包裝類對象在** setImagePath **方法或使用** isUpload ** boolean falg來區分 – Sanjeet

0

的問題是,你只有一條路徑作爲一個領域。您分配新值this.ImageData=path;。調用notifyDataSetChanged();後,您的適配器將刷新其數據,並會爲每個可見項目調用onBindViewHolder()。然後它將爲每個呼叫加載新路徑ImageData

你需要的是路徑列表。你也應該檢查是否已經加載了一個圖像。如果它是相同的圖像,則不必再次加載它。也許你可以爲此實現一個小緩存。