【问题标题】:Android Grid View cell background changesAndroid Grid View 单元格背景更改
【发布时间】:2015-03-09 01:10:21
【问题描述】:

让 GridView 在滚动时保持数据一致时遇到问题。基本上,我正在开发一个简单的宾果游戏应用程序,我希望能够在屏幕上滚动,以便用户可以看到完整的 5x5 网格。我一直在使用这个问题来试图弄清楚发生了什么(Android: Replacing images in GridView array after OnItemClick)如果我理解正确,我需要修改 getView() 以不为每个单元格使用回收视图。 这是我目前所拥有的:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    final GridView gridview = (GridView) findViewById(R.id.gridview);
    final Bingo bingo = new Bingo();
    final String[] stuff = new String[25];
    for (int i=0;i<25;i++){
        stuff[i]=bingo.getValue(i);
    }
    stuff[12]="Free Space";

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


    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

           bingo.open(position);
           if (bingo.isOpen(position))
           v.setBackgroundColor(Color.rgb(12, 15, 204));
           else
           v.setBackgroundColor(Color.rgb(128, 128, 128));


            if (bingo.bingo()==true){
                Toast.makeText(getApplicationContext(),
                        //((TextView) v).getText(),
                        "BINGO",Toast.LENGTH_SHORT).show();
            }
        }
    });
}

这种代码很有效。背景发生了变化,它与我的宾果游戏类界面很好,但是当滚动时,随机单元格会改变颜色,有时单元格值会消失,留下空白。我在适配器声明之后尝试了以下代码,但它在设备上加载后立即崩溃。

   {
            public View getView(int position, View convertView, ViewGroup                                    parent){
            Context mContext = null;
            TextView textView= new TextView(mContext);
            if (convertView == null) {  


           textView.setText(stuff[12]);
            } else {
                textView = (TextView) convertView;
            }

            if (bingo.isOpen(position))
                textView.setBackgroundColor(Color.rgb(12, 15, 204));
            else
                textView.setBackgroundColor(Color.rgb(128, 128, 128));

            return textView;
        }
    };

我只是在理解如何使其适用于文本视图时遇到问题。对此的任何帮助将不胜感激。

【问题讨论】:

    标签: android gridview textview getview


    【解决方案1】:

    您使用空上下文创建文本视图,因此发生错误

    public View getView(int position, View convertView, ViewGroup parent){ 
            // here you declare the context is null
            // Context mContext = null;         
            TextView textView;
            if (convertView == null) {  
                // error occurs because the context is null
                // TextView textView= new TextView(mContext); 
                // so try to use
                textView = new TextView(parent.getContext()); 
                textView.setText(stuff[12]);
            } else {
                textView = (TextView) convertView;
            }
    
            if (bingo.isOpen(position))
                textView.setBackgroundColor(Color.rgb(12, 15, 204));
            else
                textView.setBackgroundColor(Color.rgb(128, 128, 128));
    
            return textView;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 2011-09-24
      • 2013-04-12
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多