【问题标题】:simple Gridview to display images using image loader in android简单的 Gridview 在 android 中使用图像加载器显示图像
【发布时间】:2013-09-30 13:20:49
【问题描述】:

我正在使用图像加载器从远程 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

如何解决错误!

【问题讨论】:

  • MainActivity.java:34 你在做一些愚蠢的转换......这就是logcat日志中写的......你至少尝试过阅读它吗? @smriti2 你要发布每一个糟糕的书面代码并提出问题stackoverflow.com/questions/19089757/…
  • 多个 ppl 在同一个请求上工作...导致了! ..我没有看到发布的问题.. :)

标签: java android json gridview


【解决方案1】:

在主 Activity.java 类中 在 xml 文件中,您将其定义为活动中的网格,您以列表视图的形式检索视图,这就是问题所在

改变这一行

 listview = (ListView) findViewById(R.id.listview);

 GridView listview;

 listview = (GridView) findViewById(R.id.listview);

然后就可以了。

【讨论】:

    【解决方案2】:

    ListView 更改为 GridView ,在提问之前检查您的代码并查看 log cat 问题是什么

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多