完全空白列表视图

时间:2016-04-30 11:05:23

标签: php android listview android-volley

我正在使用排球工作自定义Listview。主要来源是listview using volley

I have made just a little change
  (1)replace movies.json with my php url
  (2)here replace some string and comment some part because it not needed for me


package info.androidhive.customlistviewvolley;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import info.androidhive.customlistviewvolley.adater.CustomListAdapter;
import info.androidhive.customlistviewvolley.app.AppController;
import info.androidhive.customlistviewvolley.model.Movie;

public class MainActivity extends Activity {
    // Log tag
    private static final String TAG = MainActivity.class.getSimpleName();

    // Movies json url
    private static final String url = "myurl.php";
    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;
    private CustomListAdapter adapter;

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

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, movieList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        // changing action bar color
        getActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#1b1b1b")));

        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {


                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.seturl(obj.optString("image"));
                                Log.e("Test",obj.optString("image"));
                                movie.setfullname(obj.optString("fullname"));
                                movie.setRating(obj.optString("location"));

                                //movie.setYear(obj.getInt("location"));

                                // Genre is json array
//                          //  JSONArray genreArry = obj.getJSONArray("genre");
//                              ArrayList<String> genre = new ArrayList<String>();
//                              for (int j = 0; j < genreArry.length(); j++) {
//                                  genre.add((String) genreArry.get(j));
//                              }
//                              movie.setGenre(genre);

                                // adding movie to movies array
                                movieList.add(movie);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());

                        hidePDialog();

                    }
                });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我的PHP代码是

  <?php

            require_once('dbConnect.php');

            $sql = "select image,fullname,location from uploadfinding";

            $res = mysqli_query($conn,$sql);


            $result = array();

            while($row = mysqli_fetch_array($res)){
                    array_push($result, array(
                        "image"=>$row['image'],"fullname"=>$row['fullname'],"location"=>$row['location']));


                echo "<pre>";       

                }

            echo json_encode($result);

            mysqli_close($conn);
            ?>

但仅显示空白页面后。 不知道出了什么问题。 //记录错误

04-30 18:00:25.788 5321-5353/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.Restarter.getActivities
04-30 18:00:50.758 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.app.AppController.access$super
04-30 18:00:50.808 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.app.AppController.access$super
04-30 18:00:50.828 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.app.AppController.access$super
04-30 18:00:50.878 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.app.Application$OnProvideAssistDataListener', referenced from method info.androidhive.customlistviewvolley.app.AppController.access$super
04-30 18:00:50.878 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.app.AppController.access$super
04-30 18:00:50.908 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.app.AppController.access$super
04-30 18:00:50.948 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.app.AppController.access$super
04-30 18:00:50.998 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.app.Application$OnProvideAssistDataListener', referenced from method info.androidhive.customlistviewvolley.app.AppController.access$super
04-30 18:00:51.068 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
04-30 18:00:51.098 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
04-30 18:00:51.217 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.278 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.297 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.329 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.338 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.357 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.app.TaskStackBuilder', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.457 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.app.TaskStackBuilder', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.508 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.588 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.617 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.617 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.638 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.688 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.transition.TransitionManager', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.768 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.838 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.858 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:51.998 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:52.088 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:52.118 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super
04-30 18:00:52.188 6781-6781/info.androidhive.customlistviewvolley E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method info.androidhive.customlistviewvolley.MainActivity.access$super

2 个答案:

答案 0 :(得分:0)

确保您的xml中包含以下代码

 <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

答案 1 :(得分:0)

在你的php文件中添加这些行,并使用results.json ..

更改url
  $fp = fopen('results.json', 'w');
fwrite($fp, json_encode($result));
fclose($fp);

我希望这对你有用..