0

in my applications i have checkbox dynamically added. in application when i click on one of the check box n then click on ok buton i'm performing some operations.below is the code.its not working on click of check box. please tell me what should i do?

 public class ImportFile extends Activity{
  TableLayout tl;
 Intent i;
 ImageView im;
 int idcount;
 TextView nameoffile,sizeoffile,line;
 CheckBox[] ch=new CheckBox[100];


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylist); 
        tl=(TableLayout) findViewById(R.id.table);

        File file = new File(Environment.getExternalStorageDirectory()
            + File.separator
            + "gallery" //folder name
        );
        if (!file.exists()) {
            file.mkdirs();
        }

               /* i=new Intent();
                i.putExtra("file","string");
                setResult(RESULT_OK, i);
                finish(); */


        TableRow tr=new TableRow(this);

        int id=1;

        File f = new File("/sdcard/download");//or Environment.getExternalStorageDirectory().getPath()
        File[] files = f.listFiles();

        for(int i = 0; i < files.length; i++) {
            File file1 = files[i];
            //take the file name only
            long size = file1.length()/1024;

            String myfile = file1.getPath().substring(file1.getPath().lastIndexOf("/")+1,file1.getPath().length()).toLowerCase(); 
            if(myfile.endsWith(".jpeg")||myfile.endsWith(".png")|| myfile.endsWith(".gif") || myfile.endsWith(".jpg"))
            {

            ch[id]=new CheckBox(this);
            ch[id].setId(id);
            ch[id].setText("  ");

            System.out.println("id is........"+id);

            Bitmap b=ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile((file1.getPath())), 40,40);

            im=new ImageView(this);
            im.setImageBitmap(b);       

            nameoffile=new TextView(this);
            nameoffile.setText("  "+myfile);
            nameoffile.setWidth(200);


            sizeoffile=new TextView(this);
            sizeoffile.setText(size+"KB");
            sizeoffile.setWidth(100);

            tr=new TableRow(this);
            tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            if((id%2)==0)
            {
                tr.setBackgroundResource(R.color.Thistle);
            }
            else{

                tr.setBackgroundResource(R.color.Bisque);
            }
            tr.addView(ch[id++]); 
            idcount=id;
            tr.addView(im);
            tr.addView(nameoffile);
            tr.addView(sizeoffile);

            tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

            }

        }
    }

    int j;

    public void ok(View v)
    {

        for (j=1;j<idcount;j++)
        {

             ch[j].setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        System.out.println("Checked ID :: " + ch[j].getId());
                    }


                });
        } 
}
}
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
  • need your whole activity bud, this snippet doesnt say much. also there are better ways to set onClickListener() for views by having the Activity imliment onClickListener(), and setting the views view.onClickListener(this) – WIllJBD Feb 15 '13 at 05:34
  • i have updated above code.and i have also tried onclickListener.its also not working – yuva ツ Feb 15 '13 at 05:38

1 Answers1

0

Try this

OnClickListener checkBoxListener;
checkBoxListener =new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(checkbox1.isChecked())
{

Toast.makeText(getBaseContext(), "something", Toast.LENGTH_SHORT).show();
}
}

};
checkbox1.setOnClickListener(checkBoxListener);
Linga
  • 10,379
  • 10
  • 52
  • 104