【问题标题】:Set Image In an Image View Using JSON Parsing in Android在 Android 中使用 JSON 解析在图像视图中设置图像
【发布时间】:2015-12-21 20:48:27
【问题描述】:

我解析了列表视图中的所有文本视图和图像,但是当我尝试单击单个列表视图时,它确实显示文本视图但不显示图像视图 bdw 我不知道如何获取它。

MainActivity.java`

public class MainActivity extends Activity {

static ArrayList<Actors> actorsList;

ActorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actorsList = new ArrayList<Actors>();
    //final ArrayList<Actors> movieList = new ArrayList<Actors>();
    new JSONAsyncTask().execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");

    ListView listview = (ListView)findViewById(R.id.list);
    adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);

    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new OnItemClickListener() {


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

            Actors m = actorsList.get(position);


            Intent i = new Intent(MainActivity.this, SingleView.class);
            i.putExtra("position", String.valueOf(position));
            i.putExtra("thumb", m.getImage());
            i.putExtra("name", m.getName());
            i.putExtra("dob", m.getDob());
            i.putExtra("height", m.getHeight());
            i.putExtra("country", m.getCountry());
    //      View imageView = null;
            //imageView.buildDrawingCache();

        //  Bitmap image= imageView.getDrawingCache();

            // Bundle extras = new Bundle();
        //  extras.putParcelable("imagebitmap", image);
        //  i.putExtras(extras);

            startActivity(i);
        }
    });
}


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

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.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(getApplicationContext(), "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;
private static ActorAdapter mInstance;

public void onCreate() {
    mInstance = this;
}


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;

}

 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);
    }


}

public static ActorAdapter getInstance() {
    // TODO Auto-generated method stub
    return mInstance;
}

SingleView.Java

public class SingleView extends Activity{
TextView name,dob,height,country;
Button back,prev,nxt;
ImageView img ;

//imageLoader = ActorAdapter.getInstance().getImage();

private ArrayList<Actors> movieList_detail = new ArrayList<Actors>();

int selected = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleview);    

    Intent i = getIntent();

    final String image1 = i.getStringExtra("thumb");
    String name1 = i.getStringExtra("name");
    String dob1 = i.getStringExtra("dob");
    String height1 = i.getStringExtra("height");
    String country1 = i.getStringExtra("country");

    img = (ImageView)findViewById(R.id.ivImage);
    name = (TextView)findViewById(R.id.tvName);
    dob = (TextView)findViewById(R.id.tvDateOfBirth);
    height = (TextView)findViewById(R.id.tvHeight);
    country = (TextView)findViewById(R.id.tvCountry);
    back = (Button)findViewById(R.id.button1);
    prev = (Button)findViewById(R.id.button2);
    nxt = (Button)findViewById(R.id.button3);

    movieList_detail = MainActivity.actorsList;


    name.setText(name1);
    dob.setText(dob1);
    height.setText(height1);
    country.setText(country1);



    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    prev.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(selected>0){

                selected--;

                Actors m = movieList_detail.get(selected);
                //String image  = m.getThumbnailUrl();
                name.setText(m.getName());
                dob.setText(m.getDob());
                height.setText(m.getHeight());
                country.setText(m.getCountry());
            //  img.DisplayImage(m.getImage(), image1);


                nxt.setVisibility(View.VISIBLE);
            }else
            {
                prev.setVisibility(View.GONE);
            }
        }
    });

        nxt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub

                if(selected<5){

                    selected++;

                    Actors m = movieList_detail.get(selected);
                    //String image  = m.getThumbnailUrl();
                    name.setText(m.getName());
                    dob.setText(m.getDob());
                    height.setText(m.getHeight());
                    country.setText(m.getCountry());
                //  imageLoader.DisplayImage(m.getImage(), img);

            prev.setVisibility(View.VISIBLE);
            }else
            {
                nxt.setVisibility(View.GONE);
            }
        }
    });


}

}

Actors.java

public class Actors {
private String name;
private String description;
private String dob;
private String country;
private String height;
private String spouse;
private String children;
private String image;

public Actors() {
    // TODO Auto-generated constructor stub
}

public Actors(String name, String description, String dob, String country,
        String height, String spouse, String children, String image) {
    super();
    this.name = name;
    this.description = description;
    this.dob = dob;
    this.country = country;
    this.height = height;
    this.spouse = spouse;
    this.children = children;
    this.image = image;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getHeight() {
    return height;
}

public void setHeight(String height) {
    this.height = height;
}

public String getSpouse() {
    return spouse;
}

public void setSpouse(String spouse) {
    this.spouse = spouse;
}

public String getChildren() {
    return children;
}

public void setChildren(String children) {
    this.children = children;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

}

【问题讨论】:

  • 您的问题不清楚,我看到您的 MainActivity 在列出您在 ActorAdapter 中加载图像的位置。但是在您的单视图活动中,您没有加载图像。那么它将如何显示图像???如果你愿意,你可以使用 Glide 或 Picasso 图片加载库。
  • 亲爱的@Sudhanshu,您将图片网址放在 SingleViewActivity 的意图中,您需要运行图片下载器任务以在 SingleViewActivity 的图片视图上设置图片。
  • 这就是我要问 Neeraj 如何在 singleview.java 类中设置图像,我通过意图并从 MainActivity 获取它并设置所有文本视图但无法设置图像视图找不到适当的解决方案。

标签: android json imageurl


【解决方案1】:

试试这个

public class SingleView extends Activity{
TextView name,dob,height,country;
Button back,prev,nxt;
ImageView img ;

//imageLoader = ActorAdapter.getInstance().getImage();

private ArrayList<Actors> movieList_detail = new ArrayList<Actors>();

int selected = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleview);    

    Intent i = getIntent();

    final String image1 = i.getStringExtra("thumb");
    String name1 = i.getStringExtra("name");
    String dob1 = i.getStringExtra("dob");
    String height1 = i.getStringExtra("height");
    String country1 = i.getStringExtra("country");

    img = (ImageView)findViewById(R.id.ivImage);
    name = (TextView)findViewById(R.id.tvName);
    dob = (TextView)findViewById(R.id.tvDateOfBirth);
    height = (TextView)findViewById(R.id.tvHeight);
    country = (TextView)findViewById(R.id.tvCountry);
    back = (Button)findViewById(R.id.button1);
    prev = (Button)findViewById(R.id.button2);
    nxt = (Button)findViewById(R.id.button3);

    movieList_detail = MainActivity.actorsList;


    name.setText(name1);
    dob.setText(dob1);
    height.setText(height1);
    country.setText(country1);

new DownloadImageTask(img).execute(image1);


    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    prev.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(selected>0){

                selected--;

                Actors m = movieList_detail.get(selected);
                //String image  = m.getThumbnailUrl();
                name.setText(m.getName());
                dob.setText(m.getDob());
                height.setText(m.getHeight());
                country.setText(m.getCountry());
            //  img.DisplayImage(m.getImage(), image1);


                nxt.setVisibility(View.VISIBLE);
            }else
            {
                prev.setVisibility(View.GONE);
            }
        }
    });

        nxt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub

                if(selected<5){

                    selected++;

                    Actors m = movieList_detail.get(selected);
                    //String image  = m.getThumbnailUrl();
                    name.setText(m.getName());
                    dob.setText(m.getDob());
                    height.setText(m.getHeight());
                    country.setText(m.getCountry());
                //  imageLoader.DisplayImage(m.getImage(), img);

            prev.setVisibility(View.VISIBLE);
            }else
            {
                nxt.setVisibility(View.GONE);
            }
        }
    });


}

【讨论】:

  • 我刚刚使用它(new DownloadImageTask(img).execute(image1);) 并在“DownloadImageTask”上出现错误,以创建一个名为“DownloadImageTask”的类..:(.跨度>
  • 您需要单独创建 DownloadImageTask 以便您可以在 SingleViewActivity 和 ActorAdapter 或其他虎钳中使用您可以从 ActorAdapter 复制粘贴到 SingleViewActivity 中的 DownloadImageTask 类
  • 完成老板谢谢老板:) 现在我可以在 imageview 中看到图像,但还有一件事请帮助我在单击下一个和上一个按钮后显示图像。当我点击一个特定的列表视图时,我可以看到图像,但是在我点击下一个按钮后,图像保持不变,请给我投票
  • Yurekkkaaaaa:).. 老板非常感谢,我只是将相同的代码放在上一个和下一个按钮中并获得图像。我想投票给你的答案,但我没有声誉所以不能对不起..
【解决方案2】:

我假设图像在 SingleView Activity 中不可见。

img = (ImageView)findViewById(R.id.ivImage);

接下来你要设置值

    name.setText(name1);
dob.setText(dob1);
height.setText(height1);
country.setText(country1);

您错过了在 img 中加载 imgae。 看看 Picasso 的图片加载库

【讨论】:

  • 感谢 vab 的回答,但我没有错过它,我真的不知道如何在其中设置图像,因为看到显示文本我们必须像这样 setText 来显示图像我们必须做的事情和我怎么不知道,如果你知道的话请告诉我?
  • 要设置图像,您需要图像。您可以保存第一次下载的图像路径,再次下载或使用 Lru 缓存进行缓存。
  • 感谢vab的建议,让我试试,做完后告诉你。
猜你喜欢
  • 1970-01-01
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
相关资源
最近更新 更多