简体   繁体   English

从解析中检索数据

[英]Retrieving data from Parse

I manage to have the user record information name, headline, age, and even gender selection as well as gender preference in parse. 我设法让用户记录信息名称,标题,年龄,甚至性别选择以及性别偏爱。 Now I want to be able to retrieve the information, with conditions. 现在,我希望能够有条件地检索信息。 For instance, if the current user selected that he is looking for a female, than it would only display queryequal female. 例如,如果当前用户选择了他正在寻找女性,则它将仅显示queryequal女性。

Below is the code where the user enter the information that is submitted to parse: 下面是用户输入要解析的信息的代码:

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

        }
    }

Below is the code that I am brainstorming could be used to retrieved information: 以下是我脑力激荡的代码可用于检索信息:

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

The issue i have now is setting a condition, where if a guy wants to be match with guy so only those guys who want to be match with guys information returns, and vice-versa for women. 我现在遇到的问题是设置一个条件,如果一个男人想与男人相匹配,那么只有那些想要与男人相匹配的男人才会返回,反之亦然。 Let me know, if this is fine. 让我知道是否可以。

Thanks in advance. 提前致谢。

The following line will ensure that the Users returned are only users who match the gender for which the current user is looking. 下一行将确保返回的用户仅是与当前用户正在寻找的性别匹配的用户。 That is, if I'm looking for a female, the constraint below will ensure that only females are returned. 也就是说,如果我要寻找女性,则以下约束将确保仅女性返回。

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

The following line will ensure that the Users returned are only users who are looking for other users that match the gender for which I am. 下一行将确保返回的用户仅是正在寻找与我的性别匹配的其他用户的用户。 That is, if I'm a male, the constraint below will ensure that only users who are looking for males will be returned. 也就是说,如果我是男性,则以下约束将确保仅返回寻找男性的用户。

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

If I understand your question correctly, using the above two constraints together should achieve what you're looking for. 如果我正确地理解了您的问题,那么将以上两个约束一起使用就可以实现您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM