【问题标题】:How to populate Grid View with images from PHP MySql in Android?如何在 Android 中使用来自 PHP MySql 的图像填充网格视图?
【发布时间】:2014-12-22 10:56:11
【问题描述】:

我想从 PHP MySql 数据库中检索所有图像并填充 GridView。我正在使用 JSON 解析。但是在我的网格视图中没有显示来自 PHP 的图像,也没有任何错误。那么为什么不在我的应用程序的 Gridview 中显示图像。代码有什么问题。感谢感谢。

这是我的适配器代码。

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewAdapter extends BaseAdapter
{
    private Context context;
    public ArrayList<HashMap<String, String>> mThumbIds;

    public GridViewAdapter (Context context,ArrayList<HashMap<String, String>> data )
    {
        this.context= context;
        mThumbIds= data;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position ;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(mThumbIds.get(position).get("image"));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;

    }
}

这是活动代码。

package com.photo_app;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;

public class Photo_Gallery extends Activity
{
    JSONObject jsonobject;
    JSONArray jsonarray;
    GridView gridview;
    GridViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    JSONParser jsonParser = new JSONParser();
    ProgressDialog pDialog;
    private String URL_PHOTO_GALLERY = "http://192.168.1.102/timesofindia/admin/photo_gallery.php";

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_gallery);
        new DownloadJSON().execute();
    }


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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(Photo_Gallery.this);
            mProgressDialog.setTitle("Wait");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) 
        {

            arraylist = new ArrayList<HashMap<String, String>>();
            jsonobject = JSONfunctions.getJSONfromURL(URL_PHOTO_GALLERY);
            System.out.println("Json String = " + jsonobject.toString());


            try
            {
                jsonarray = jsonobject.getJSONArray("photo_gallary");
                for (int i = 0; i < jsonarray.length(); i++)
                {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    map.put("flag", jsonobject.getString("image"));
                    arraylist.add(map);
                    Log.e("arraylist","=");
                }
            } 
            catch (JSONException e)
            {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {

            gridview = (GridView) findViewById(R.id.photoGallery);
            adapter = new GridViewAdapter();
            gridview.setAdapter(adapter);
            mProgressDialog.dismiss();
        }
    }
}

【问题讨论】:

  • 安居,你的图片在哪里?在 mThumbIds 数组或 arraylist 数组中???
  • 您是否为网格项设计了自定义行布局?
  • @BellaSwan 我认为问题在于获取图像及其数组,她采用了动态图像视图,所以不是问题。
  • @Pratik : - Arraylist 中的图像来自 Activity 中的 json 而不是 Adapter.Ho 中的 mThumbIds 来执行此操作。
  • @anjupatel 很简单,亲爱的。如果你不介意那么你能把你的代码发给我吗?让我用工作代码回复你。

标签: android phpmyadmin


【解决方案1】:

您需要将数据传递给适配器。因此,创建一个constructor 接受数据并在getView 中随意使用它。

将所需的数据从Activity传递给Adapter(可以是任何类型。我刚刚给出了传递Integer[]的例子)

public GridViewAdapter (Context context,ArrayList<HashMap<String, Integer>> data )
    {
        this.context= context;
        mThumbIds= data;
    }

并将其传递给您的活动:

GridViewAdapter adapter = new GridViewAdapter(this,your_data_array);

更新:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageView = new ImageView(context);
         imageView.setImageResource(mThumbIds.get(position).get("key")); // assuming key contains the resource id
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;

    }

【讨论】:

  • 她正在尝试从 url 加载图像,她需要将 url arraylist 传递给适配器。整数不会达到目的。
  • DataType 是什么...这只是一个示例..她需要根据自己的要求进行更改。我展示它是因为我在她的代码中找到了public Integer[] mThumbIds = {};..
  • @Bella Swan :- 我已经用您的代码进行了编辑,但问题出现在 Adapter 类中。表达式的类型必须是数组类型,但它解析为 ArrayList>。 imageView.setImageResource(mThumbIds[位置]);
  • @BellaSwan 你的回答很好,但我认为anju是新手,所以不会理解。
  • 是的..and 这不是喂汤的网站 .你还需要她学习如何code ..所以最好一步一步地向她展示而不是直接她的代码
【解决方案2】:

试试下面的代码:

package com.photo_app;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;

public class Photo_Gallery extends Activity
{
    JSONObject jsonobject;
    JSONArray jsonarray;
    GridView gridview;
    GridViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    JSONParser jsonParser = new JSONParser();
    ProgressDialog pDialog;
    private String URL_PHOTO_GALLERY = "http://192.168.1.102/timesofindia/admin/photo_gallery.php";

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_gallery);
        new DownloadJSON().execute();
    }


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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(Photo_Gallery.this);
            mProgressDialog.setTitle("Wait");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) 
        {

            arraylist = new ArrayList<HashMap<String, String>>();
            jsonobject = JSONfunctions.getJSONfromURL(URL_PHOTO_GALLERY);
            System.out.println("Json String = " + jsonobject.toString());


            try
            {
                jsonarray = jsonobject.getJSONArray("photo_gallary");
                for (int i = 0; i < jsonarray.length(); i++)
                {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    map.put("flag", jsonobject.getString("image"));
                    arraylist.add(map);

                    Log.e("arraylist","=" + arraylist.add(map) );
                }
            } 
            catch (JSONException e)
            {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {

            gridview = (GridView) findViewById(R.id.photoGallery);
            adapter = new GridViewAdapter(Photo_Gallery.this,arraylist);
            gridview.setAdapter(adapter);
            mProgressDialog.dismiss();
        }
    }



}

//适配器代码:

package com.photo_app;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.androidquery.AQuery;

public class GridViewAdapter extends BaseAdapter
{
    private Context context;
    public ArrayList<HashMap<String,String>> mThumbIds=new ArrayList<HashMap<String, String>>();
    AQuery aQuery;
    private LayoutInflater mInflater;

    public GridViewAdapter (Context context,ArrayList<HashMap<String,String>> data )
    {
        this.context= context;
        mThumbIds= data;
        aQuery=new AQuery(context);
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return  0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.photo_gallery_list_item, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        aQuery.id(holder.imageView).image(mThumbIds.get(position).get("flag"),true,true);

        return convertView;

    }

    class ViewHolder {

        ImageView imageView;
    }
}

//photo_gallery_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher"
        android:adjustViewBounds="true"/>

</LinearLayout>

【讨论】:

  • 现在你也采用了自定义的 ow 布局,问题不存在了 你通过向新手介绍新事物使代码变得复杂
  • @BellaSwan 一点也不,我给她一个学习好东西的机会,这样她以后就可以保重,走正路。这一点也不复杂。为什么你带我走错路,你能告诉我吗?
  • @BelaaWsan:- 我已经完成了从 php 服务器检索图像的一半工作,但问题是,我不明白如何在 GridView 中显示。就这样。
  • @Pratik:- 我已经运行了你的代码,但是得到了空指针异常。我不浪费你的时间好吗。您已经完成了完美的代码,那么为什么会出现空指针异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
相关资源
最近更新 更多