Android - Parse Server SDK的GridAdapter问题 [英] Android - GridAdapter issue with Parse Server SDK

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

问题描述

我正在进入Android,并且我是一名绝对的初学者,所以我希望你能帮我解决我的自定义GridAdapter和Parse SDK的问题,Logcat不会显示任何错误,但设备仍然崩溃而不显示任何数据。

I'm getting into Android and I'm an absolute beginner with that, so I hope you can help me with an issue with my custom GridAdapter and Parse SDK, the Logcat doesn't show me any error but the device still crashes without showing any data.

以下是我的代码:

Here's my code :

public class Home extends AppCompatActivity {

/* Views */
ProgressDialog progDialog;


/* Variables */
List<ParseObject> eventsArray = null;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    // ProgressDialog
    progDialog = new ProgressDialog(this);
    progDialog.setTitle(R.string.app_name);
    progDialog.setMessage("Loading...");
    progDialog.setIndeterminate(false);
    progDialog.show();

    // Call query
    queryEvents();
}



// MARK: - QUERY EVENTS
public  void queryEvents() {

    ParseQuery<ParseObject> query = ParseQuery.getQuery(Configs.EVENTS_CLASS_NAME);
    query.whereEqualTo(Configs.EVENTS_IS_PENDING, false);
    query.orderByAscending(Configs.EVENTS_END_DATE);
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException error) {
            if (error == null) {
                eventsArray = objects;
                progDialog.dismiss();


                // CUSTOM GRID ADAPTER
                class GridAdapter extends BaseAdapter {
                    private List<ParseObject> objects;
                    private Context context;


                    public GridAdapter(FindCallback<ParseObject> context, List<ParseObject> objects) {
                        super();
                        this.context = (Context) context;
                        eventsArray = objects;
                    }

                    // CONFIGURE CELL
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View gridView;

                        if (convertView == null) {
                            gridView = new View(context);

                            // Get layout from event_cell.xml
                            gridView = inflater.inflate(R.layout.event_cell, null);

                            // Get title
                            TextView titleTxt = (TextView) gridView.findViewById(R.id.titleTxt);
                            if (eventsArray.get(position).getString(Configs.EVENTS_TITLE) != null) {
                                titleTxt.setText(eventsArray.get(position).getString(Configs.EVENTS_TITLE).toString());
                            } else { titleTxt.setText("N/A"); }




                        } else { gridView = (View) convertView; }

                        return gridView;
                    }

                    @Override
                    public int getCount() {
                        if (eventsArray != null) {
                            return eventsArray.size();
                        }
                        return 0;

                    }

                    @Override
                    public Object getItem(int position) {
                        return eventsArray.get(position);
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }
                }


                // Init GridView and set its adapter
                GridView eventsGrid = (GridView) findViewById(R.id.eventsGridView);
                eventsGrid.setAdapter(new GridAdapter(this, eventsArray));

                eventsGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                        Toast.makeText(getApplicationContext(), ((TextView) v.findViewById(R.id.titleTxt)).getText(), Toast.LENGTH_SHORT).show();

                    }
                });


                // Error in query
            } else {
                Toast.makeText(getApplicationContext(), error.getMessage().toString(), Toast.LENGTH_LONG).show();
            }

        }
    });

}


} //@end

我创建了一个 event_cell.xml ,并且已成功记录 Home.java 中的 eventsArray ,所以Parse SDK工作正常,我可以从我的数据库收集数据(如果您认为这可能是从Parse Server中检索数据导致的问题)。

I've created an event_cell.xml, and I've successfully logged the eventsArray in Home.java so the Parse SDK works fine, I can gather data from my database (in case you'll think it may be an issue caused by retrieving data from Parse Server).

我在这里做错了吗?

推荐答案

您可以在Activity类中设置最终的Context变量。在innerClass(FindCallback)中使用此变量

You can set final Context variable in Activity class. Use this variable in the innerClass(FindCallback)

public  void queryEvents() {
    final Context myContext= this;
    ParseQuery<ParseObject> query = ParseQuery.getQuery(Configs.EVENTS_CLASS_NAME);
    query.whereEqualTo(Configs.EVENTS_IS_PENDING, false);
    query.orderByAscending(Configs.EVENTS_END_DATE);
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException error) {

            ....
            public GridAdapter(Context context, List<ParseObject> objects) {
                super();
                this.context = context;
                eventsArray = objects;
            }
            ...
            eventsGrid.setAdapter(new GridAdapter(myContext, eventsArray));

        }
    });
}

这篇关于Android - Parse Server SDK的GridAdapter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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