Listview不是从远程服务器android填充

时间:2018-04-22 19:24:13

标签: android json listview post android-volley

我正在尝试从json加载ListView到post请求,api工作正常,代码中也没有错误,但不知何故ListView没有填充任何数据。请帮帮我。

这是我的活动。

public class Reciepe extends AppCompatActivity {

    String Barname;
    String finalJson;
    private ListView reciepeListView;
    private ProgressDialog loading;
    String url = "http://thehostels.in/Foody/reciepe_json.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reciepe);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        JSONTask();
        toolbar.setBackgroundColor(Color.parseColor("#FFBC03"));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        DisplayImageOptions options = new DisplayImageOptions.Builder()

                .cacheInMemory(true)
                .cacheOnDisk(true)

                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(Reciepe.this)
                .defaultDisplayImageOptions(options)
                .build();
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);
        reciepeListView = (ListView) findViewById(R.id.list_recipe);


        Intent intent = getIntent();
        if (intent != null) {

            Barname = intent.getStringExtra("Type");
            Log.e("Type", Barname);
        }

        if (Barname != null) {

            switch (Barname) {
                case "Punjabi":

                    getSupportActionBar().setTitle("Punjabi");
                    break;
                case "Chinese":

                    getSupportActionBar().setTitle("Chinese");
                    break;
                case "South Indian":

                    getSupportActionBar().setTitle("South Indian");
                    break;
                case "Gujarati":

                    getSupportActionBar().setTitle("Gujarati");
                    break;
                case "Chicken":

                    getSupportActionBar().setTitle("Chicken");
                    break;
            }
        }
    }

    public void JSONTask() {

        ProgressDialog loading;
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

        StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("response : ", response);

                JSONObject parentObject = null;
                try {
                    parentObject = new JSONObject(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JSONArray parentArray = null;
                try {
                    assert parentObject != null;
                    parentArray = parentObject.getJSONArray("list");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                List<Listview_reciepe_conveyer> fixture_conveyerList = new ArrayList<Listview_reciepe_conveyer>();

                assert parentArray != null;
                for (int i = 0; i < parentArray.length(); i++) {
                    JSONObject finalObject = null;
                    try {
                        finalObject = parentArray.getJSONObject(i);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Listview_reciepe_conveyer fixtureList = new Listview_reciepe_conveyer();
                    try {
                        assert finalObject != null;
                        fixtureList.setImage(finalObject.getString("image"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try {
                        fixtureList.setFood(finalObject.getString("food"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try {
                        fixtureList.setPrice(finalObject.getString("price"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    fixture_conveyerList.add(fixtureList);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("response : ", error.toString());

            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Dish", Barname);

                Log.d("Dish", ": " + params.toString());

                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };
        Log.d("Leaderboard", "Req : " + sr.getUrl());

        requestQueue.add(sr);
    }
}

ListAdapter

public class ListAdapter extends ArrayAdapter<Listview_reciepe_conveyer> {

    private List<Listview_reciepe_conveyer> reciepe_conveyerList;
    private int resource;
    private LayoutInflater inflater;

    public ListAdapter(Context context, int resource, List<Listview_reciepe_conveyer> objects) {
        super(context, resource, objects);
        reciepe_conveyerList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(resource, null);
        }

        ImageView food_photo;
        final TextView food,price;
        food_photo = (ImageView)convertView.findViewById(R.id.food_photo);
        food = (TextView)convertView.findViewById(R.id.food_name);
        price = (TextView)convertView.findViewById(R.id.food_price);
        ImageLoader.getInstance().displayImage(reciepe_conveyerList.get(position).getImage(), food_photo);

        food.setText(reciepe_conveyerList.get(position).getFood());
        String newprice= ("Rs."+reciepe_conveyerList.get(position).getPrice());
        price.setText(newprice);
        return convertView;
    }
}

Listview_conveyer

public class Listview_reciepe_conveyer {

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    private String image;
    private String food;
    private String price;

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

代码中没有错误,但ListView为空。我正在使用凌空库来获取数据,而api正在完美运行。我已经检查过邮差很好。请指导我。谢谢。

1 个答案:

答案 0 :(得分:0)

以下是您的代码的固定版本。请检查一下是否有效。请注意,我还没有测试过代码。我在代码中添加了一些注释,以便您可以了解在哪里放置什么。

public class Reciepe extends AppCompatActivity {

    String Barname;
    String finalJson;
    private ListView reciepeListView;
    private ListAdapter adapter;
    private ProgressDialog loading;
    List<Listview_reciepe_conveyer> fixture_conveyerList;
    String url = "http://thehostels.in/Foody/reciepe_json.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reciepe);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Set up ListView.
        reciepeListView = (ListView) findViewById(R.id.list_recipe);
        fixture_conveyerList = new ArrayList<Listview_reciepe_conveyer>();
        adapter = new ListAdapter(this, R.layout.list_item, fixture_conveyerList);
        reciepeListView.setAdapter(adapter);

        JSONTask();
        toolbar.setBackgroundColor(Color.parseColor("#FFBC03"));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(Reciepe.this)
                .defaultDisplayImageOptions(options)
                .build();
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);

        Intent intent = getIntent();
        if (intent != null) {
            Barname = intent.getStringExtra("Type");
            Log.e("Type", Barname);
        }

        if (Barname != null) {
            switch (Barname) {
                case "Punjabi":
                    getSupportActionBar().setTitle("Punjabi");
                    break;
                case "Chinese":
                    getSupportActionBar().setTitle("Chinese");
                    break;
                case "South Indian":
                    getSupportActionBar().setTitle("South Indian");
                    break;
                case "Gujarati":
                    getSupportActionBar().setTitle("Gujarati");
                    break;
                case "Chicken":
                    getSupportActionBar().setTitle("Chicken");
                    break;
            }
        }
    }

    public void JSONTask() {
        ProgressDialog loading;
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

        StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("response : ", response);

                JSONObject parentObject = null;
                try {
                    parentObject = new JSONObject(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JSONArray parentArray = null;
                try {
                    assert parentObject != null;
                    parentArray = parentObject.getJSONArray("list");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                assert parentArray != null;
                for (int i = 0; i < parentArray.length(); i++) {
                    JSONObject finalObject = null;
                    try {
                        finalObject = parentArray.getJSONObject(i);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Listview_reciepe_conveyer fixtureList = new Listview_reciepe_conveyer();
                    try {
                        assert finalObject != null;
                        fixtureList.setImage(finalObject.getString("image"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try {
                        fixtureList.setFood(finalObject.getString("food"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try {
                        fixtureList.setPrice(finalObject.getString("price"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    fixture_conveyerList.add(fixtureList);
                }

                // Call notifyDataSetChanged here to make the ListView to update its data
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("response : ", error.toString());

            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Dish", Barname);

                Log.d("Dish", ": " + params.toString());

                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };
        Log.d("Leaderboard", "Req : " + sr.getUrl());

        requestQueue.add(sr);
    }
}

这是适配器。

private class ListAdapter extends ArrayAdapter<Listview_reciepe_conveyer> {

    private List<Listview_reciepe_conveyer> reciepe_conveyerList;
    private int resource;
    private LayoutInflater inflater;

    public ListAdapter(Context context, int resource, List<Listview_reciepe_conveyer> objects) {
        super(context, resource, objects);
        reciepe_conveyerList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(resource, null);
        }

        ImageView food_photo;
        final TextView food, price;
        food_photo = (ImageView) convertView.findViewById(R.id.food_photo);
        food = (TextView) convertView.findViewById(R.id.food_name);
        price = (TextView) convertView.findViewById(R.id.food_price);
        ImageLoader.getInstance().displayImage(reciepe_conveyerList.get(position).getImage(), food_photo);

        food.setText(reciepe_conveyerList.get(position).getFood());
        String newprice = ("Rs." + reciepe_conveyerList.get(position).getPrice());
        price.setText(newprice);
        return convertView;
    }
}

和班级

public class Listview_reciepe_conveyer {

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    private String image;
    private String food;
    private String price;

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

布局list_item是要在ListView中显示为每个列表项的项目。

相关问题