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

查看:179
本文介绍了多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Parse.com两大类:形象和放大器;数据。在数据类,我节省了3场姓名,手机号码和放大器;占用。在图像类,我保存图像。

我要创建这两个类之间的关系,这样我就可以与相应的手机号码获取图像。一个移动号码可以节省许多图像和表格数据可以有多个手机号码。

无法找到我怎么能获取的图像与特定的手机号码。请帮助,因为我已经直通的文档。提前致谢。这是我的code:

 公共类的getImage延伸活动{
//声明变量
GridView控件GridView控件;
名单<的parseObject>肥胖;
ProgressDialog mProgressDialog;
GridViewAdapter适配器;
按钮imgbtn;
的EditText mbltxt;
字符串移动电话号码;
私人列表< PHONELIST> phonearraylist = NULL;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    //从gridview_main.xml视图
    的setContentView(R.layout.listview_main);
    //执行RemoteDataTask的AsyncTask
    imgbtn =(按钮)findViewById(R.id.imgbtn);
    mbltxt =(EditText上)findViewById(R.id.mbltxt);

    imgbtn.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根

                如果(mbltxt.getText()。的toString()。等于()){
                    Toast.makeText(getBaseContext(),请输入一个有效的移动电话号码,Toast.LENGTH_LONG).show();
                }其他 {
             新RemoteDataTask()执行()。
        }
        }
    });
}
 

在询问手机号码,点击的getImage按钮,查询所有图像的手机号码。

  // RemoteDataTask的AsyncTask
私有类RemoteDataTask扩展的AsyncTask<虚空,虚空,虚空> {
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        //创建一个progressdialog
        mProgressDialog =新ProgressDialog(GetImage.this);
        //设置progressdialog称号
        mProgressDialog.setTitle(图像);
        //设置progressdialog消息
        mProgressDialog.setMessage(载入中...);
        mProgressDialog.setIndeterminate(假);
        //显示progressdialog
        mProgressDialog.show();
    }

    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        //创建数组
        phonearraylist =新的ArrayList< PHONELIST>();
        尝试 {
            //在Parse.com找到名为图像之类的表
            ParseRelation<的parseObject>关系= currentUser.getRelation(IMG);
            ParseQuery<的parseObject>查询=新ParseQuery<的parseObject>(图像);

            OB = query.find();
            对于(的parseObject imgob:OB){
                ParseFile图像=(ParseFile)imgob.get(镜像文件);
                PHONELIST地图=新PHONELIST();
                map.setPhone(image.getUrl());
                phonearraylist.add(图)
            }
         }赶上(ParseException的E){
            Log.e(错误,e.getMessage());
            e.printStackTrace();
        }
        返回null;
    }

    @覆盖
    保护无效onPostExecute(无效的结果){
        //定位在gridview_main.xml在GridView
        gridview的=(GridView控件)findViewById(R.id.gridview);
        //将结果传递到ListViewAdapter.java
        适配器=新GridViewAdapter(GetImage.this,
                phonearraylist);
        //绑定适配器到ListView
        gridview.setAdapter(适配器);
        //关闭progressdialog
        mProgressDialog.dismiss();
    }
}}
 

解决方案

我不是一个Android开发者,但我会尽力场这一个。你有两个类,数据和图像。数据的每个实例可以关联到许多图像。该资料片,使数据唯一的每个实例的指定属性Mobile的号码。

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

  1. A Parse.com阵列是一个很好的解决方案,如果数据的实例是与比约50幅图像较少。当您将图片关联到一个数据,使用Parse.com增加的方法:

  aData.add(图像,someImageObject);
aData.saveInBackground();
 

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

  aData.addAll(图像,Arrays.asList(此搜索,图像2,的Image3));
 

当您检索解析数据实例,Image对象显示为指针的数组。为了拉回实际图像对象,请使用提取。例如如何使用提取,查找本节Parse.com文档:

  

在默认情况下,获取的对象时,相关ParseObjects不   牵强。这些对象的值不能被检索,直到他们有   被取像这样:

  fetchedComment.getParseObject(后)
    .fetchIfNeededInBackground(新GetCallback<的parseObject>(){
        公共无效做(的parseObject对象,ParseException的E){
          字符串标题= post.getString(标题);
        }
    });
 

  1. A Parse.com关系比阵列解决方案更具可扩展性。如果一个单一的数据与数百图像或数千相关联,这是正确的解决方案。我已经开始转换我的阵列,以我自己的code的关系。

    ParseRelation关系= aData.getRelation(图像);
    relation.add(someImageObject);
    aData.saveInBackground();

  1. 这是关联类是一个很好的解决方案,如果有关于关于例如数据和Image--之间的关系额外的信息,如果用户可以标记的图像作为我的一个最爱。为了解决这个问题,创建一个新的类Parse.com称为ImageAssociation。这个类有三个属性:

    • theDataObject(或只是数据对象的手机号码)
    • theImageObject
    • isFavorite(true或false)

我不会进入这个解决方案的机制。按照Waquas在他的一般信息的答案的链接。又见Parse.com文档中关于关系。

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".

这篇关于多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆