jeudi 19 juillet 2018

Problems with Dialog when checkBox is checked?

I've a fragment where I need to select some Images from Gallery. So basically I want to display a dialog where I populate with thumbnail images and checkbox. Images and checkboxes are correctly displayed, but when I select one or more checkbox it returns a "JavaNullPointerException" on a null object. This code working fine if I use an activity, but with fragment I don't understand where is the mistake. Below my code:

My method to call the dialog.

private void scegliFotoDaMostrare(){
    final Dialog d = new Dialog(getActivity());
    d.setContentView(R.layout.activity_show_image);

    ImageList = getSD();

    // gridView1
     gView1 = (GridView)d.findViewById(R.id.gridView1);

    gView1.setAdapter(new ImageAdapter(getActivity(),ImageList));


    // Check All
    Button btnCheckAll = (Button) d.findViewById(R.id.btnCheckAll);
    btnCheckAll.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                checkbox.setChecked(true);
            }
        }
    });

    // Clear All
    Button btnClearAll = (Button) d.findViewById(R.id.btnClearAll);
    btnClearAll.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                checkbox.setChecked(false);
            }
        }
    });

    // Get Item Checked
    Button btnGetItem = (Button) d.findViewById(R.id.btnGetItem);
    btnGetItem.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                if(checkbox.isChecked())
                {
                    Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());

                    Toast.makeText(getActivity(),checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();

                }
            }
        }
    });
    d.show();

    }

The Adapter

public class ImageAdapter extends BaseAdapter{        
    private Context context;
    private List <String> lis;

    public ImageAdapter(Context c, List <String> li)
    {
        // TODO Auto-generated method stub
        context = c;
        lis = li;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return lis.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.showimage, null);
        }

        TextView textView = (TextView) convertView.findViewById(R.id.textView1);
        String strPath = lis.get(position).toString();

        // Get File Name
        String fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length() );
        textView.setText(fileName);

        // Image Resource
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        Bitmap bm = BitmapFactory.decodeFile(strPath);
        imageView.setImageBitmap(bm);

        // CheckBox
        CheckBox Chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        Chkbox.setTag(fileName);

        return convertView;

    }
}

Get the List !

    private List <String> getSD() {
     List <String> it = new ArrayList <String>();
     String pathImmagini =  Environment.getExternalStorageDirectory().toString() +
     File.separator + "DCIM"+ File.separator +  File.separator;
     File f = new File(pathImmagini);
     File files[] = f.listFiles();
     String titoloDaCercare = "giretto";      
     for (int i = 0; i <files.length; i++){   
              String fileName = (files[i].getName().substring(0,files[i].getName().indexOf(";"))
                .replace("IMG_",""));
         if (fileName.equals( titoloDaCercare) ){
              File file = files[i];
              Log.d("Count", file.getPath());
              it.add(file.getPath());
         }
     }
    return it;
}




Aucun commentaire:

Enregistrer un commentaire