0

我是 Java、Android 等的新手。我有一个我无法弄清楚的问题。

我有GridView一个ImageAdapter基于UniversalImageLoader. 当我单击按钮启动方法时,

public void buttonsearch(View view)
{
imagesearch();      
}

GridView正常显示图像。但是当我直接调用它时。

imagesearch("somestring");

它根本什么都没有显示。通过我的“调试”消息,我可以看到它ImageAdapter正在工作。当我单击按钮时,GridView开始按内容填充。

private void imagesearch(String... params) throws ClientProtocolException,
        JSONException, IOException, InterruptedException,
        ExecutionException {

    EditText wordimage = (EditText) findViewById(R.id.wordimage);
    String previos = null;
      String wordtotr = null;
        String strwordtos = null;

        if ( params.length != 0 )
          {
          previos = params[0];
          }
        if ( previos != null)
          {
            strwordtos = previos;

            wordimage.setText(strwordtos);
          }
            strwordtos = wordimage.getText().toString().replaceAll(" ", "%20");

    parsejsonimages getUrls = new parsejsonimages();

    getUrls.execute(strwordtos);
    urlsp = getUrls.get();
    List<String> urls = new ArrayList<String>();

    urls = new ArrayList<String>(urlsp.values());
    directurls = new ArrayList<String>(urlsp.keySet());
    int j = 0;

    while (urls.size() > j) {
        urls.set(j, "http://someaddress:" + urls.get(j));
        j++;
    }

    GridView gridview = (GridView) findViewById(R.id.gridViewImages);

    Integer gwight = gridview.getWidth();
    Integer ghight = gridview.getHeight();
    ImageAdapter images = new ImageAdapter(this, urls, ghight, gwight);
    gridview.setAdapter(images);
    gridview.setSelected(true);


    gridview.setOnItemClickListener(new OnItemClickListener() {



        public void setalphatoimagebypos(Integer pos, Integer Alpha)
        {

            GridView gridview = (GridView) findViewById(R.id.gridViewImages);
            ImageView imgrecoil = (ImageView) (gridview
                    .getChildAt(pos));
            imgrecoil.setAlpha(Alpha);

        }

        public void onItemClick(AdapterView parent, View v, int position,
                long id) {

            if (strimgtag > -1) {
                setalphatoimagebypos(strimgtag, 255);           
                strimgtag = position;
                setalphatoimagebypos(strimgtag, 128);


        }

    });

编辑:好的问题已解决,稍后解释

4

1 回答 1

0

好的,所以我试图在GridView实际绘制之前获得尺寸。所以OnCreate()当我调用这个方法时,它只会在GridView绘制后启动我的代码。

public void waiter()
    {
        final GridView gridview1 = (GridView) findViewById(R.id.gridViewImages);

        gridview1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() 
          {
                @Override
                public void onGlobalLayout() 
                {
                    gridview1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    gwight = gridview1.getWidth(); 
                    ghight = gridview1.getHeight();
                        try {
                    imagesearch(wordfromprev);
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
          });
    }
于 2013-04-19T08:54:31.970 回答