许多对许多的关系[英] Many to many relation

本文是小编为大家收集整理的关于许多对许多的关系的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有Parse.com两类:图像和数据.在数据类,我节省了3场姓名,手机号码和职业.在图像类我保存图像.

我要创建这两个类之间的关系,这样我可以与相应的移动设备的数目取图像.一个移动电话号码可以节省许多图像和表中的数据可以有许多手机号码.

不能够找到如何可以与特定移动号码获取图像.请帮我去了的文档通.提前致谢.这是我的代码:

public class GetImage extends Activity {
// Declare Variables
GridView gridview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
GridViewAdapter adapter;
Button imgbtn;
EditText mbltxt;
String mobileNumber;
private List<PhoneList> phonearraylist = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from gridview_main.xml
    setContentView(R.layout.listview_main);
    // Execute RemoteDataTask AsyncTask
    imgbtn=(Button) findViewById(R.id.imgbtn);
    mbltxt = (EditText) findViewById(R.id.mbltxt);

    imgbtn.setOnClickListener(new View.OnClickListener(){

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

                if (mbltxt.getText().toString().equals("")) {
                    Toast.makeText(getBaseContext(), "Please enter a valid mobilenumber", Toast.LENGTH_LONG).show();    
                }else {
             new RemoteDataTask().execute();
        } 
        }
    });       
}

询问移动号码后,点击按钮的getImage查询的所有图像为移动电话号码.

// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(GetImage.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Images");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        phonearraylist = new ArrayList<PhoneList>();
        try {
            // Locate the class table named "Image" in Parse.com
            ParseRelation<ParseObject> relation = currentUser.getRelation("img");
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Image");

            ob = query.find();
            for (ParseObject imgob : ob) {
                ParseFile image = (ParseFile) imgob.get("ImageFile");
                PhoneList map = new PhoneList();
                map.setPhone(image.getUrl());
                phonearraylist.add(map);
            }
         }catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the gridview in gridview_main.xml
        gridview = (GridView) findViewById(R.id.gridview);
        // Pass the results into ListViewAdapter.java
        adapter = new GridViewAdapter(GetImage.this,
                phonearraylist);
        // Binds the Adapter to the ListView
        gridview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}}

推荐答案

我不是一个Android开发者,但我会尽力领域本之一.你有两个班,数据和图像.数据的每个实例可以关联到许多图像.该件,使数据的独特的每个实例的信息称为移动设备数量的属性.

您有三个选项: 1. Parse.com阵列 2. Parse.com关系 3.关联类(如Waquas建议).

  1. A <强> Parse.com阵列是一个很好的解决方案,如果数据的一个实例被链接到大于约50倍的图像少.当一个图像的数据相关联,使用Parse.com"添加"方法:
aData.add("images", someImageObject);
aData.saveInBackground();

是可能的图像列表一气呵成关联:的

aData.addAll("images", Arrays.asList(image1, image2, image3));

当从解析检索数据情况下,图像对象被显示为"指针"的阵列.拉回来的实际图像对象,用"取".例如如何使用取,查找Parse.com文档的本节:

默认情况下,获取的对象时,相关ParseObjects不 牵强.无法检索这些对象的值,直到他们有 被取出,如下所示:

fetchedComment.getParseObject("post")
    .fetchIfNeededInBackground(new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
          String title = post.getString("title");
        }
    }); 
  1. A <强> Parse.com关系比数组溶液更具伸缩性.如果一个数据与数百或数千张图片的关联,这是正确的解决方案.我已经开始转换我的阵列,以在自己的代码关系.
    ParseRelation relation = aData.getRelation("images");    
    relation.add(someImageObject);     
    aData.saveInBackground();
  1. 这是的关联类是,如果有关于关于例如数据和Image--之间的关系的附加信息,如果用户可以标记的图像作为"中的一个很好的解决我的最爱".为了解决这个问题,创建Parse.com一个新类叫ImageAssociation.类有三个属性:

    • theDataObject(或只是该数据对象的移动号码)
    • theImageObject
    • isFavorite(true或false)

我不会进入该溶液的机制.按照Waquas'在他的答案一般信息的链接.另见关于"关系"的Parse.com文档.

<01>

我无法确切地告诉你的代码,而是从数据库的角度来看,它不是一个好主意,有许多表许多关系.为了解决许多<- >许多的关系,就必须打破他们如下:

<强>一个 - >许多<- 一个

有关的详细信息,请阅读本: http://qisql.com/qisql_sqlite_many2many.html

本文地址:https://www.itbaoku.cn/post/102541.html

问题描述

I have two classes on Parse.com : Image & Data. In Data class, I am saving 3 fields Name,Mobile-number & Occupation. In Image class I'm saving images.

I have to create relation between these two classes so that I can fetch images with the corresponding mobile-number. one mobile number can save many images and the table Data can have many mobile numbers.

Not able to find how can I fetch the images with a particular mobile number. Please help as I have gone thru the documentation. Thanks in advance. Here is my code:

public class GetImage extends Activity {
// Declare Variables
GridView gridview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
GridViewAdapter adapter;
Button imgbtn;
EditText mbltxt;
String mobileNumber;
private List<PhoneList> phonearraylist = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from gridview_main.xml
    setContentView(R.layout.listview_main);
    // Execute RemoteDataTask AsyncTask
    imgbtn=(Button) findViewById(R.id.imgbtn);
    mbltxt = (EditText) findViewById(R.id.mbltxt);

    imgbtn.setOnClickListener(new View.OnClickListener(){

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

                if (mbltxt.getText().toString().equals("")) {
                    Toast.makeText(getBaseContext(), "Please enter a valid mobilenumber", Toast.LENGTH_LONG).show();    
                }else {
             new RemoteDataTask().execute();
        } 
        }
    });       
}

After asking for mobile number, click on getImage button to query all the images for that mobile number.

// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(GetImage.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Images");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        phonearraylist = new ArrayList<PhoneList>();
        try {
            // Locate the class table named "Image" in Parse.com
            ParseRelation<ParseObject> relation = currentUser.getRelation("img");
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Image");

            ob = query.find();
            for (ParseObject imgob : ob) {
                ParseFile image = (ParseFile) imgob.get("ImageFile");
                PhoneList map = new PhoneList();
                map.setPhone(image.getUrl());
                phonearraylist.add(map);
            }
         }catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the gridview in gridview_main.xml
        gridview = (GridView) findViewById(R.id.gridview);
        // Pass the results into ListViewAdapter.java
        adapter = new GridViewAdapter(GetImage.this,
                phonearraylist);
        // Binds the Adapter to the ListView
        gridview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}}

推荐答案

I'm not an Android developer, but I'll try to field this one. You have two classes, Data and Image. Each instance of Data can be associated to many Images. The piece of information that makes each instance of Data unique is the attribute named Mobile-Number.

You have three options: 1. Parse.com Array 2. Parse.com Relation 3. An association class (as Waquas suggests).

  1. A Parse.com array is a good solution if an instance of Data is linked to fewer than about 50 images. When you associate an Image to a Data, use the Parse.com "add" method:
aData.add("images", someImageObject);
aData.saveInBackground();

It is possible to associate a list of Images in one go:

aData.addAll("images", Arrays.asList(image1, image2, image3));

When you retrieve a Data instance from parse, the Image objects show up as an array of "pointers". To pull back the actual Image objects, use "fetch". For example of how to use fetch, look for this section of the Parse.com documentation:

By default, when fetching an object, related ParseObjects are not fetched. These objects' values cannot be retrieved until they have been fetched like so:

fetchedComment.getParseObject("post")
    .fetchIfNeededInBackground(new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
          String title = post.getString("title");
        }
    }); 
  1. A Parse.com relation is more scalable than the array solution. If a single Data is associated with hundreds or thousands of images, this is the correct solution. I have started switching my arrays to relations in my own code.
    ParseRelation relation = aData.getRelation("images");    
    relation.add(someImageObject);     
    aData.saveInBackground();
  1. An association class is a good solution if there is extra information about about the relationship between Data and Image-- for example, if a user can mark an image as "one of my favorites". To solve this problem, create a new class in Parse.com called ImageAssociation. The class has three attributes:

    • theDataObject (or just the Data object's Mobile Number)
    • theImageObject
    • isFavorite (true or false)

I won't go into the mechanics of this solution. Follow Waquas' link in his answer for general information. See also the Parse.com documentation about "relations".

其他推荐答案

I can't exactly tell you the code, but from database perspective, its not a good idea to have tables with many to many relationship. In order to resolve many <-> many relationships, you must break them as below:

one -> many <- one

For more info, read this: http://qisql.com/qisql_sqlite_many2many.html