将凌空图像传递给下一个活动

时间:2015-01-07 15:08:05

标签: android json android-volley

现在一直困在这个小问题上。这很简单,但让我感到困惑。我的第一个活动作为列表视图使用凌空下载了json数据,点击进入详细信息,如下所示 enter image description here

我可以将标题发送到详细信息,但无法解决传递图像的问题。 这是我的第一个活动:

public class Movies extends ActionBarActivity{
    // Log tag
    private static final String TAG = MainActivity.class.getSimpleName();

    // Movies json url
    private static final String url = "http://api.androidhive.info/json/movies.json";
    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;
    private CustomListAdapter adapter;
    private static String Title="title";
    private static String bitmap="thumbnailUrl";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event);

     getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent newActivity2=new Intent();
    setResult(RESULT_OK, newActivity2);

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, movieList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#1b1b1b")));

    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    pDialog.dismiss();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("title"));
                            movie.setThumbnailUrl(obj.getString("image"));
                            movie.setRating(((Number) obj.get("rating"))
                                    .doubleValue());
                            movie.setYear(obj.getInt("releaseYear"));

                            // Genre is json array
                            JSONArray genreArry = obj.getJSONArray("genre");
                            ArrayList<String> genre = new ArrayList<String>();
                            for (int j = 0; j < genreArry.length(); j++) {
                                genre.add((String) genreArry.get(j));
                            }
                            movie.setGenre(genre);

                            // adding movie to movies array
                            movieList.add(movie);

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

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (error instanceof NoConnectionError){
                    Toast.makeText(getBaseContext(), "Bummer..There's No Internet connection!", Toast.LENGTH_LONG).show();

                }};
            });

    // Adding request to request queue
ParseApplication.getInstance().addToRequestQueue(movieReq);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) { 
            String name = ((TextView) view.findViewById(R.id.title))
                    .getText().toString();

            Intent intent = new Intent(Movies.this, Detail.class);    
            intent.putExtra(Title, name);
            intent.putExtra("images", bitmap);

            startActivity(intent);
        }
        });}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



    }

详情活动

public class Detail extends ActionBarActivity{
    private static String Title="title";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);
     getSupportActionBar().hide();

    Intent i=getIntent();
    String name = i.getStringExtra(Title);
    TextView lblName = (TextView) findViewById(R.id.name_label);

    lblName.setText(name);}


    public void onClickHandler(View v){
            switch(v.getId()){
            case R.id.imageview1:
            startActivity(new Intent(this,Movies.class));
        }

}}

如果需要,我会发布更多代码。我是这个东西的新手所以任何帮助将不胜感激。

CustomListAdapter:

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomListAdapter(Activity activity, List<Movie> movieItems) {
    this.activity = activity;
    this.movieItems = movieItems;
}

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

@Override
public Object getItem(int location) {
    return movieItems.get(location);
}

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

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

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
    TextView title = (TextView) convertView.findViewById(R.id.title);
    TextView rating = (TextView) convertView.findViewById(R.id.rating);
    TextView genre = (TextView) convertView.findViewById(R.id.genre);
    TextView year = (TextView) convertView.findViewById(R.id.releaseYear);

    // getting movie data for the row
    Movie m = movieItems.get(position);

    // thumbnail image
    thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

    // title
    title.setText(m.getTitle());

    // rating
    rating.setText("Rating: " + String.valueOf(m.getRating()));

    // genre
    String genreStr = "";
    for (String str : m.getGenre()) {
        genreStr += str + ", ";
    }
    genreStr = genreStr.length() > 0 ? genreStr.substring(0,
            genreStr.length() - 2) : genreStr;
    genre.setText(genreStr);

    // release year
    year.setText(String.valueOf(m.getYear()));

    return convertView;
}

}

5 个答案:

答案 0 :(得分:1)

您没有在详细信息活动中检索位图:

Bitmap bitmap = (Bitmap) i.getParcelableExtra("images");

然后用该位图做任何你需要的事情

答案 1 :(得分:1)

您需要在详细信息页面中检索缩略图网址,如下所示

String bitmap = i.getStringExtra("images"); // Use this in details page

现在您可以使用此bitmap在您的详细信息页面中加载图片。

修改

onItemClick()

中的Movies中使用此功能
bitmap = ((Movie)movieList.get(position)).getThumbnailUrl();
Intent intent = new Intent(Movies.this, Detail.class);    
intent.putExtra(Title, name);
intent.putExtra("images", bitmap);

Detail活动onCreate()

中添加此内容
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
String bitmap = i.getStringExtra("images");
thumbNail.setImageUrl(bitmap, imageLoader);

答案 2 :(得分:0)

我有完全相同的代码,但我在片段中使用。有人讲述了如何将JSON值从片段传递给activity。我想Lastest_fragment.java到News_Detail.java(新活动)

Lastest_Fragment.java

public class LatestFragment extends Fragment {

    private static final String TAG = LatestFragment.class.getSimpleName();

    // Movies json url
    private static final String url = "http://api.androidhive.info/json/movies.json";
    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;
    private CustomListAdapter adapter;
    private static String Title="title";
    private static String bitmap="thumbnailUrl";


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


        View v = inflater.inflate(R.layout.latest_layout, container, false);

        Intent intent2 = new Intent();
       getActivity().setResult(Activity.RESULT_OK, intent2);
       // getActivity().finish();

        listView = (ListView) v.findViewById(R.id.list);
        adapter = new CustomListAdapter(getActivity(), movieList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(getActivity());
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        // changing action bar color
       // getActivity().getActionBar().setBackgroundDrawable( new ColorDrawable(Color.parseColor("#1b1b1b")));

        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.setTitle(obj.getString("title"));
                                movie.setThumbnailUrl(obj.getString("image"));
                                movie.setRating(((Number) obj.get("rating"))
                                        .doubleValue());
                                movie.setYear(obj.getInt("releaseYear"));

                                // Genre is json array
                                JSONArray genreArry = obj.getJSONArray("genre");
                                ArrayList<String> genre = new ArrayList<String>();
                                for (int j = 0; j < genreArry.length(); j++) {
                                    genre.add((String) genreArry.get(j));
                                }
                                movie.setGenre(genre);

                                // adding movie to movies array
                                movieList.add(movie);

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

                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error instanceof NoConnectionError){
                    Toast.makeText(getActivity().getBaseContext(), "Bummer..There's No Internet connection!", Toast.LENGTH_LONG).show();

                }

            }
        });

        // Adding request to request queue
       AppController.getInstance().addToRequestQueue(movieReq);
        //ParseApplication.getInstance().addToRequestQueue(movieReq);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
                public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                    // TODO Auto-generated method stub
                    String name = ((TextView) view.findViewById(R.id.title)).getText().toString();
                    bitmap = ((Movie)movieList.get(position)).getThumbnailUrl();
                    Intent intent = new Intent(getActivity(), News_Detail.class);
                    intent.putExtra(Title, name);
                    intent.putExtra("images", bitmap);

                   startActivity(intent);
                }
            });
        return v;

        }


News_detail.java

public class News_Detail extends AppCompatActivity {

    Intent intent = getIntent();
    String name = intent.getStringExtra("name");

    private static String Title="title";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_detail);
        getSupportActionBar().hide();

        Intent i = getIntent();
        ImageLoader imageLoader = AppController.getInstance().getImageLoader();
        String name = i.getStringExtra(Title);
        String bitmap = i.getStringExtra("images");
        NetworkImageView thumbNail = (NetworkImageView) findViewById(R.id.thumbnail);
        thumbNail.setImageUrl(bitmap, imageLoader);
        TextView lblname = (TextView) findViewById(R.id.name_label);

        lblname.setText(name);
    }

    public void onClickHandler(View v){
        switch(v.getId()){
            case R.id.thumbnail:startActivity(new Intent(this,LatestFragment.class));
        }
}
}

答案 3 :(得分:0)

首先在片段活动中输入该代码......

Intent intent = new Intent(view.getContext(), ItemContents.class);

intent.putExtra("Position", position);

ShopsModel item = (ShopsModel) adapter.getItem(position);
intent.putExtra("logo", item.getThumbnailUrl());
startActivity(intent);

答案 4 :(得分:0)

现在将该代码粘贴到您要显示文本和图像的下一个活动

NetworkImageView img = (NetworkImageView) findViewById(R.id.tv_ic_thumbnail);
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
String bitmap = intent.getStringExtra("logo");
img.setImageUrl(bitmap, imageLoader);