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

问题描述

林具有使用JSON数组来填充用毕加索的GridView内的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)转换为一个HashMap
C)添加一个数组列表
D)通过数组列表来ImageAdaptor
E)设置视图,并添加正确的数据。

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 而不是<$ C $从这里C> 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));

这个将真正给你 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(新ImageAdapter(MainActivity.this,oslist));

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

10-20 09:24