无法在包含指针字段的 Parse.com 对象的 GridView 数据中显示 - Android

Can't display in GridView data from Parse.com object that contains a Pointer field - Android

我想使用来自名为 Shop_photos 的对象的数据填充 gridview,该对象具有以下结构和其中的行:


下面代码中的问题是,即使我正在用结果填充搜索列表(我检查了这一点,因为 for 中的消息 "I AM HERE" 正在按应有的方式打印),出于某种原因,旗帜变成了 false,我为没有有效的商店照片而干杯,然后我返回上一页,正如我在 onPostExecuteelse 部分所说的那样.

这是Logcat:

04-27 17:29:42.400  30431-30431/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_launch_request id:guide_me_for_all.guide_me_for_all time:161438256
04-27 17:29:44.142  30431-30431/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_launch_request id:guide_me_for_all.guide_me_for_all time:161439997
04-27 17:29:44.472  30431-30431/guide_me_for_all.guide_me_for_all D/width of screen﹕ 480
04-27 17:29:44.492  30431-30431/guide_me_for_all.guide_me_for_all I/Choreographer﹕ Skipped 48 frames!  The application may be doing too much work on its main thread.
04-27 17:29:44.762  30431-30431/guide_me_for_all.guide_me_for_all D/SHOP ID1﹕ QfqCIzTSRg
04-27 17:29:45.013  30431-30431/guide_me_for_all.guide_me_for_all D/I AM HERE﹕ [ 04-27 17:29:45.043 30431:30431 I/Choreographer ]
    Skipped 121 frames!  The application may be doing too much work on its main thread.
04-27 17:29:47.065  30431-30431/guide_me_for_all.guide_me_for_all D/SHOP SUGGESTION﹕ Costa Coffee Nicosia
04-27 17:29:47.065  30431-30431/guide_me_for_all.guide_me_for_all I/MemoryCache﹕ MemoryCache will use up to 32.0MB
04-27 17:29:47.075  30431-30431/guide_me_for_all.guide_me_for_all I/Choreographer﹕ Skipped 442 frames!  The application may be doing too much work on its main thread.
04-27 17:29:47.355  30431-30817/guide_me_for_all.guide_me_for_all I/MemoryCache﹕ cache size=76800 length=1
04-27 17:29:47.405  30431-30431/guide_me_for_all.guide_me_for_all I/Choreographer﹕ Skipped 61 frames!  The application may be doing too much work on its main thread.
04-27 17:29:47.695  30431-30431/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41ffbda8 time:161443549
04-27 17:29:47.695  30431-30431/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41f2a440 time:161443549

这是我执行查询以填充 result 对象的片段:

public class ViewShopsPhotosFragment extends Fragment {

    protected ProgressDialog proDialog;

    private long mLastClickTime = 0;

    String shopname = null;
    String shopid = null;

    GridView gridview;
    List<ParseObject> ob;
    GridViewAdapter adapter;
    List<PhotosList> photosarraylist = null;

    boolean flag_photos = false;

    View convertView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        proDialog = CustomProgressDialog.ctor(this.getActivity());

        if (convertView == null) {
            convertView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_viewshopsphoto, container, false);

                Parse.initialize(this.getActivity().getApplicationContext(), "hoMaKMBjhkjh", "wWV193mEtPqbhjgh");

                // Execute RemoteDataTask AsyncTask
                new RemoteDataTask().execute();

        }
        return convertView;
    }

    // RemoteDataTask AsyncTask
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            proDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            photosarraylist = new ArrayList<PhotosList>();

            final ParseQuery<ParseObject> shop = ParseQuery.getQuery("Shop");
            shop.whereEqualTo("name", shopname); //shop name

            shop.findInBackground(new FindCallback<ParseObject>() {
                public void done(final List<ParseObject> shopList, ParseException e) {
                    if (e == null) {
                        shopid = shopList.get(0).getObjectId();

                        Log.d("SHOP ID1", shopid);

                        ParseQuery<ParseObject> query_photos = new ParseQuery<ParseObject>("Shop_photos");
                        query_photos.whereEqualTo("name", ParseObject.createWithoutData("Shop", shopid)); //shop name

                        //pio prosfati prwta
                        query_photos.orderByDescending("updatedAt");

                        try {
                            ob = query_photos.find();

                            for (ParseObject photo : ob) {
                                Log.d("I AM HERE", "");

                                flag_photos = true;

                                ParseFile image = (ParseFile) photo.get("photo");
                                PhotosList photolist = new PhotosList();

                                photolist.setPhoto(image.getUrl());
                                photolist.setShopname(shopname);

                                photosarraylist.add(photolist);
                            }
                        } catch (ParseException e1) {
                            e1.printStackTrace();
                        }
                    } else {
                        Log.d("shop", "Error: " + e.getMessage());
                    }
                }
            });

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            if (flag_photos) {
                // Locate the gridview in gridview_main.xml
                gridview = (GridView) convertView.findViewById(R.id.photos_grid);

                // Pass the results into ListViewAdapter.java
                adapter = new GridViewAdapter(getActivity(), photosarraylist);

                // Binds the Adapter to the ListView
                gridview.setAdapter(adapter);
            }
            else{
                Toast.makeText(getActivity().getApplicationContext(), "We couldn't find any photos for this shop!", Toast.LENGTH_LONG).show();

                Intent intent;
                intent = new Intent(getActivity().getApplicationContext(), BaseActivity.class);

                intent.putExtra("flag_showshop", "Y");
                intent.putExtra("shopname", shopname);

                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                getActivity().overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);

                proDialog.hide();

                startActivity(intent);
            }

            proDialog.hide();
        }
    }
}

这是我发送的 class 数据来自 - PhotosList:

public class PhotosList {
    private String photo;
    private String shopname;

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getShopname() {
        return shopname;
    }

    public void setShopname(String shopname) {
        this.shopname = shopname;
    }
}

这是在文本视图中设置数据的 GridViewAdapter e.t.c:

public class GridViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ImageLoader imageLoader;
    private List<PhotosList> photosarraylist = null;
    private ArrayList<PhotosList> arraylist;

    public GridViewAdapter(Context context, List<PhotosList> photosarraylist) {
        this.context = context;
        this.photosarraylist = photosarraylist;
        inflater = LayoutInflater.from(context);
        this.arraylist = new ArrayList<PhotosList>();
        this.arraylist.addAll(photosarraylist);
        imageLoader = new ImageLoader(context);
    }

    public class ViewHolder {
        ImageView phone;
    }

    @Override
    public int getCount() {
        return photosarraylist.size();
    }

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

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

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.gridview_item_viewphotos, null);
            // Locate the ImageView in gridview_item.xml
            holder.phone = (ImageView) view.findViewById(R.id.photo);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Load image into GridView
        imageLoader.DisplayImage(photosarraylist.get(position).getPhoto(),
                holder.phone);
        // Capture GridView item click
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Send single item click data to SingleItemView Class
                Intent intent = new Intent(context, SingleItemView_GridPhotosActivity.class);
                // Pass all data phone
                intent.putExtra("photo", photosarraylist.get(position).getPhoto());
                intent.putExtra("shopname", photosarraylist.get(position).getShopname());

                context.startActivity(intent);
            }
        });
        return view;
    }
}

这是第一个片段的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="false"
    tools:context="guide_me_for_all.guide_me_for_all.ViewPhotoCommentActivity"
    android:background="@drawable/pattern9">

    <LinearLayout
        android:orientation="vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <ImageButton
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:id="@+id/lessinfo"
                android:background="@drawable/less"
                android:layout_alignParentTop="false"
                android:layout_alignParentRight="false"
                android:layout_alignParentEnd="false"
                android:layout_alignParentLeft="true" />

        </RelativeLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ecf0f1"
            android:layout_marginBottom="16dp">

            <GridView
                android:id="@+id/photos_grid"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:columnWidth="120dp"
                android:gravity="center"
                android:horizontalSpacing="5dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="5dp"
                android:layout_marginTop="16dp"
                android:layout_gravity="center"
                android:nestedScrollingEnabled="true"
                android:smoothScrollbar="true"
                android:layout_marginBottom="16dp" />
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

这是每个 gridview 项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

首先,我想您已经完成了开始在 Parse 上共享数据的第一个必要步骤(注册和初始化)。我这么说是因为在你的模型 class (PhotoList) 中我没有看到任何关于这个的注释。

其次,也是更重要的一点,我喜欢 Parse,如果你要处理数据然后使用适配器 Grid/List 视图,最好的方法不是 BaseAdapter 的扩展,而是更好的 ParseQueryAdapter。

你可以获取适配器中的所有数据,然后你可以为所欲为,因为像 BaseAdapter 或 ArrayAdapter 一样,也有 getView 方法。

我建议您查看这些链接:

(示例来自 GitHub)https://github.com/ParsePlatform/ParseQueryAdapterTutorial/blob/master/src/com/parse/samples/parsequeryadapter/CustomAdapter.java

(官方文档)https://parse.com/tutorials/parse-query-adapter

希望你喜欢! :)