我在Parse.com上有兩個類:圖片&數據。在Data類中,我保存了3個字段名稱,手機號碼&職業。在Image類中,我正在保存圖像。多對多關係
我必須在這兩個類之間創建關係,以便我可以使用相應的手機號碼獲取圖像。一個手機號碼可以保存很多圖像,而表格Data可以有很多手機號碼。
無法找到我怎麼能獲取的圖像與特定的手機號碼。請幫助,因爲我已經通過文件。提前致謝。這裏是我的代碼:
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();
}
}}
如果每個Image只有一個手機號碼,那麼這不是一個多對多的關係,它只是一對多的關係。正如我在其他類似問題中所說的,請勿使用可能會更改的字段鏈接對象。在解析中,您應該喜歡使用對象引用(例如,'Image'可以有一個名爲'relatedData'的屬性,您可以將其設置爲'Data'記錄,這只是存儲指向該記錄的鏈接並使查詢更容易。 –
你知道如何做到這一點?此外,如果項目是開源的,請做一個鏈接 – Zen