`
wang_peng1
  • 浏览: 3904661 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

扩展GridView显示文字以及BaseAdapter的集成

阅读更多

可以参考http://developer.android.com/guide/tutorials/views/hello-gridview.html
然后只需要修改:
public class MyAdapter extends BaseAdapter {
     private Context context;
     private String[] texts = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "eee", "hhh", "iii"};
     public MyAdapter(Context context) {   
      this.context = context;
     }
     public int getCount() {    return 9;}
     public Object getItem(int position) {    return null;}
     public long getItemId(int position) {    return 0;}
     public View getView(int position, View convertView, ViewGroup parent) {
         TextView tv;   
         if (convertView == null) {
                 tv = new TextView(context);       
                 tv.setLayoutParams(new GridView.LayoutParams(85, 85));
                     }
           else {
                  tv = (TextView) convertView;  
                   }
             tv.setText(texts[position]);   
             return tv;
       }

view集成的方法
public View getView(int position, View contentView, ViewGroup arg2)
    {        ViewHolder holder;       
           if (contentView == null) {
                       holder = new ViewHolder();           
                       contentView = inflater.inflate(R.layout.my_magic_list,null);          
                        holder.label = (TextView) contentView.findViewById(R.id.label);           
                        contentView.setTag(holder);        }
            else {            holder = (ViewHolder) contentView.getTag();        }       
            holder.label.setText(getLabel());       
            return contentView;    }

•Use convertView
•If you have images, don't scale your images on the fly. Use Bitmap.createScaledBitmap to create a scaled bitmap and put that into your views
•Use a ViewHolder so you don't have to call a bunch of findViewByIds() every time
•Decrease the complexity of the views in your listview. The fewer subviews, the better. RelativeLayout is much better at this than, say, LinearLayout. And make sure to use if you're implementing custom views.

4.LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
final View dialogView = li.inflate(R.layout.edit_event, null);   
ArrayList<String> routes = new ArrayList<String>(); 
ArrayAdapter<String> aa = new ArrayAdapter<String>(GOFdroid.this, android.R.layout.simple_spinner_item, routes);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
     Spinner destSpinner = (Spinner) dialogView.findViewById(R.id.edit_event_destination);
        String dest = events.get(pos).getDestination();
           int routesPos = routes.indexOf(dest);
              Log.d(TAG, "Dest: " + dest + ", pos: " + routesPos); 
              
                   destSpinner.setAdapter(aa);
destSpinner.setSelection(routesPos);
最后一句是在spiner没有显示之前显示一个特定信息

 

5.

  Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null); 
    startManagingCursor
(c); 
 
   
SpinnerAdapter adapter = new SimpleCursorAdapter(this, 
   
// Use a template that displays a text view 
            android
.R.layout.simple_gallery_item, 
           
// Give the cursor to the list adatper 
            c
, 
           
// Map the NAME column in the people database to... 
           
new String[] {People.NAME}, 
           
// The "text1" view defined in the XML template 
           
new int[] { android.R.id.text1 }); 
5.只想显示一个

display a static list of String objects

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new String[] {People.NAME}); 
add more String objects to the list later

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new ArrayList<String>()); 

分享到:
评论
1 楼 weixiang106 2014-02-22  
 

相关推荐

Global site tag (gtag.js) - Google Analytics