简体   繁体   中英

How to spend a remote image of a listview to another layout , to just click on that item ? … In Android Studio

I made a listview of my server that sends images to a smartphone and the code I have works up there, the result is this :

在此处输入图片说明

Now I want to do that when you click on an item in the list, see the complete image in another layout and only get this by using intents.

I do not understand how to pass parameters in the onItemClick method. Please if someone tells me the solution. The code is this :

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ProgressDialog progressDialog;
    ArrayList asuntos=new ArrayList();
    ArrayList imagen=new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolBar);
        listView=(ListView) findViewById(R.id.list);
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        lista o=new lista();
        o.obtenerAvisos();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this, Visor.class);
                startActivity(intent);
            }
        });
    }

    public class lista {

        public void obtenerAvisos() {

            asuntos.clear();
            imagen.clear();

            String tag_string_req = "req_data";

            progressDialog.setMessage("Conectando...");
            showDialog();

            StringRequest strReq = new StringRequest(Request.Method.POST, AppURLs.URL, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    hideDialog();

                    try {

                        JSONArray jsonArray = new JSONArray(response);

                        for (int i = 0; i < jsonArray.length(); i++) {
                            asuntos.add(jsonArray.getJSONObject(i).getString("asunto"));
                            imagen.add(jsonArray.getJSONObject(i).getString("publicacion"));
                        }

                        listView.setAdapter(new ImagenAdaptador(getApplicationContext()));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                    hideDialog();
                }
            }) {

                @Override
                protected Map<String, String> getParams() {

                    Map<String, String> params = new HashMap<String, String>();
                    params.put("tag", "data");
                    return params;
                }

            };
            AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
        }
    }

    private void showDialog() {

        if (!progressDialog.isShowing())
            progressDialog.show();
    }

    private void hideDialog() {

        if (progressDialog.isShowing())
            progressDialog.dismiss();
    }

    public class ImagenAdaptador extends BaseAdapter {

        Context ctx;
        LayoutInflater layoutInflater;
        SmartImageView smartImageView;
        TextView tvasunto;

        public ImagenAdaptador(Context applicationContext) {

            this.ctx=applicationContext;
            layoutInflater=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
        }

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

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

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

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

            ViewGroup viewGroup=(ViewGroup) layoutInflater.inflate(R.layout.activity_main_items,null);
            smartImageView=(SmartImageView)viewGroup.findViewById(R.id.imagen1);
            tvasunto=(TextView) viewGroup.findViewById(R.id.tvAsunto);
            String urlfinal="http://192.168.43.45/xxxx/xxxxx/"+imagen.get(position).toString();
            Rect rect=new Rect(smartImageView.getLeft(), smartImageView.getTop(), smartImageView.getRight(), smartImageView.getBottom());
            smartImageView.setImageUrl(urlfinal, rect);
            tvasunto.setText(asuntos.get(position).toString());
            return viewGroup;
        }
    }
}

Note: Use these libraries:
com.mcxiaoke.volley:library:1.0.19
and
com.github.snowdream.android:smartimageview:0.0.2

In this case you don't need to , you already have access to "imagen arrayList" :

public static final string ARG_IMAGE_URL = "URL";

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(MainActivity.this, Visor.class);
            intent.putExtras(ARG_IMAGE_URL, imagen.get(position))
            startActivity(intent);
        }
    });

But you will have to think of a strategy to avoid reloading the image from the network that would be a waste of resources.

Okay thanks so are the lines . Although this method had to move to the getView ImagenAdaptador class method :

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

        ViewGroup viewGroup=(ViewGroup) layoutInflater.inflate(R.layout.activity_main_items,null);
        smartImageView=(SmartImageView)viewGroup.findViewById(R.id.imagen1);
        tvasunto=(TextView) viewGroup.findViewById(R.id.tvAsunto);
        final String urlfinal="http://192.168.43.45/InfoTec/publicaciones/"+imagen.get(position).toString();
        Rect rect=new Rect(smartImageView.getLeft(), smartImageView.getTop(), smartImageView.getRight(), smartImageView.getBottom());
        smartImageView.setImageUrl(urlfinal, rect);
        tvasunto.setText(asuntos.get(position).toString());

        **listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, Visor.class);
                intent.putExtra("arg", urlfinal);
                startActivity(intent);
            }
        });**
        return viewGroup;
    }

For taking the ArrayList where they were not sent anything, the image variable is processed in this method of ImagenAdaptador class. Everything seemed fine only now visualize the last picture but click on any of the others, always sends the photo of Heineken. What happens now?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM