从Parse中检索数据

时间:2014-08-11 14:19:26

标签: java android android-layout android-activity parse-platform

我设法在解析时拥有用户记录信息名称,标题,年龄,甚至性别选择以及性别偏好。现在我希望能够根据条件检索信息。例如,如果当前用户选择他正在寻找女性,那么它只会显示查询女性。

以下是用户输入提交解析信息的代码:

public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;
    protected EditText mName;
    protected EditText mAge;
    protected EditText mHeadline;
    protected ImageView mprofilePicture;
    RadioButton male, female;
    String gender;
    RadioButton lmale, lfemale;
    String lgender;

    protected Button mConfirm;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);

        RelativeLayout v = (RelativeLayout) findViewById(R.id.main); 

        v.requestFocus();

        Parse.initialize(this, "ID", "ID");

        mName = (EditText)findViewById(R.id.etxtname);
        mAge = (EditText)findViewById(R.id.etxtage);
        mHeadline = (EditText)findViewById(R.id.etxtheadline);
        mprofilePicture = (ImageView)findViewById(R.id.profilePicturePreview);
        male = (RadioButton)findViewById(R.id.rimale);
        female = (RadioButton)findViewById(R.id.rifemale);
        lmale = (RadioButton)findViewById(R.id.rlmale);
        lfemale = (RadioButton)findViewById(R.id.rlfemale);

        mConfirm = (Button)findViewById(R.id.btnConfirm);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = mName.getText().toString();
                String age = mAge.getText().toString();
                String headline = mHeadline.getText().toString();



                age = age.trim();
                name = name.trim();
                headline = headline.trim();

                if (age.isEmpty() || name.isEmpty() || headline.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                    builder.setMessage(R.string.signup_error_message)
                        .setTitle(R.string.signup_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                else {
                    // create the new user!
                    setProgressBarIndeterminateVisibility(true);

                    ParseUser currentUser = ParseUser.getCurrentUser();

                    /*   // Locate the image from the ImageView
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                           fron image view);
                    // Convert it to byte
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    // Compress image to lower quality scale 1 - 100
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] image = stream.toByteArray();

                    // Create the ParseFile
                    ParseFile file = new ParseFile("profilePicture.png", image);
                    // Upload the image into Parse Cloud
                    file.saveInBackground();

                    // Create a column named "ImageName" and set the string
                    currentUser.put("ImageName", "AndroidBegin Logo");

                    // Create a column named "ImageFile" and insert the image
                    currentUser.put("ProfilePicture", file); 

                    // Create the class and the columns
                    currentUser.saveInBackground();  */
                    if(male.isChecked())
                        gender = "Male";
                    else
                        gender = "Female";

                    if(lmale.isChecked())
                        lgender = "Male";
                    else
                        lgender = "Female";


                    currentUser.put("Name", name); 
                    currentUser.put("Age", age); 
                    currentUser.put("Headline", headline); 
                    currentUser.put("Gender", gender);
                    currentUser.put("Looking_Gender", lgender);

                    currentUser.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            setProgressBarIndeterminateVisibility(false);

                            if (e == null) {
                                // Success!
                                Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                            else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                                builder.setMessage(e.getMessage())
                                    .setTitle(R.string.signup_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
                }
            }
        });



        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
        final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                seekBarValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this

        Button mcancel = (Button)findViewById(R.id.btnBack);
        mcancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ProfileCreation.this.startActivity(new Intent(ProfileCreation.this, LoginActivity.class));
            }
        });




        SeekBar seekBarMinimum = (SeekBar) findViewById(R.id.seekBarMinimumAge);
        final TextView txtMinimum = (TextView) findViewById(R.id.tMinAge);

        seekBarMinimum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                txtMinimum.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this

        SeekBar seekBarMaximum = (SeekBar) findViewById(R.id.seekBarMaximumAge);
        final TextView txtMaximum = (TextView) findViewById(R.id.tMaxAge);

        seekBarMaximum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                txtMaximum.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this




        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });

    } 

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

以下是我头脑风暴可以用来检索信息的代码:

currentUserId = ParseUser.getCurrentUser().getObjectId();
            names = new ArrayList<String>();
           // String userActivitySelectionName = "";


            ParseQuery<ParseUser> query = ParseUser.getQuery();
           query.whereNotEqualTo("objectId", currentUserId);
               query.whereEqualTo("ActivityName","");
               query.whereNotEqualTo("Looking_Gender",currentUserID);

我现在遇到的问题是设定一个条件,如果一个人想要与男性匹配,那么只有那些想要与男性信息相匹配的人才会回归,反之亦然。 如果可以,请告诉我。

提前致谢。

1 个答案:

答案 0 :(得分:1)

以下行将确保返回的用户只是与当前用户正在查找的性别匹配的用户。也就是说,如果我正在寻找女性,下面的约束将确保只返回女性。

query.whereEqualTo("Gender", ParseUser.getCurrentUser().getString("Looking_Gender"));

以下行将确保返回的用户只是正在寻找符合我所属性别的其他用户的用户。也就是说,如果我是男性,下面的约束将确保只返回寻找男性的用户。

query.whereEqualTo("Looking_Gender", ParseUser.getCurrentUser().getString("Gender"));

如果我正确理解你的问题,一起使用上述两个约束应该可以实现你想要的。