清除网格视图并在android中重新生成它

时间:2013-04-08 12:42:59

标签: android gridview android-gridview

我在页面加载时有一个网格视图填充它。当我点击它时我有一个同步按钮它应该重新生成网格视图。如何做到这一点。

我正在使用

ImageAdapter adapter = new ImageAdapter(MainActivity.this); gridview.setAdapter(adapter);

详细代码

try {
            if(stringArrayPdfUrlForLocalDB.length>0)
            {
                for(int i=0 ; i < stringArrayPdfUrlForLocalDB.length ; i++)
                {
                    String fileName = getOnlyFileName(stringArrayPdfUrlForLocalDB[i]);
                    String imageName = getImageName(stringArrayPdfUrlForLocalDB[i]);
                    String BookId   = stringArrayBookId[i];

                    //Toast.makeText(getApplicationContext(), "File Name: "+fileName+"\nBookId: "+BookId, Toast.LENGTH_LONG).show();


                    db.open();        
                    long id = db.insertRecord(BookId, fileName + ".pdf", imageName);        
                    db.close();
                }
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Not getting any book form server.", Toast.LENGTH_SHORT).show();
            }

            //populate grid view

            ImageAdapter adapter = new ImageAdapter(MainActivity.this);
            gridview.setAdapter(adapter);

        } catch (Throwable e) {
            Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_SHORT).show();
        }

1 个答案:

答案 0 :(得分:1)

单击按钮后

首先加载您的数据

之后就行了。

adapter.notifyDataSetChanged();

<强>编辑:

ImageAdapter adapter = new ImageAdapter(MainActivity.this);

在执行该代码后,将此适配器创建为全局变量:

adapter = new ImageAdapter(MainActivity.this);
gridview.setAdapter(adapter); 

在代码中更改此代码:

protected void onPostExecute(Double result) {
            // Toast.makeText(getApplicationContext(), responseBody,
            // Toast.LENGTH_SHORT).show();

            if (responseBody != null) {
                processResponce(responseBody);
            } else {
                Toast.makeText(getApplicationContext(), "Empty Responce.",
                        Toast.LENGTH_LONG).show();
            }

            adapter.notifyDataSetChanged();

        }