如何在android中禁用gridview中的项目

时间:2012-12-04 07:25:06

标签: android android-gridview

我的gridview中有imageview。我想禁用在android的gridview中点击的当前项目/图像。我无法做到这一点。如何做到这一点? 这是一个代码 -

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int no,long lg) {
            // TODO Auto-generated method stub

            final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            id=no;

            final EditText input = new EditText(MainActivity.this);
            alert.setView(input);

            alert.setPositiveButton("Check", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

            String value = input.getText().toString();

              AlertDialog.Builder alert1 = new AlertDialog.Builder(MainActivity.this);

              if(value.equalsIgnoreCase(country[id])){

                              // here i want to disable item
            alert1.setPositiveButton("Correct", new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int whichButton) {

                      }
                    });

              }
              else
              {

              }
            }
            });

            alert.show();
        }
    });

2 个答案:

答案 0 :(得分:8)

您需要一个自定义适配器。

覆盖方法

ListAdapter.isEnabled(int position)

并覆盖ListAdapter.areAllitemsEnabled()以返回false。

完全禁用在GridView的UI中单击以及选择图形。

答案 1 :(得分:0)

试试这个,

   @Override
        public void onItemClick(AdapterView<?> arg0, View v, int no,long lg) {
            // TODO Auto-generated method stub

            // check for item clicked  say you have to disable image at position 4


      if (no==4){
// do nothing
}
else{
 final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            id=no;

            final EditText input = new EditText(MainActivity.this);
            alert.setView(input);

            alert.setPositiveButton("Check", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

            String value = input.getText().toString();

              AlertDialog.Builder alert1 = new AlertDialog.Builder(MainActivity.this);

              if(value.equalsIgnoreCase(country[id])){

                              // here i want to disable item
            alert1.setPositiveButton("Correct", new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int whichButton) {

                      }
                    });

              }
              else
              {

              }
            }
            });

            alert.show();
        }
}





    });