2011-07-21 44 views
0

在我的android應用程序/我試圖做那樣的http://i56.tinypic.com/2ivno8l.jpg。我發現如何使用圖像或textview在這裏使用gridview的示例:http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGalleryB.html#GridView 但我不知道如何處理圖像和textview。誰能幫我?GridView與來自網站和textView的圖像?

謝謝..

我想:

package com.MyGridView; 

import java.io.InputStream; 
import java.net.URI; 
import java.net.URL; 

import android.R.integer; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Color; 
import android.graphics.drawable.Drawable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.ListAdapter; 
import android.widget.TextView; 

public class ImageAdapter1 extends BaseAdapter { 

    private Context mContext; 

    private LayoutInflater inflater = null; 

    private Activity activity; 

    public ImageAdapter1(Context c) { 
     mContext = c; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return filesnames.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    public class ViewHolder { 
     public TextView text; 
     public ImageView image; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     ViewHolder holder; 
     if (convertView == null) { 
      vi = inflater.inflate(R.layout.item, null); 
      holder = new ViewHolder(); 
      holder.text = (TextView) vi.findViewById(R.id.text); 
      holder.image = (ImageView) vi.findViewById(R.id.image); 

      holder.image.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      holder.image.setPadding(8, 8, 8, 8); 

     } else 
      holder = (ViewHolder) vi.getTag(); 

     holder.text.setText(WORLDCUP2010[position]); 
     holder.image.setImageResource(filesnames[position]); 

     return vi; 
    } 


    private Integer[] filesnames = { R.drawable.icon, R.drawable.icon, 
      R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, 
      R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, 
      R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon, 
      R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon 

    }; 

    static final String[] WORLDCUP2010 = new String[] { "Algeria", "Argentina", 
      "Australia", "Brazil", "Cote d'Ivoire", "Cameroon", "Chile", 
      "Costa Rica", "Denmark", "England", "France", "Germany", "Ghana", 
      "Greece", "Honduras", "Italy", "Japan", "Netherlands", 
      "New Zealand", "Nigeria", "North Korea", "Paraguay", "Portugal", }; 

}

,我得到這個在logcat中:

07-21 17:30:43.152: ERROR/AndroidRuntime(396): Uncaught handler: thread main exiting due to uncaught exception 
07-21 17:30:43.172: ERROR/AndroidRuntime(396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyGridView/com.MyGridView.MyGridView}: java.lang.NullPointerException 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.os.Looper.loop(Looper.java:123) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at dalvik.system.NativeStart.main(Native Method) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396): Caused by: java.lang.NullPointerException 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at com.MyGridView.ImageAdapter1.<init>(ImageAdapter1.java:34) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at com.MyGridView.MyGridView.onCreate(MyGridView.java:23) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
07-21 17:30:43.172: ERROR/AndroidRuntime(396):  ... 11 more 
+0

你可以爲gridview創建自定義適配器 –

+0

我不知道如何做到這一點:| ...我試過,但它不工作:| – Gabrielle

回答

0

不僅僅是谷歌有關自定義適配器包括的TextView和示例imageviews。

我發現這個範例: http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/

編輯:

一個很好的例子是efficientAdapter在這裏找到:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html 在他們使用的佈局吹氣,而不是從創建ImageViews(或按鈕)例子上下文。

+0

這不是什麼...我也發現這個例子,但這裏是一個按鈕,當我把ImageView和TextView我得到了力量關閉..我需要一個inflater somthing,但我不知道用它 – Gabrielle

+0

那麼,最好的獲得解決方案的一個方法就是包含代碼和獲取錯誤時獲得的堆棧跟蹤(catlog)。無論如何,編輯回答給出更多的例子。 –