2011-10-04 69 views
0

@@ Branislav-i做了和你說過的一樣的東西,它對我很有用,但是沒有任何圖像超過9n文字,它也讓我看到了同樣的第一或螞蟻隨機圖像和文字在第九圖像的位置和文字帶有圖標和文字的gridview圖像

public class ImageAdapter extends BaseAdapter { 
private Context mContext; 

public ImageAdapter(Context c) { 
    mContext = c; 
} 

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

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

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

// create a new ImageView for each item referenced by the Adapter 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v; 
    if (convertView == null) { // if it's not recycled, initialize some attributes 
     LayoutInflater li = getLayoutInflater(); 
     v = li.inflate(R.layout.mainmenu, null); 
     TextView tv = (TextView)v.findViewById(R.id.icon_text); 
     tv.setText(mTextsIds[position]); 
     ImageView iv = (ImageView)v.findViewById(R.id.icon_image); 
     iv.setImageResource(mThumbIds[position]); 
    } else { 
     v = (View) convertView; 
    } 
    return v; 
} 



    private Integer[] mThumbIds = { 
     R.drawable.hotel1, R.drawable.rest,R.drawable.dubaicityinfoicon_new,R.drawable.history1,R.drawable.geography,R.drawable.infoicon,R.drawable.infoicon,R.drawable.infoicon,R.drawable.parkmain, 

}; 

// references to our texts 
private String[] mTextsIds = { 
     "Hotels","Restaurants","City Info","Dubai History","Geography Of Dubai","Useful Information","Embassies in Duabai","Museum's","park" 
}; 

在此它回事,直到完美博物館但公園的工作不被顯示陣列mThumbIds內的任何隨機圖像n相關聯的文本。 plz plz催促需要幫助.. ñ呀圖像是可繪製的。

+0

http://developer.android.com/resources/tutorials/views/hello-gridview.html –

+0

@@ Dr.nik-我沒有在教程中做過這樣的事情say.stil面臨問題 – sups

回答

2

您可以custome適配器類爲

public class MyAdapter extends ArrayAdapter<BindData> { 
    private LayoutInflater inflater; 
    private int layoutId; 

    public MyAdapter(Context context, int layoutId, BindData[] objects) { 
     super(context, 0, objects); 
     this.inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     this.layoutId = layoutId; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 

     if (convertView == null) { 
      convertView = inflater.inflate(layoutId, parent, false); 
      holder = new ViewHolder(); 
      holder.textView = (TextView) convertView.findViewById(R.id.textview); 
      holder.imageView = (ImageView) convertView.findViewById(R.id.imageview); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     //BindData data = getItem(position); 
     holder.textView.setText(titles[position]); 
     holder.imageView.setImageBitmap(imgs[position]); 
     return convertView; 
    } 
} 

,並調用這樣

issueGridView = (GridView) findViewById(R.id.gvissue); 
     adapter =new MyAdapter(Issue.this, R.layout.issue_item, mDatas); 
     issueGridView.setAdapter(adapter); 

哪裏mDatas是

public class BindData { 
    Bitmap b; 
    String title; 
    BindData(Bitmap bitmap, String s) { 
     this.b = bitmap; 
     this.title = s; 
    } 
} 
private BindData[] mDatas; 
    static class ViewHolder { 
     TextView textView; 
     ImageView imageView; 
    } 
1

使用地圖或HashMap來配對你的價值,

HashMap<Integer, String> store = new HashMap<Integer, String>(); 
store.put(R.drawable.hotel1, "Hotels"); 
+0

@@ xjaphx-你可以告訴我如何使用這個? bcz我新來android – sups