0
我正在使用Parse.com構建一個Android應用程序,並且我正在最困難的時候弄清楚什麼不起作用。爲什麼我沒有看到我的分類名稱列表?帶子類的ParseQueryAdapter,android爲什麼我無法從Parse獲取數據?
我沒有收到任何錯誤,但是當我的活動運行時沒有看到我的數據。我有這兩個解析類,一個配方可能是多個類別的一部分。
@ParseClassName("Category")
public class Category extends ParseObject {
public Category() {
}
public String getName() {
return getString("name");
}
public void setName(String name) {
put("name", name);
}
}
@ParseClassName("Recipe")
public class Recipe extends ParseObject{
public Recipe() {
}
public String getName() {
return getString("name");
}
public void setName(String name) {
put("name", name);
}
public Number getTotalMeatAndFat(){
return getNumber("totalMeatAndFat");
}
public void setTotalMeatAndFat(Number totalMeatAndFat){put("totalMeatAndFat", totalMeatAndFat);}
public String getUnit(){ return getString("unit");}
public void setUnit(String unit) {
put("unit", unit);
}
}
我已經創建並通過調用函數CreateData看起來像這樣保存我的解析數據:
public void CreateData() {
// create the categories
Category fCat = new Category();
fCat.put("name", "Fresh Sausages");
fCat.saveInBackground();
Category erCat = new Category();
erCat.put("name", "Fermented Sausages");
erCat.saveInBackground();
// create the base recipes
Recipe rec = new Recipe();
rec.setName("Sweet Italian");
rec.setTotalMeatAndFat(1000);
rec.setUnit("g");
Recipe rec1 = new Recipe();
rec1.setName("Hot Italian");
rec1.setTotalMeatAndFat(1000);
rec1.setUnit("g");
Recipe rec2 = new Recipe();
rec2.setName("Chorizo Cantimpalos Style");
rec2.setTotalMeatAndFat(1000);
rec2.setUnit("g");
// assign recipes to categories
ParseRelation<Category> relationRecCat = rec.getRelation("categories");
relationRecCat.add(fCat);
rec.saveInBackground();
ParseRelation<Category> relationRec1Cat = rec1.getRelation("categories");
relationRec1Cat.add(fCat);
rec1.saveInBackground();
ParseRelation<Category> relRec2Cat = rec2.getRelation("categories");
relRec2Cat.add(erCat);
rec2.saveInBackground();
}
這裏是我的category_list.xml文件的佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
最後,這裏是我的CategoryListActivity onCreate方法。再次,我在佈局中放置了一個TextView,以便我可以看到佈局是否正在使用。
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.ParseQueryAdapter;
import com.parse.ParseUser;
public class CategoryListActivity extends Activity {
private TextView titleTextView;
private ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_list);
listView = (ListView) findViewById(R.id.listview);
ParseQueryAdapter<Category> adapter;
adapter = new ParseQueryAdapter<Category>(this , Category.class);
adapter.setTextKey("name");
listView.setAdapter(adapter);
// this is just to see if we are getting anywhere at all.
titleTextView = (TextView) findViewById(R.id.title);
titleTextView.setText(R.string.hello_world);
}
@Override
protected void onStart() {
super.onStart();
// Set up the profile page based on the current user.
ParseUser user = ParseUser.getCurrentUser();
showProfile(user);
}
private void showProfile(ParseUser user) {
if (user != null) {
String fullName = user.getString("name");
if (fullName != null) {
Toast.makeText(getApplicationContext(), "You are logged in as" + fullName, Toast.LENGTH_SHORT).show();
}
}
}
新ParseQueryAdapter(此,Category.class);什麼id Category.class意味着它給你的名單或什麼? –
Pavya
這就是我想要做的,但它不起作用。我試圖獲取所有類別對象的列表 –