本文介绍了GridView 中的 JSON 数组到 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 json 数组在 gridview 中使用 picasso 填充 imageview 时遇到了一些问题.

Im having some problems with using a json array to populate an imageview using picasso within a gridview.

我想我已经解决了大部分问题,但是遇到了一个问题

I think i have most of it down but have however run into a but of a problem

我的计划是

a) 收集 json 数组b) 转换为哈希图c) 添加一个数组列表d) 将数组列表传递给 ImageAdaptore) 设置视图并添加正确的数据

a) collect the json arrayb) convert to a hashmapc) add it an array listd) pass arraylist to ImageAdaptore) set the view and add correct data

我目前被困在 d 上,出现以下错误

i am currently stuck on d with the following error

Error:(97, 41) error: constructor ImageAdapter in class ImageAdapter cannot be applied to given types;
required: Context,ArrayList<HashMap<String,String>>
found: MainActivity.JSONParse,ArrayList<HashMap<String,String>>
reason: actual argument MainActivity.JSONParse cannot be converted to Context by method invocation conversion

不知道我做错了什么,但它让我难住了,谷歌搜索也无济于事

not sure what i have got wrong but its got me stumped, and no amount of googling is helping

主要活动

    package com.example.alex.jsonparsegridview;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

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

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

public class MainActivity extends Activity {

    GridView gridView;

    static final String[] MOBILE_OS = new String[]{
            "Android", "iOS", "Windows", "Blackberry"};

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



    }


    private class JSONParse extends AsyncTask<String, String, JSONArray> {
        private ProgressDialog pDialog;

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

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override
        protected JSONArray doInBackground(String... args) {

            JSONParser jParser = new JSONParser();

            // Getting JSON from URL

            String url = "http://www.500kgiveaway.co.uk/phpscript.php";
            JSONArray json = jParser.getJSONFromUrl(url);
            return json;
        }

        @Override
        protected void onPostExecute(JSONArray json) {
            pDialog.dismiss();
            try {
                // Getting JSON Array from URL

                JSONArray android = json;
                String TAG_PATH = "path";
                ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

                for (int i = 0; i < android.length(); i++) {

                    JSONObject c = android.getJSONObject(i);

                    // Storing  JSON item in a Variable
                    String path = c.getString(TAG_PATH);

                    // Adding value HashMap key => value

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_PATH, path);

                    oslist.add(map);


                 //copied from onCreate
                    gridView = (GridView) findViewById(R.id.gridView1);

                    gridView.setAdapter(new ImageAdapter(this, oslist));

                    gridView.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> parent, View v,
                                                int position, long id) {
                            Toast.makeText(
                                    getApplicationContext(),
                                    ((TextView) v.findViewById(R.id.grid_item_label))
                                            .getText(), Toast.LENGTH_SHORT).show();

                        }
                    });

                 //End Copy


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

        }
    }
}

图像适配器

package com.example.alex.jsonparsegridview;

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

import com.squareup.picasso.Picasso;

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


public class ImageAdapter extends BaseAdapter {
    private Context context;
    ArrayList<HashMap<String, String>> dataList;

    public ImageAdapter(Context context, ArrayList<HashMap<String, String>> oslist) {
        this.context = context;
        this.dataList = oslist;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);

            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(((Map)getItem(position)).get("path").toString());

            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);


            Picasso.with(context).load(((Map) getItem(position)).get("path").toString()).into(imageView);

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}

提前致谢

推荐答案

问题是当您提供对 MainActivity.JSONParse 的引用而不是来自的 MainActivity 引用时这里:

The problem is when you give the reference to the MainActivity.JSONParse instead of the MainActivity reference from here:

gridView.setAdapter(new ImageAdapter(this, oslist));

this 实际上会给你 MainActivity.JSONParse 因为你是从 MainActivity 的内部类调用它;要获得您需要调用的活动的参考:MainActivity.this

the this will actually give you MainActivity.JSONParse because you are calling it from an inner class of your MainActivity; To get the reference of you activity you need to call: MainActivity.this

gridView.setAdapter(new ImageAdapter(MainActivity.this, oslist));

这篇关于GridView 中的 JSON 数组到 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:02