1

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

我在用

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();
        }
4

1 回答 1

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();

        }
于 2013-04-08T13:03:29.327 回答