简体   繁体   English

具有子类,Android的ParseQueryAdapter为什么我不能从Parse取回数据?

[英]ParseQueryAdapter with Subclasses, android Why can't I get my data back from Parse?

I am using Parse.com to build an Android app, and I am having the hardest time figuring out what is not working. 我正在使用Parse.com来构建一个Android应用程序,而我却花了最艰难的时间来弄清什么不起作用。 Why am I not seeing my list of Category names? 为什么我看不到类别名称列表?

I am not getting any errors, but I am not seeing my data when my activity runs. 我没有收到任何错误,但是我的活动运行时看不到我的数据。 I have these two parse classes, and a recipe could be part of more than one category. 我有这两个解析类,一个配方可能属于多个类别。

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

I have created and saved my data in parse by calling a CreateData function that looks like this: 通过调用如下所示的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();
}

Here is the layout for my category_list.xml file: 这是我的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>

And finally, here is my CategoryListActivity onCreate method. 最后,这是我的CategoryListActivity onCreate方法。 Again, I am putting a TextView in the layout just so I can see if the layout is being used. 同样,我将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();
        }

    }
}

I ended up using a ListActivity, and creating a custom adapter. 我最终使用了ListActivity,并创建了一个自定义适配器。

public class CategoryListActivity extends ListActivity {

int listCount = -1;
private CategoryViewListViewAdapter adapter;

public static final String TAG = CategoryListActivity.class.getSimpleName();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getListView().setClickable(true);

    CategoryViewListViewAdapter adapter = new CategoryViewListViewAdapter(this);

    adapter.addOnQueryLoadListener(new ParseQueryAdapter.OnQueryLoadListener<Category>() {
        @Override
        public void onLoading() {
            //Toast.makeText(getApplicationContext(), "Loading...", Toast.LENGTH_LONG).show();
            Log.d(TAG, "Loading.....");
        }

        @Override
        public void onLoaded(List<Category> list, Exception e) {
            Log.d(TAG, "Loading complete.");
            Log.d(TAG, "List contains "+ list.size() + " items");
            // this is just to see if we are getting anywhere at all.


        }
    });

   setListAdapter(adapter);


}

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

    }
}

// OnListItemClick event
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // Display item in toast.
    super.onListItemClick(l, v, position, id);

    Category cat = (Category) l.getAdapter().getItem(position);
    String objId = cat.getObjectId().toString();
    Log.d(TAG, "Category name = "+ cat.getName());
    Log.d(TAG, "Category object id = "+ objId);
    // now we have the category, we want to go get the list of Recipes in that category.
    Intent intent = new Intent(this, RecipeListActivity.class);
    EventBus.getDefault().postSticky(cat);
    startActivity(intent);

}



class CategoryViewListViewAdapter extends ParseQueryAdapter<Category> {

public CategoryViewListViewAdapter(Context context) {
    super(context, new ParseQueryAdapter.QueryFactory<Category>() {
        public ParseQuery<Category> create() {
            ParseQuery<Category> query = ParseQuery.getQuery(Category.class);
            //query.include("User");
            query.whereExists("name");
            query.orderByAscending("name");
            return query;
        }
    });

}
@Override
public View getItemView(Category category, View v, ViewGroup parent) {
    if(v == null) {
        v = View.inflate(getContext(), R.layout.category_list_item, null);
    }

    super.getItemView(category, v, parent);

    final TextView catTextView = (TextView) v.findViewById(R.id.category_name);
    catTextView.setText(category.getName());
    return v;
}

} }

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

相关问题 如何使用ParseQueryAdapter从另一个类获取数据(Android + Parse) - How to get data from another class using ParseQueryAdapter (Android + Parse) Android parse:从 parsequeryadapter 中获取字符串并将它们存储到一个 ArrayList 中 - Android parse: Get strings from parsequeryadapter and store them into an ArrayList Android parse.com我无法从数据库获得第二个用户 - Android parse.com I can't get second user from data base 为什么我不能从我的活动中获取数据到我的片段? - Why can't I get data from my activity to my fragment? Android Back4App(Parse)从多个类获取数据 - Android Back4App(Parse) get data from multiple classes Android解析:本地数据存储和ParseQueryAdapter不兼容? - Parse for Android: Local Datastore and ParseQueryAdapter incompatible? 为什么我不能在我的android项目中获取资源文件? - Why can't I get a resource file in my android project? 为什么我的Android手机无法获得经度和纬度? - Why can't I get the latitude and longitude for my android phone? 为什么我无法从Android Studio中的MainActivity获取上下文? - Why can't I get context from my MainActivity in android studio? 为什么我不能在我的 Android 应用程序中使用意图转换回以前的片段? - Why can't I use an intent to transition back to a previous fragment in my Android app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM