【问题标题】:How to refresh Image Data in GridView如何在 GridView 中刷新图像数据
【发布时间】:2017-06-18 16:29:41
【问题描述】:

我的应用有一个 Fragment,它显示 GridView 中文件夹中的图像。

在 gridview Item OnClick 上,我通过 Intent 将文件路径、文件名和位置传递给另一个 Activity。

此活动以全尺寸显示图像... 我使用 Delete 选项从 SdCard 中删除图像,然后调用 finish() 以退出活动... 图像已删除,但 Gridview 未刷新... 它仍然显示缩略图!

调用finish();时如何刷新Gridview?

显示 Gridview 的片段 -

public class Downloaded extends Fragment{



    // Declare variables
    private String[] FilePathStrings;
    private String[] FileNameStrings;
    private File[] listFile;
    GridView grid;
    GridViewAdapter adapter;
    File file;
   TextView empty;

@Override
public void onActivityCreated( Bundle
savedInstanceState) {
super.onActivityCreated(savedInstanceState);
    getActivity().setTitle("Saved");
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        //Inflating the Layout 
        View rootView = inflater.inflate(R.layout.gridview_main, container, false); 


        // Check for SD Card
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG)
                    .show();
        } else {
            // Locate the image folder in your SD Card
            file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "Sample");
            // Create a new folder if no folder named SDImageTutorial exist
            file.mkdirs();
        }

        if (file.isDirectory()) {
            listFile = file.listFiles();
            // Create a String array for FilePathStrings
            FilePathStrings = new String[listFile.length];
            // Create a String array for FileNameStrings
            FileNameStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++) {
                // Get the path of the image file
                FilePathStrings[i] = listFile[i].getAbsolutePath();
                // Get the name image file
                FileNameStrings[i] = listFile[i].getName();
            }
        }


        // Locate the GridView in gridview_main.xml
        grid = (GridView) rootView.findViewById(R.id.gridview);

       empty = (TextView) rootView.findViewById(R.id.empty);

       empty.setText("You haven't saved any Pic yet...!");

       grid.setEmptyView(empty);

        // Pass String arrays to LazyAdapter Class
        adapter = new GridViewAdapter(getActivity(), FilePathStrings, FileNameStrings);

        // Set the LazyAdapter to the GridView
        grid.setAdapter(adapter);



        // Capture gridview item click
        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Intent i = new Intent(getActivity(), ViewImage.class);
                // Pass String arrays FilePathStrings
                i.putExtra("filepath", FilePathStrings);
                // Pass String arrays FileNameStrings
                i.putExtra("filename", FileNameStrings);
                // Pass click position
                i.putExtra("position", position);
                startActivity(i);
            }

        });
        return rootView;
    }
}

& 这是删除图像的 ViewImage 类 -

public class ViewImage extends AppCompatActivity {
    // Declare Variable
    TextView text;
    public String[] filepath;
    public int position;
    public String[] filename;

   private TouchImageView imageview;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from view_image.xml
        setContentView(R.layout.img_act);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        // Retrieve data from MainActivity on GridView item click
        Intent i = getIntent();

        // Get the position
        position = i.getExtras().getInt("position");

        // Get String arrays FilePathStrings
          filepath = i.getStringArrayExtra("filepath");

        // Get String arrays FileNameStrings
        filename = i.getStringArrayExtra("filename");

        // Locate the TextView in view_image.xml
        //text = (TextView) //findViewById(R.id.imagetext);

        // Load the text into the TextView followed by the position
        getSupportActionBar().setTitle(filename[position]);

        // Locate the ImageView in view_image.xml
        imageview = (TouchImageView) findViewById(R.id.full_image_view);

    // Decode the filepath with BitmapFactory followed by the position
        Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);

        // Set the decoded bitmap into ImageView
        imageview.setImageBitmap(bmp);

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.image_view, menu);
        return true;
    }


public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId()){

          case R.id.vi;

              File f = new File(filepath[position]);

            if (f.exists())
            {
                f.delete ();
            }
            this.finish();

           break;

            case android.R.id.home:
                onBackPressed();
                break;
                }

        return true;
        }
    }

【问题讨论】:

    标签: android android-fragments android-gridview


    【解决方案1】:

    删除并完成活动后,您必须在 onActivityResult 的片段中调用 adapter.notifyDataSetChanged()

    在你的片段中

    grid.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
    
                    Intent i = new Intent(getActivity(), ViewImage.class);
                    // Pass String arrays FilePathStrings
                    i.putExtra("filepath", FilePathStrings);
                    // Pass String arrays FileNameStrings
                    i.putExtra("filename", FileNameStrings);
                    // Pass click position
                    i.putExtra("position", position);
                    startActivityForResult (i, 101);
                }
    
            });
    

    在 ViewImage 活动中 onDelete 点击写如下

    case R.id.vi;
    
                  File f = new File(filepath[position]);
    
                if (f.exists())
                {
                    f.delete ();
                }
                Intent i = new Intent();
            i.putInt("POS", your_deleted_item_pos");
            setResult(RESULT_OK, i);
            finish();
    
               break;
    

    在片段中

        @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == RESULT_OK) {
                    switch (requestCode) {
                        case 101:
                            if(data.containsKey("POS"){
                             int pos = data.getInt("POS");
                             /*here you have to  Remove item from FilePathStrings and FileNameStrings for particular position.*/
                            adapter.notifyDataSetChanged();  // it only done when your deleted image is remove from list which you bind in gridview.
                            break;
    
                    }
                }
            }
    

    【讨论】:

    • 在我调用finish() 的情况下,我应该在没有请求代码、结果代码和意图数据的情况下使用onActivityResult,对吗?那是什么RESULT_OK?
    • 现在有什么问题?
    • 它不刷新gridview
    • 在完成活动后,您在片段中收到回调吗?
    • 我传递了结果意图并取回了数据,即片段中的位置,但它仍然显示缩略图.. 我们可以在 onResume() 中执行 notifyDataSetChanged 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多