【问题标题】:images url are not displaying in grid图像 url 未在网格中显示
【发布时间】:2013-04-19 05:41:51
【问题描述】:

我已经为图像 url 设置了一个 gridview..我无法在网格中看到图像..只显示背景...然后单击网格在下一个屏幕中播放。

我做错了什么?如何实现?

提前非常感谢

我的代码

    public class act extends Activity {
static  String uri1="https://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg";
static String uri2="https://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg";
static String uri3="https://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg";
public static String[] urls={uri1,uri2,uri3};
public Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GridView grd=(GridView)findViewById(R.id.gridView1);
    grd.setAdapter(new ImageAdapter(this));
    grd.setOnItemClickListener(new OnItemClickListener()
    {
    public void onItemClick(AdapterView<?> parent,View v,int pos,long id)
    {
        Toast.makeText(getBaseContext(),"pic"+(pos+1)+"select ",Toast.LENGTH_SHORT).show();
    }
    });
}
public class ImageAdapter extends BaseAdapter
{
    private Context context;
    private int itemBackground;
    ImageAdapter(Context c)
    {
    context=c;
    TypedArray a=obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
    a.recycle();
    }
    public int getCount()
    {
        return urls.length;
    }
    public Object getItem(int pos)
    {
        return pos;
    }
    public long getItemId(int pos)
    {
        return pos;
    }
    public View getView(int pos,View cv,ViewGroup vg)
    {
Bitmap bitmap=  DownloadImage( urls[pos] );
ImageView imageview=new ImageView(context);
imageview.setImageBitmap(bitmap);
return cv;    
    }
private Bitmap DownloadImage(String URL)
    {        
        final String URL1=URL;       
        new Thread()
        {
            public void run()
            {               
                InputStream in = null;  
                Message msg = Message.obtain();
                msg.what = 1;
                try {
                    in = OpenHttpConnection(URL1);
                    Bitmap bitmap = BitmapFactory.decodeStream(in);     
                    Bundle b = new Bundle();
                    b.putParcelable("bitmap", bitmap);
                    msg.setData(b);
                    in.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }.start();
        return bitmap;
    }
    private InputStream OpenHttpConnection(String urlString)
            throws IOException
            {
                InputStream in = null;
                int response = -1;
                URL url = new URL(urlString);
                URLConnection conn = url.openConnection();
                if (!(conn instanceof HttpURLConnection))                    
                    throw new IOException("Not an HTTP connection");
                try{
                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                    httpConn.setAllowUserInteraction(false);
                    httpConn.setInstanceFollowRedirects(true);
                    httpConn.setRequestMethod("GET");
                    httpConn.connect();
                    response = httpConn.getResponseCode();                
                    if (response == HttpURLConnection.HTTP_OK) 
                    {
                        in = httpConn.getInputStream();                                
                    }                    
                }
                catch (Exception ex)
                {
                    throw new IOException("Error connecting");            
                }
                return in;    
    }
}
}

日志猫

     E/AndroidRuntime(832): java.lang.NullPointerException
       E/AndroidRuntime(832):   at android.widget.GridView.onMeasure(GridView.java:937)
        E/AndroidRuntime(832):  at android.view.View.measure(View.java:8313)
       E/AndroidRuntime(832):   at 
       android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       E/AndroidRuntime(832):   at 
       android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
        E/AndroidRuntime(832):  at 
        android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
         E/AndroidRuntime(832):     at 
        android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
         E/AndroidRuntime(832):     at android.view.View.measure(View.java:8313)
         E/AndroidRuntime(832):     at 
         android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
          E/AndroidRuntime(832):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
         E/AndroidRuntime(832):     at android.view.View.measure(View.java:8313)
         E/AndroidRuntime(832):     at 
          android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
          E/AndroidRuntime(832):    at 
          android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
          E/AndroidRuntime(832):    at android.view.View.measure(View.java:8313)
          E/AndroidRuntime(832):    at 
          android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
          E/AndroidRuntime(832):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
          E/AndroidRuntime(832):    at android.view.View.measure(View.java:8313)
          E/AndroidRuntime(832):    at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
          E/AndroidRuntime(832):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
          E/AndroidRuntime(832):    at android.os.Handler.dispatchMessage(Handler.java:99)
           E/AndroidRuntime(832):   at android.os.Looper.loop(Looper.java:123)
           E/AndroidRuntime(832):   at android.app.ActivityThread.main(ActivityThread.java:3683)
           E/AndroidRuntime(832):   at java.lang.reflect.Method.invokeNative(Native Method)
           E/AndroidRuntime(832):   at java.lang.reflect.Method.invoke(Method.java:507)
           E/AndroidRuntime(832):   at 
           com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            E/AndroidRuntime(832):  at 
           com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            E/AndroidRuntime(832):  at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 我的输出是这样的......见截图
  • 在网格视图中显示之后,您必须从服务器下载该图像。 stackoverflow.com/a/3068012/1168654
  • 我的代码不能正常工作..我在做什么错误..

标签: android image gridview


【解决方案1】:

尝试用这个替换你的getView(),然后告诉我结果:

public class ImageAdapter extends BaseAdapter
{
    private Context context;
    private int itemBackground;
    public ImageLoader imageLoader; 

    ImageAdapter(Context c)
    {
    context=c;
    TypedArray a=obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
    a.recycle();
    imageLoader=new ImageLoader(context);
    }
    public int getCount()
    {
        return urls.length;
    }
    public Object getItem(int pos)
    {
        return pos;
    }
    public long getItemId(int pos)
    {
        return pos;
    }
      public View getView(int position, View convertView, ViewGroup parent)
    {
       ImageView imageView;
             if (convertView == null) {
               imageView = new ImageView(context);
               imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
               imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
               imageView.setPadding(2, 2, 2, 2);
    } else {
             imageView = (ImageView) convertView;
    }

       imageLoader.DisplayImage(urls[position], imageView)
       return imageView;
    }

}

【讨论】:

    【解决方案2】:

    这是我尝试过的代码..图像正在显示..您可以根据需要自定义网格视图。您只需将以下代码复制并粘贴到您的活动中并尝试..

    public class MainActivity extends Activity {
    
        static  String uri1="http://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg";
        static String uri2="http://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg";
        static String uri3="http://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg";
        public static String[] urls={uri1,uri2,uri3};
    //  public Bitmap bitmap;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            GridView grd=(GridView)findViewById(R.id.gridview1);
            grd.setAdapter(new ImageAdapter(this));
    
    
        }
        public class ImageAdapter extends BaseAdapter
        {
            private Context context;
            private int itemBackground;
            ImageAdapter(Context c)
            {
            context=c;    
            }
            public int getCount()
            {
                return urls.length;
            }
            public Object getItem(int pos)
            {
                return pos;
            }
            public long getItemId(int pos)
            {
                return pos;
            }
    
        private Bitmap DownloadImage(String URL)
            {        
                 String URL1=URL; 
                 Bitmap bitmap = null;
    //          new Thread()
    //          {
    //              public void run()
    //              {               
                        InputStream in = null;  
                        Message msg = Message.obtain();
                        msg.what = 1;
                        try {
                            in = OpenHttpConnection(URL1);
                            bitmap = BitmapFactory.decodeStream(in);     
                            Bundle b = new Bundle();
                            b.putParcelable("bitmap", bitmap);
                            msg.setData(b);
                            in.close();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
    //              }
    //          }.start();
                return bitmap;
            }
            private InputStream OpenHttpConnection(String urlString)
                    throws IOException
                    {
    //          System.out.println("Insdie conn");
                        InputStream in = null;
                        int response = -1;
                        URL url = new URL(urlString);
                        URLConnection conn = url.openConnection();
                        if (!(conn instanceof HttpURLConnection))                    
                            throw new IOException("Not an HTTP connection");
                        try{
    //                      System.out.println("Inside try");
                            HttpURLConnection httpConn = (HttpURLConnection) conn;
                            httpConn.setAllowUserInteraction(false);
                            httpConn.setInstanceFollowRedirects(true);
    //                      httpConn.setRequestMethod("GET");
                            httpConn.connect();
                            response = httpConn.getResponseCode();  
    //                      System.out.println("res="+response);
    //                      System.out.println("cccccc="+HttpURLConnection.HTTP_OK);
                            if (response == HttpURLConnection.HTTP_OK) 
                            {
    //                          System.out.println("Inside if");
                                in = httpConn.getInputStream();                                
                            }                    
                        }
                        catch (Exception ex)
                        {
                            throw new IOException("Error connecting");            
                        }
                        return in;    
            }
            @Override
            public View getView(int position, View cv, ViewGroup parent) 
            {
                ImageView imageview = null;
    //          System.out.println("vvvv="+urls[position]);
                Bitmap bitmap=  DownloadImage( urls[position] );        
                // TODO Auto-generated method stub
                if(cv == null)
                {
    //              cv=LayoutInflater.from(parent.getContext()).inflate(R.layout.gridviewitem, null);
                    imageview =  new ImageView(context);
                }
                else 
                {
                    imageview = (ImageView) cv;
                }
    
                imageview.setImageBitmap(bitmap);
                return imageview;    
            }
        }
    

    【讨论】:

    • 你必须添加 作为应用程序标签的直接子级..
    • 好的,我接受你的帮助对我很有帮助......但我没有得到第三张图片......为什么?
    • 像这样转换 xml .. 跨度>
    【解决方案3】:

    这里是ExampleGridViewImageLoading

    已编辑:

    注意代码中的错误。但是您要显示来自服务器的图像(在线它不在您的应用程序资源或 sdcard、图库等中),这就是它没有显示在您的 GridView 中的原因。因此,您必须从服务器下载该图像。之后,您必须在 GridView 中显示它们。我在上面提供了如何下载图像并在 GridView 中显示它们的链接。

    【讨论】:

    • 在数组中的顺序是什么..我应该在列表视图中得到相同的顺序..怎么做
    • 同listview看不懂
    • 你下载了那个例子??
    猜你喜欢
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多