CardStackView添加具有数据库信息的位置

时间:2018-08-01 15:59:58

标签: android database

很抱歉,如果此帖子没有正确填写,但生病了,请尽我所能。我正在尝试使用需要一个TinderLike滑动支架的android studio创建一个android应用。

为此,我正在使用以下库:CardStackView

如果我使用默认代码填充图片,则一切正常。

private List<TouristSpot> createTouristSpots() {
    List<TouristSpot> spots = new ArrayList<>();
    spots.add(new TouristSpot("Yasaka Shrine", "Kyoto", "https://source.unsplash.com/Xq1ntWruZQI/600x800"));
    spots.add(new TouristSpot("Fushimi Inari Shrine", "Kyoto", "https://source.unsplash.com/NYyCqdBOKwc/600x800"));
    spots.add(new TouristSpot("Bamboo Forest", "Kyoto", "https://source.unsplash.com/buF62ewDLcQ/600x800"));
    spots.add(new TouristSpot("Brooklyn Bridge", "New York", "https://source.unsplash.com/THozNzxEP3g/600x800"));
    spots.add(new TouristSpot("Empire State Building", "New York", "https://source.unsplash.com/USrZRcRS2Lw/600x800"));
    spots.add(new TouristSpot("The statue of Liberty", "New York", "https://source.unsplash.com/PeFk7fzxTdk/600x800"));
    spots.add(new TouristSpot("Louvre Museum", "Paris", "https://source.unsplash.com/LrMWHKqilUw/600x800"));
    spots.add(new TouristSpot("Eiffel Tower", "Paris", "https://source.unsplash.com/HN-5Z6AmxrM/600x800"));
    spots.add(new TouristSpot("Big Ben", "London", "https://source.unsplash.com/CdVAUADdqEc/600x800"));
    spots.add(new TouristSpot("Great Wall of China", "China", "https://source.unsplash.com/AWh9C-QjhE4/600x800"));
    return spots;
}

但是,当我尝试使用从数据库中检索信息的代码更改默认填充代码时,将不再显示任何内容。

private List<TouristSpot> createTouristSpots() {
    final List<TouristSpot> spots = new ArrayList<>();

    String url = AppData.api + "radar_2.php";

    alerttext.setVisibility(View.VISIBLE);

    SharedPreferences token = getApplicationContext().getSharedPreferences(AppData.sp_user_info, 0);
    String user_id = token.getString(AppData.sp_user_id, "0");
    UserData userData=new AppData().getUserData();

    Map<String, String> params = new HashMap<String, String>();

    params.put("user_id", user_id);
    params.put("number", String.valueOf(n));
    params.put("gender", userData.getLooking_gender());
    params.put("looking_age_start",userData.getLooking_age_start());
    params.put("looking_age_end", userData.getLooking_age_end());
    params.put("latitude", userData.getLatitude());
    params.put("longitude", userData.getLongitude());
    params.put("distance", "100");

    CustomRequest jsObjRequest = new CustomRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>()
    {
        @Override
        public void onResponse(JSONObject response)
        {
            if (n == 1)
            {
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("MATCHDATALIST", response.toString());
                editor.commit();
            }

            try
            {

                String str = response.getString("Error");

                if (str.equals("None"))
                {
                    JSONArray jsonArray = response.getJSONArray("Responsedata");
                    //Toast.makeText(getActivity(), jsonArray.toString(), Toast.LENGTH_LONG).show();

                    for (int i = 0; jsonArray.length() > i; i++)
                    {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        spots.add(new TouristSpot(jsonObject.getString("user_id"),jsonObject.getString("username"),jsonObject.getString("hometown"),jsonObject.getString("age"),jsonObject.getString("gender"),jsonObject.getString("profile_image"),jsonObject.getString("interests"),jsonObject.getString("distance")));
                    }

                    alerttext.setVisibility(View.GONE);

                } else {
                    //Toast.makeText(getActivity(), response.getString("Error"), Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e)
            {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError response)
        {
            //Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_LONG).show();
        }
    });

    jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(15000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    AppController.getInstance().addToRequestQueue(jsObjRequest);

    //Toast.makeText(getActivity(), spots.toString(), Toast.LENGTH_LONG).show();

    return spots;
}

在我的OnCreateView中,我正在调用函数reload();

private void reload() {
    cardStackView.setVisibility(View.GONE);
    adapter = createTouristSpotCardAdapter();
    cardStackView.setAdapter(adapter);
    cardStackView.setVisibility(View.VISIBLE);
}

private TouristSpotCardAdapter createTouristSpotCardAdapter() {
    final TouristSpotCardAdapter adapter = new TouristSpotCardAdapter(getApplicationContext());
    adapter.addAll(createTouristSpots());
    return adapter;
}

我确实检查了JSONArray的字符串,并且从数据库中检索到的所有信息都在其中。

PS:TouristSpot适用于包含数据库中的所有变量。

如果有人能让我走上正确的道路,使其与从我的数据库中检索到的值一起使用,我将非常满意

此致, 杰里米

0 个答案:

没有答案