java - ListView 中的 Json 解析

标签 java android json android-activity android-fragments

我是初学者。 我尝试在从 json 解析数据的 fragment 中实现 listview,但遇到错误

错误是。

Error:(119, 22) error: no suitable constructor found for   ProgressDialog(My_Actor)
constructor ProgressDialog.ProgressDialog(Context,int) is not applicable
(actual and formal argument lists differ in length)
constructor ProgressDialog.ProgressDialog(Context) is not applicable
(actual argument My_Actor cannot be converted to Context by method invocation conversion)

就我的水平而言,我没有发现任何错误。请帮帮我。

MainActivity.java

 @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments

        Fragment newFragment = null;

        switch (position){

            case 0:
                newFragment = new My_Actors();
                break;
            case 1:
                newFragment = new My_Editors();
                break;
        }
        if(newFragment!=null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, newFragment)
                    .commit();
        }
    }

My_Actors.java

public class My_Actors extends Fragment{
 ArrayList<Actors> actorsList;

            ActorAdapter adapter;


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        actorsList = new ArrayList<Actors>();
        new JSONAsyncTask().execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");
        View v = inflater.inflate(R.layout.deal_featured, container,false);
        ListView listview = (ListView)v.findViewById(R.id.listView1);
        adapter = new ActorAdapter(getActivity().getBaseContext(), R.layout.listview_layout, actorsList);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity().getBaseContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();
            }
        });
        return v;
    }

    class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(Deal_featured.this);
            dialog.setMessage("Loading, please wait");
            dialog.setTitle("Connecting server");
            dialog.show();
            dialog.setCancelable(false);
        }

        @Override
        protected Boolean doInBackground(String... urls) {
            try {

                //------------------>>
                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);


                    JSONObject jsono = new JSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("actors");

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object = jarray.getJSONObject(i);

                        Actors actor = new Actors();

                        actor.setName(object.getString("name"));
                        actor.setDescription(object.getString("description"));
                        actor.setDob(object.getString("dob"));
                        actor.setCountry(object.getString("country"));
                        actor.setHeight(object.getString("height"));
                        actor.setSpouse(object.getString("spouse"));
                        actor.setChildren(object.getString("children"));
                        actor.setImage(object.getString("image"));

                        actorsList.add(actor);
                    }
                    return true;
                }

                //------------------>>

            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return false;
        }

        protected void onPostExecute(Boolean result) {
            dialog.cancel();
            adapter.notifyDataSetChanged();
            if(result == false)
                Toast.makeText(getActivity().getBaseContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

        }
    }
}

ActorAdapter.java

public class ActorAdapter extends ArrayAdapter<Actors> {
    ArrayList<Actors> actorList;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;

    public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
        super(context, resource, objects);
        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        actorList = objects;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convert view = design
        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);
            holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
            holder.tvName = (TextView) v.findViewById(R.id.tvName);
            holder.tvDescription = (TextView) v.findViewById(R.id.tvDescriptionn);
            holder.tvDOB = (TextView) v.findViewById(R.id.tvDateOfBirth);
            holder.tvCountry = (TextView) v.findViewById(R.id.tvCountry);
            holder.tvHeight = (TextView) v.findViewById(R.id.tvHeight);
            holder.tvSpouse = (TextView) v.findViewById(R.id.tvSpouse);
            holder.tvChildren = (TextView) v.findViewById(R.id.tvChildren);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.imageview.setImageResource(R.drawable.ic_launcher);
        new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
        holder.tvName.setText(actorList.get(position).getName());
        holder.tvDescription.setText(actorList.get(position).getDescription());
        holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
        holder.tvCountry.setText(actorList.get(position).getCountry());
        holder.tvHeight.setText("Height: " + actorList.get(position).getHeight());
        holder.tvSpouse.setText("Spouse: " + actorList.get(position).getSpouse());
        holder.tvChildren.setText("Children: " + actorList.get(position).getChildren());
        return v;

    }

    static class ViewHolder {
        public ImageView imageview;
        public TextView tvName;
        public TextView tvDescription;
        public TextView tvDOB;
        public TextView tvCountry;
        public TextView tvHeight;
        public TextView tvSpouse;
        public TextView tvChildren;

    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }

    }
}

最佳答案

Error:(119, 22) error: no suitable constructor found for
ProgressDialog(My_Actor) constructor ProgressDialog.ProgressDialog(Context,int) is not applicable (actual and formal argument lists differ in length) constructor ProgressDialog.ProgressDialog(Context) is not applicable (actual argument My_Actor cannot be converted to Context by method invocation conversion)

很明显,您必须传递上下文,而Deal_featured.this不是上下文。所以而不是

dialog = new ProgressDialog(Deal_featured.this);

你必须使用

dialog = new ProgressDialog(getActivity());

getActivity() 返回此 fragment 的父 Activity 的上下文。

关于java - ListView 中的 Json 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27979843/

相关文章:

java - JMockit Deencapsulation setField 不持久

Java - 从jar目录中读取文件

jquery - 将 JSON 数据发送到 WebMethod

java - 将 HashMap 数据保存到 SQLite

javascript - 将 JSON 数据加载到 Angular-nvD3 Graph (AngularJS)

java - 对 Java HashMap createEntry 方法的混淆

java - Oracle:命名的 java 过程,由模式名称限定范围?

java - bigdecimal 除以整数

java - 输出其中有所需文件的文件夹

android - Google Books API - 不断收到错误代码 "403"原因 : "ipRefererBlocked"