0

我有一个注册表单。我正在检查性别;如果性别是男性,则应将男性图像(ieRdrawable.male)添加到仪表板。如果性别是女性,则应将女性图像添加到仪表板中。如何做那?这是我的活动

    public class KidRegistration extends Activity
   {
         Intent it2;
ImageView imageView;
private static KidDbHelper kdbhelper;
EditText Editkid,Editkidfname,Editkidlname,Editkidphone,Editkidemail;
RadioGroup rgroup;
RadioButton radio1,radiob2;
Button submit,reset,view;;
TextView addTextView;
int kidmalephotopath, kidfemalephotopath;
public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
          "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
          "\\@" +
          "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
          "(" +
          "\\." +
          "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
          ")+"
      );
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.kid_registration);
    View title=getWindow().findViewById(android.R.id.title);
    View titlebar=(View) title.getParent();
    titlebar.setBackgroundColor(Color.DKGRAY);

    Editkid=(EditText) findViewById(R.id.kid);
    Editkidfname=(EditText) findViewById(R.id.kidfname);
    Editkidlname=(EditText) findViewById(R.id.kidlname);
    Editkidphone=(EditText) findViewById(R.id.kidphone);
    Editkidemail=(EditText) findViewById(R.id.kidemail);
    rgroup=(RadioGroup) findViewById(R.id.rgroup);
    radio1=(RadioButton) findViewById(R.id.radio1);
    radiob2=(RadioButton) findViewById(R.id.radio2);
    submit=(Button) findViewById(R.id.kidsubmit);
    reset=(Button) findViewById(R.id.kidreset);

    kidmalephotopath=getResources().getIdentifier("kidmale", "drawable", getPackageName());
    kidfemalephotopath=getResources().getIdentifier("kidfemale", "drawable", getPackageName());

    it2=getIntent();

    submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) 
        {
            // TODO Auto-generated method stub
            //Validating each entry
               if((Editkid.getText().toString().contains("a-z,A-Z"))||(Editkid.getText().length()==0))
                {
                    Editkid.setError("Please enter the valid number only");

                }
                else if((Editkidfname.getText().toString().contains("0-9"))|| Editkidfname.getText().toString().length()==0)
                {
                    Editkidfname.setError("Please Enter Valid First Name");

                }
                else if((Editkidlname.getText().toString().contains("0-9"))|| Editkidlname.getText().toString().length()==0)
                {
                    Editkidlname.setError("Please Enter valid Last Name");

                }
                else if(!(radio1.isChecked()||radiob2.isChecked()))
                {
                    mt("Select gender");

                }
                else if((Editkidphone.getText().toString().contains("a-z,A-Z"))||(Editkidphone.getText().toString().length()<6)||(Editkidphone.getText().toString().equalsIgnoreCase("")))
                {
                    Editkidphone.setError("Please Enter Valid Phone No");

                }
                else if(!(checkEmail(Editkidemail.getText().toString())))
                {
                    Editkidemail.setError("Please Enter valid email Id");

                }
                else
                {
                    long flag = 0;
                    int id = 1;
                    KidDbHelper kdbhelper=new KidDbHelper(getApplicationContext());
                    SQLiteDatabase db=kdbhelper.getWritableDatabase();
                    Cursor cursor = db.query("kiddetails", new String[]{"count(*) k_id"}, null, null, null, null, null);
                    while(cursor.moveToNext())
                    {
                        int idFromDatabase = cursor.getInt(cursor.getColumnIndex("k_id"));
                        if(idFromDatabase != 0)
                        {
                            id = 1 + idFromDatabase;
                        }
                    }
                    ContentValues values = new ContentValues();
                    values.put("k_id", Integer.parseInt(Editkid.getText().toString()));
                    values.put("firstname", Editkidfname.getText().toString().trim());
                    values.put("lastname", Editkidlname.getText().toString().trim());
                    if(radio1.isChecked())
                    {
                        values.put("gender","M");  

                    }
                    else
                    {
                        values.put("gender", "F");

                    }
                    values.put("phone", Long.parseLong(Editkidphone.getText().toString()));
                    values.put("email", Editkidemail.getText().toString());
                    //values.put("kidphotopath", photopath);
                    flag = db.insert("kiddetails", null, values);
                    if(flag != -1)
                    {
                        Toast toast = Toast.makeText(getApplicationContext(), "You have successful inserted this record into database! Your Entered ID is  "+flag, Toast.LENGTH_LONG); 
                        toast.show();   
                        db.close();
                        Intent mydash=new Intent(getApplicationContext(),DashBoard.class);
                        startActivity(mydash);
                        return;
                    }
                    else
                    {
                        Toast toast = Toast.makeText(getApplicationContext(), "An error occured when insert this record into database! Please try different kid ID", Toast.LENGTH_LONG); 
                        toast.show();
                        db.close();
                        return;
                    }
                }               
        }//OnClick()
    });//Listner

}//onCreate()


void mt( String text ) 
{
    Toast.makeText( this, text, Toast.LENGTH_SHORT).show();
}

public void kidreset(View v) 
{
   Editkid.setText("");
   Editkidfname.setText("");
   Editkidlname.setText("");
   rgroup.clearCheck();
   Editkidphone.setText("");
   Editkidemail.setText("");       
}

private boolean checkEmail(String email) 
{
    return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}


 }//Class

这是我的 ImageAdapter 类

    public class ImageAdapter extends BaseAdapter 
    {
              private Context mContext;
     public ImageAdapter(Context c) 
      {
           mContext = c;

       }
public int getCount() 
{
    return mThumbIds.length;
}
public Object getItem(int position) 
{
    return null;
}
public long getItemId(int position) 
{
    return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) 
{
    ImageView imageView = new ImageView(mContext);
    if (convertView == null) 
    {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } 
    else 
    {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mThumbIds[position]);
    return imageView;    
}   
// references to our images
private Integer[] mThumbIds = {R.drawable.add};
 } 
4

0 回答 0