簡體   English   中英

如何在不同的活動上使用GridView的同一適配器

[英]How Can I use Same Adapter of a GridView on Different activities

假設我有兩個活動,活動A和活動B。

活動A使用適配器Z顯示圖像列表。

當用戶單擊活動A中的任何圖像時,它們將被帶到活動B中以顯示完整圖像。 我正在使用Intent將圖像路徑和網格位置傳遞給Activity。

現在在活動B中,放置一個刪除按鈕,該按鈕應該從gridview適配器中刪除圖像路徑。

問題是:如何訪問活動B中的活動A適配器以在適配器中調用remove(position)方法。

因此,我可以在活動A的onResume中調用notifyDataSetChanged來更新gridview圖像。

活動A

MyGridView = (GridView) findViewById(R.id.gridview);
adapter = new MyAdapter(this);
MyGridView .setAdapter(adapter );    

Intent fullImageActivity = new Intent(getApplicationContext(), ActivityB.class);
    fullImageActivity.putExtra("position", position);
    fullImageActivity.putExtra("path", mediaPath);
    startActivity(fullImageActivity);

活動B

Intent i = getIntent();
// I'm getting position and path from setOnItemClickListener
position = i.getExtras().getInt("position");
path = i.getExtras().getString("path");

// I want to remove path from my adapter after clicking delete button in Activity B

適配器

public ArrayList<String> images;

    public void remove(int position){
            images.remove(position);
        }

使用startActivityForResult

Intent fullImageActivity = new Intent(getApplicationContext(), ActivityB.class);
    fullImageActivity.putExtra("position", position);
    fullImageActivity.putExtra("path", mediaPath);

   startActivityForResult(fullImageActivity, 2);

檢查是否已刪除Activity B中特定位置的內容。 在這里我重寫onBackPressed() (Ex。)方法

public void onBackPressed(){

   super.onBackPressed()

Intent intent=new Intent();  
                    intent.putExtra("isdeleted",true);  
intent.putExtra("pos",position);
                    setResult(2,intent);  
                    finish();
}

Activity A onActivityResult中處理它。

 @Override  
       protected void onActivityResult(int requestCode, int resultCode, Intent data)  
       {  
                 super.onActivityResult(requestCode, resultCode, data);  
                  // check if the request code is same as what is passed  here it is 2  
                   if(requestCode==2)  
                         {  
                            if(data.getBooleanExtra("isdeleted")){   
                           remove from position array and notify dataset change. // pos = data.getIntExtra("pos") 
                            }
                         }  
     } 

很抱歉TYPO。

這里的關鍵是您需要共享數據,而不是真正的適配器。

使用單例

您可以使用單例類來保存圖像數據,然后在兩個活動中都訪問它們。 這樣可以確保兩個活動之間始終保持同步。

單身人士班

public class ImageData{
   private ArrayList<ImageModel> mDataOfImages;
   private ImageData mHelperInstance;

   private ImageData(){
         //private constructor to ensure singleton
   }

   public static ImageData getInstance(){
      // return an ImageData instance according to your implementation
      // of singleton pattern
   }

   private void setData(ArrayList<ImageModel> newData){
       this.mDataOfImages = newData;
   }

   private void removeImage(int position){
       if(this.mDataOfImages !=null && this.mDataOfImages.size() > position){
         mDataOfImages.remove(position);
     }
   }

}

活動A

private void saveImageData(ArrayList<ImageModel> data){
     if(data !=null){
          if(mAdapter !=null){
              mAdapter.setData(data);
          }
     }
}

//Call notifydatasetchanged when activity is opened again
@Override
protected void onStart() {
    if(mAdapter !=null){
        mAdapter.notifyDataSetChanged();
    }
}

我的適配器

public void setData(ArrayList<ImageMode> newData){
     if(newData !=null){
        ImageDataSingleton.getInstance().setData(newData);
        notifyDataSetChanged();
     }
}

活動B

使用單例類在活動B中顯示圖像。由於使用的是模型陣列列表,因此您可以輕松實現向右/向左滑動並刪除多個圖像。

//Delete a image
private void deleteImage(){
     ImageDataSingleton.getInstance().removeImage(getCurrentPosition());
 // Rest of deletion handling like moving to right or left image
}

我認為您應該在活動A中構建remove方法,並確保它是靜態的:

public static void remove(int position){
        images.remove(position);
    }

現在您可以從活動B調用它,如下所示:

ActivityA.remove(position);

我認為這會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM