0

这是您的帮助代码

public class SelectImageActivity extends Activity {

private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private int selectedIndices[];
private String[] arrPath;
private ImageAdapter imageAdapter;
String servletPath = "http://192.168.46.1:8084//ServletModule/";
GridView imagegrid;
CheckBox cbPrev = null;
int sel = 0;
int cpSelectedStat=0;
Point clickPoints[];
String accno = "", pass = "",state ="";
int stat=0;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_image);

    Bundle extras = getIntent().getExtras();
    if(extras != null){
        accno = extras.getString("accno");
        pass = extras.getString("pass");
        state = extras.getString("state"); 
    }

    selectedIndices = new int[5];
    tv = (TextView)findViewById(R.id.tvSelectedItems);
    clickPoints = new Point[5];
    showCurrentStat();
    Vector<MyImage> imgVec = new Vector<MyImage>();
    imgVec = (Vector<MyImage>)callServlet("", "FetchAllImages");
    count = 20;
    this.thumbnails = new Bitmap[this.count];
    this.thumbnailsselection = new boolean[this.count];
    MyImage mi;
    for (int i = 0; i < this.count; i++) {
        mi = imgVec.elementAt(i);
        this.thumbnails[i] = Bitmap.createBitmap(mi.ww, mi.hh, Config.ARGB_8888);
        this.thumbnails[i].setPixels(mi.img, 0, mi.ww, 0, 0, mi.ww, mi.hh);
    }

    imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);
    //imagecursor.close();

    final Button selectBtn = (Button) findViewById(R.id.BtnSelectImages);
    selectBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

                for(int i=0;i<20;i++) thumbnailsselection[i] = false;
                imageAdapter = new ImageAdapter();
                imagegrid.setAdapter(imageAdapter);     
                selectedIndices[sel] = cbPrev.getId();
                sel++;
                showCurrentStat();
                if(sel == 5){
                    Intent ii = new Intent(v.getContext(),SelectClickPoint.class);
                    ii.putExtra("imageindex", selectedIndices[0]);
                    ii.putExtra("stat", 0);
                    startActivityForResult(ii, 1);
                    return;
                }

            }
    });
}

@Override
public void onBackPressed() {
    Intent iii = new Intent(SelectImageActivity.this,MainMenuActivity.class);
    iii.putExtra("uid", accno);
    iii.putExtra("pass", pass);
    iii.putExtra("stat", state);
    startActivity(iii);
    finish();
    return;
    }

void showCurrentStat(){
    tv.setText(sel + " of 5 Images Selected!");

}

Object callServlet(Object inp, String servletName){
    Object ret = null;
    try{
        String urlstr = servletPath + servletName;
        URL url = new URL(urlstr);
        URLConnection connection = url.openConnection();

        connection.setDoOutput(true);
        connection.setDoInput(true);

        // don't use a cached version of URL connection
        connection.setUseCaches(false);
        connection.setDefaultUseCaches(false);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        // specify the content type that binary data is sent
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
        // send and serialize the object
        out.writeObject(inp);
        out.close();

        // define a new ObjectInputStream on the input stream
        ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
        // receive and deserialize the object, note the cast
        ret = in.readObject();
        in.close();
    }catch(Exception e) {
        System.out.println("E:" + e);
    }
    return ret;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        clickPoints[cpSelectedStat] = new Point(data.getExtras().getInt("clickx"), data.getExtras().getInt("clicky"));
        cpSelectedStat++;
        if(cpSelectedStat == 5){
            SingleUser u = new SingleUser();
            u.accno = accno;
            u.pass = pass;
            u.gpMultiple = "";
            for(int i=0;i<5;i++){
                u.gpMultiple += selectedIndices[i] + ",";
                u.gpSequence[i] = clickPoints[i].x + "," + clickPoints[i].y;
            }
            u.gpMultiple = u.gpMultiple.substring(0,u.gpMultiple.length()-1);
            String ret = (String)callServlet(u, "SetGraphicalPassword");
            Toast.makeText(SelectImageActivity.this, "Graphical Password Set Successfully!", Toast.LENGTH_LONG).show();
            finish();
            state=Integer.toString(1);
            Intent i = new Intent(this,MainMenuActivity.class);
            i.putExtra("uid", u.accno);
            i.putExtra("pass", u.pass);
            i.putExtra("stat", state);
            startActivity(i);
            return;
        }
        Intent ii = new Intent(this,SelectClickPoint.class);
        ii.putExtra("imageindex", selectedIndices[cpSelectedStat]);
        ii.putExtra("stat", cpSelectedStat);
        startActivityForResult(ii, 1);

    }
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checkbox.setId(position);
        holder.imageview.setId(position);
        holder.checkbox.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(cbPrev != null){
                    cbPrev.setChecked(false);
                }
                cbPrev = (CheckBox) v;
                int id = cbPrev.getId();
                cbPrev.setChecked(true);
            }
        });
        holder.imageview.setImageBitmap(thumbnails[position]);
        holder.checkbox.setChecked(thumbnailsselection[position]);
        holder.id = position;
        return convertView;
    }
}
    class ViewHolder {
    ImageView imageview;
    CheckBox checkbox;
        int id;
}
}

想要在未选中复选框并单击继续按钮时使用此代码块执行

 AlertDialog ad = new AlertDialog.Builder(SelectImageActivity.this).create();
    ad.setCancelable(false);
    ad.setMessage("Please Select an Image to proceed");
    ad.setButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
    }
    });
    ad.show();
4

1 回答 1

0

将此添加到继续按钮下的 onClick 方法中。

if (checkbox.isChecked()){ //do something }else{ //call the method you want to execute when check box is not selected }

否则,一个简单的try catch语句应该可以解决问题。您可以将您的方法添加到,catch以便在捕获到异常时执行它。

于 2013-06-04T13:15:24.300 回答