我正在进行网格布局,并希望将其置于警告对话框中。但是当我尝试使用show()显示警告对话框时,警报对话框未显示GridView
。相反,它正在显示前面的对话框和背景中的GridView
,其中网格项目不可点击。这是编码:
public class GridViewActivity extends Activity {
Context context;
View layout;
static final String[] MOBILE_OS = new String[] { "timer", "about"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView)findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position == 0){
Toast.makeText(GridViewActivity.this, "first icon is pressed", Toast.LENGTH_SHORT).show();
}
else if(position == 1){
Toast.makeText(GridViewActivity.this, "second icon is pressed", Toast.LENGTH_SHORT).show();
}
}});
Dialog();
}
public void Dialog(){
AlertDialog alertDialog;
Context mContext = GridViewActivity.this;
AlertDialog.Builder builder = new AlertDialog.Builder(GridViewActivity.this);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main,null);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}
请告诉我如何解决这个问题。
答案 0 :(得分:0)
编辑为:
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position == 0){
Toast.makeText(GridViewActivity.this, "first icon is pressed", Toast.LENGTH_SHORT).show();
}
else if(position == 1){
Toast.makeText(GridViewActivity.this, "second icon is pressed", Toast.LENGTH_SHORT).show();
}
...............
Dialog(); // You Have called this method OUT-SIDE the click event
.......................
}});