0

在我的应用程序中,我正在动态添加复选框。我想在单击复选框时执行操作。我尝试了很多。下面是代码。请帮助我。我怎样才能获得点击复选框的 ID。在下面的代码中它甚至没有进入 onclick 函数。

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

}

4

2 回答 2

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

}

于 2013-02-15T07:04:17.590 回答
1

改变

ch[id]=new CheckBox(this);

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

它会解决你的问题

于 2013-02-15T06:42:13.880 回答