YXTS122
2017-03-22 21:58
采纳率: 100%
浏览 2.5k

getCount和getView方法怎么多次被调用

运行如下:
图片说明
点击按钮,情况是这样的
图片说明
图片说明
执行gridView.setAdapter(adapter);时会去调用getCount()方法,但后面getCount和getView方法怎么多次被调用,什么情况下调用的?不是说有9张图就只调用9次getView方法么?
然后任意点击9张图的其中一张,LogCat如下:
图片说明
咦?怎么getItem方法没被调用到?
还有data.getIntExtra("imageId",R.drawable.abc_ab_bottom_solid_dark_holo )
怎么不能这样写data.getIntExtra("imageId")?不是说键值存储,得到键,就得到了值么?
代码在下面的回答

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • fcwxin 2017-03-23 08:32
    已采纳

    你把gridview的宽高设为定值,或者设置为match_parent 试一下

    点赞 打赏 评论
  • YXTS122 2017-03-22 22:02

    然后任意点击9张图的其中一张,LogCat如下:
    图片说明
    MainActivity.java

     package com.example.gridviewdemo;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity implements OnClickListener {
        private Button button01;
        private ImageView view;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button01=(Button)findViewById(R.id.bt1);
            view=(ImageView)findViewById(R.id.iv1);
            button01.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v)
        {
            switch(v.getId())
            {
            case R.id.bt1:
                Intent intent=new Intent(MainActivity.this,ResultActivity.class);
                MainActivity.this.startActivityForResult(intent,1);
            }
        }
    
        protected void onActivityResult(int requestCode,int resultCode,Intent data)
        {
            if (requestCode==1&&resultCode==Activity.RESULT_OK)
            {
                int imageId=data.getIntExtra("imageId",R.drawable.abc_ab_bottom_solid_dark_holo);
                view.setImageResource(imageId);
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    
    

    ResultActivity.java

     package com.example.gridviewdemo;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class ResultActivity extends Activity
    {
        private GridView gridView;
        private int[] images={R.drawable.abc_ic_clear,R.drawable.abc_textfield_search_selected_holo_light,R.drawable.abc_tab_selected_pressed_holo,
                R.drawable.abc_menu_hardkey_panel_holo_light,R.drawable.abc_spinner_ab_holo_dark,R.drawable.abc_list_selector_holo_light,
                R.drawable.abc_menu_dropdown_panel_holo_dark,R.drawable.abc_textfield_search_selected_holo_light,R.drawable.abc_ab_solid_dark_holo
                };
        private List<Map<String,Object>> list;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_result);
            gridView=(GridView)findViewById(R.id.gv1);
        list=getResource();
        MyBaseAdapter adapter=new MyBaseAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener (new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id)
            {
                Intent intent=new Intent();
                intent.putExtra("imageId",images[position]);
                setResult(Activity.RESULT_OK,intent);
                ResultActivity.this.finish();
            }
        });
        }
    
        public class MyBaseAdapter extends BaseAdapter 
        {
            @Override
            public int getCount()
            {
                Log.e("ResultActivity","getCount");
                return list.size();
            }
            @Override
            public Object getItem(int position)
            {
                Log.e("ResultActivity","getItem");
                return list.get(position);
            }
            @Override
            public long getItemId(int position)
            {
                Log.e("ResultActivity","getItemId");
                return position;
            }
            @Override
            public View getView(int position,View convertView,ViewGroup parent)
            {
                ViewHolder viewHolder=null;
                Log.e("ResultActivity","getView");
                if (convertView==null)
                {
                    LayoutInflater layoutInflater=LayoutInflater.from(ResultActivity.this );
                    convertView=layoutInflater.inflate(R.layout.activity_item,null);
                    viewHolder=new ViewHolder();
                    viewHolder.imageView=(ImageView)convertView.findViewById(R.id.iv2);
                    viewHolder.textView=(TextView)convertView.findViewById(R.id.tv2);
                    convertView.setTag(viewHolder);
                }
                else
                {
                    viewHolder=(ViewHolder)convertView.getTag();
                }
                viewHolder.imageView.setImageResource((Integer)list.get(position).get("images"));
                viewHolder.textView.setText((CharSequence)list.get(position).get("text"));
                return convertView;
            }
        }
        static class ViewHolder
        {
            ImageView imageView;
            TextView textView;
        }
        public List<Map<String,Object>> getResource()
        {
            List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
            for (int i=0;i<images.length;i++)
            {
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("images",images[i]);
                map.put("text","图--"+i);
                list.add(map);
            }
            return list;
        }
    }
    
    
    
    
    
    
    
    
    
    点赞 打赏 评论
  • YXTS122 2017-03-22 22:07

    图片说明
    图片说明
    图片说明

    点赞 打赏 评论

相关推荐 更多相似问题