2013-02-15 27 views
0

複選框在我的應用程序我有複選框動態添加。 在應用程序中,當我點擊其中一個複選框n然後單擊確定buton我正在執行一些操作。下面是code.its不工作點擊複選框。 請告訴我該怎麼辦?oncheckedchangelistener在android

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()); 
        } 


       }); 
     } 
} 
} 
+0

需要你的整個活動芽,這個片段犯規多言。也有更好的方法來設置onClickListener()的視圖通過具有活動imliment onClickListener(),並設置視圖view.onClickListener(this) – WIllJBD 2013-02-15 05:34:14

+0

我已經更新上面的code.and我也試過onclickListener.its也不工作 – 2013-02-15 05:38:31

回答

0

試試這個

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); 
+0

它不起作用。即使沒有進入 – 2013-02-15 05:52:41

相關問題