2013-02-15 81 views
0

在我的应用程序我添加复选框dyanmically.i想要执行操作点击复选框。 我尝试了很多。下面是code.please help me.how我可以得到id的点击checkbox.in下面的代码它甚至没有进入onclick函数。onclickListener android

public class ImportFile extends Activity implements OnClickListener{ 

TableLayout tl; 
Intent i; 
ImageView im; 
int idcount; 
public OnClickListener checkBoxListener; 
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(getApplicationContext()); 
      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); 
      tr.setId(id); 

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

      } 

     } 

     } 


    @Override 
    public void onClick(View v) { 
     int j; 
     // TODO Auto-generated method stub 
     for(j=1;j<idcount;j++){ 
      if(ch[j].isChecked()) 
      { 
     System.out.println("PPPPPPPPPPPPPPPPPP"+j); 
    }  
     } 
    } 

}

+0

是工作或没有 – 2013-02-15 06:47:29

+0

它不工作。 – 2013-02-15 06:55:31

+0

你增加了ID吗? – SKT 2013-02-15 06:58:45

回答

2

你要的onclick监听器更改为onCheckChangedListener

public class ImportFile extends Activity implements OnCheckedChangeListener { 

TableLayout tl; 
Intent i; 
ImageView im; 
int idcount; 
public OnClickListener checkBoxListener; 
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(" "); 
      ch[id].setOnCheckedChangeListener(this); 

      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); 
      tr.setId(id); 

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

     } 

    } 

} 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
// TODO Auto-generated method stub 
    int kk = ch[1].getId(); 
    System.out.println("PPPPPPPPPPPPPPPPPP" + kk); 
} 

}

+0

嘿thanku这么much.its工作.... :) – 2013-02-15 07:09:30

1

变化

ch[id]=new CheckBox(this); 

ch[id] = new CheckBox(getApplicationContext()); 

这将解决您的问题

+0

它也没有working.i已编辑上面的代码。 – 2013-02-15 06:54:46

+0

你没有改变这个 – 2013-02-15 06:55:07

+0

我已经更新它now.not working.please帮助雅 – 2013-02-15 06:57:42