-1

イメージ ローダーを使用して、リモート URL の JSON からイメージを取得し、グリッド ビューに入力しています。

私はクラスを持っています::

  • FileCache.java
  • ImageLoader.java
  • MemoryCache.java
  • Utils.java

MainActivity.java

public class MainActivity extends Activity {
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;

    static String FLAG = "flag";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);


        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Android JSON Parse Tutorial");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions.getJSONfromURL("-----------url-------");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("restaurants");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects

                    map.put(MainActivity.FLAG, "http://54.218.73.244:7006/"+jsonobject.getString("restaurantIMAGE"));

                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}

listview_main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >  

</GridView>

ログ::

09-30 18:39:40.420: D/AndroidRuntime(550): Shutting down VM
09-30 18:39:40.420: W/dalvikvm(550): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-30 18:39:40.439: E/AndroidRuntime(550): FATAL EXCEPTION: main
09-30 18:39:40.439: E/AndroidRuntime(550): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.jsonparsetutorial/com.androidbegin.jsonparsetutorial.MainActivity}: java.lang.ClassCastException: android.widget.GridView
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.os.Looper.loop(Looper.java:123)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 18:39:40.439: E/AndroidRuntime(550):  at java.lang.reflect.Method.invokeNative(Native Method)
09-30 18:39:40.439: E/AndroidRuntime(550):  at java.lang.reflect.Method.invoke(Method.java:507)
09-30 18:39:40.439: E/AndroidRuntime(550):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 18:39:40.439: E/AndroidRuntime(550):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 18:39:40.439: E/AndroidRuntime(550):  at dalvik.system.NativeStart.main(Native Method)
09-30 18:39:40.439: E/AndroidRuntime(550): Caused by: java.lang.ClassCastException: android.widget.GridView
09-30 18:39:40.439: E/AndroidRuntime(550):  at com.androidbegin.jsonparsetutorial.MainActivity.onCreate(MainActivity.java:34)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-30 18:39:40.439: E/AndroidRuntime(550):  ... 11 more

エラーを解決する方法 !

4

2 に答える 2