Android网格视图单元格背景更改
我在让GridView在滚动时保持数据一致性方面遇到问题。基本上,我正在开发一个简单的宾果应用程序,我希望能够在屏幕上滚动,以便用户可以看到完整的5x5网格。我一直在用这个问题试图弄清楚到底发生了什么,如果我理解正确,我需要修改getView,使每个单元格不使用循环视图。 以下是我到目前为止的情况:Android网格视图单元格背景更改,android,gridview,textview,getview,Android,Gridview,Textview,Getview,我在让GridView在滚动时保持数据一致性方面遇到问题。基本上,我正在开发一个简单的宾果应用程序,我希望能够在屏幕上滚动,以便用户可以看到完整的5x5网格。我一直在用这个问题试图弄清楚到底发生了什么,如果我理解正确,我需要修改getView,使每个单元格不使用循环视图。 以下是我到目前为止的情况: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
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();
}
}
});
}
我只是在理解如何使文本视图工作时遇到了一个问题。在此方面的任何帮助都将不胜感激。您使用空上下文创建textview,因此会发生错误
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;
}
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;
}