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

本文介绍如何在 Android 应用中使用 GridView,并通过创建自定义适配器实现动态加载字符串列表。文章展示了如何减少视图复杂度、优化图片加载及使用 ViewHolder 模式提高 ListView 性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以参考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>()); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值