I’m having a problem with my Android app. It’s probably a simple Java problem, but I don’t know Java well enough to know what to google. Below is a simplified version of what I have that illustrates my problem. I want to be able to access gridview from my dialog. Clearly what I have here isn’t right. I tried moving the declaration outside of the onCreate method, but then my application crashes right at the start (there’s no error message). I also tried adding it as an argument to ShowDialog, but I guess since I’m overriding that function that didn’t work. As you can probably tell, I don’t know what I’m doing. Thoughts?
public class HelloGridView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
((ImageAdapter) gridview.getAdapter()).initializemThumbIds();
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
showDialog(0);
}
});
}
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
gridview.setAdapter(new ImageAdapter(this)); // gridview cannot be resolved
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
HelloGridView.this.finish();
}
});
AlertDialog diag = builder.create();
return diag;
}
}
I think you just don’t have the correct scope, so below I made gridView a member variable. But I think what you really want to do is make your ImageAdapter a member variable and update the ImageAdapter only in your Dialog “yes” click.