Siddharth
Siddharth

Reputation: 181

How to send to websites on click listview in android?

I am getting list of data from json in which a parameter named as click_url is also there I fetch data in ListView .what I want when on click ListView I want to go to website that click_url contain .how can I do that.

jsons response:-

{
    "offers": [{
        "offer_id": 97245,
        "name": "Earn Talktime & Data Android App",
        "description": "Download and install",
        "requirements": null,
        "credit_delay": "0",
        "featured_global": 0,
        "epc": "0.00",
        "conversion_rate": "0.016",
        "testing_status": 0,
        "testing_time": "0000-00-00 00:00:00",
        "creative_id": 164789,
        "creative_filename": "97245-164789.gif",
        "creative_url": "https:\/\/asmclk.com\/creat\/97245-164789.gif",
        "payout": 0.14,
        "payout_custom": 0,
        "stats_pending_ce": 0,
        "currency_count": 70,
        "target_system": 40,
        "featured_profile": 0,
        "click_url": "https:\/\/asmclk.com\/click.php?aff=105639&camp=97245&from=6453&prod=4&sub1=9555517491&prod_channel=1&device=fb772712-deff-4cc6-9365-41451ed33976&xt=Cb0xo807sNVx8ARZai%2B9dbKYSSBS2XZ23KjB3UGchmL3f8zjm8TT4okSW1ypbTqJ%3A6Jncp2Gx4KZjhM3JqeDoKQ%3D%3D",
        "image_url": "\/\/adscendmedia.com\/creat\/97245-164789.gif",
        "category_id": [17, 18],
        "matches_target_system_detected": true,
        "mobile_app": {
            "store_id": "info.earntalktime",
            "platform": 1
        }
    }, {
        "offer_id": 107027,
        "name": "Speak Up - Share Your Thoughts",
        "description": "Take part in a survey and get rewarded",
        "requirements": null,
        "credit_delay": "0",
        "featured_global": 0,
        "epc": "0.00",
        "conversion_rate": "0.006",
        "testing_status": 0,
        "testing_time": "0000-00-00 00:00:00",
        "creative_id": 176235,
        "creative_filename": "106989-176199.jpg",
        "creative_url": "https:\/\/asmclk.com\/creat\/106989-176199.jpg",
        "payout": 0.14,
        "payout_custom": 0,
        "stats_pending_ce": 0,
        "currency_count": 70,
        "target_system": 0,
        "featured_profile": 0,
        "click_url": "https:\/\/asmclk.com\/click.php?aff=105639&camp=107027&from=6453&prod=4&sub1=9555517491&prod_channel=1&device=fb772712-deff-4cc6-9365-41451ed33976&xt=udTdOoT4NSeWh53J%2FJaAf8UGzlJtpd9ZqLvy3TrPf53fPSmCqhaQpWu35HmDYP4V%3Apgx2an3HDsf7Za5dwjSA2A%3D%3D",
        "image_url": "\/\/adscendmedia.com\/creat\/106989-176199.jpg",
        "category_id": [20],
        "matches_target_system_detected": true
    }, {
        "offer_id": 136497,
        "name": "Pockets By ICICI Bank Android App",
        "description": "Install and Launch",
        "requirements": null,
        "credit_delay": "0",
        "featured_global": 0,
        "epc": "0.00",
        "conversion_rate": "0.021",
        "testing_status": 0,
        "testing_time": "0000-00-00 00:00:00",
        "creative_id": 207101,
        "creative_filename": "136497-207101.png",
        "creative_url": "https:\/\/asmclk.com\/creat\/136497-207101.png",
        "payout": 0.14,
        "payout_custom": 0,
        "stats_pending_ce": 0,
        "currency_count": 70,
        "target_system": 40,
        "featured_profile": 0,
        "click_url": "https:\/\/asmclk.com\/click.php?aff=105639&camp=136497&from=6453&prod=4&sub1=9555517491&prod_channel=1&device=fb772712-deff-4cc6-9365-41451ed33976&xt=TFkQXE6w185fT4sagQsrrkcdTd5LJrFe9K2pGZgJ3reXPR0MSVpvsMrjbcd9oShQ%3AaFy%2BGFW2OkHdvEvYmcIfsw%3D%3D",
        "image_url": "\/\/adscendmedia.com\/creat\/136497-207101.png",
        "category_id": [17, 18],
        "matches_target_system_detected": true,
        "mobile_app": {
            "store_id": "com.icicibank.pockets",
            "platform": 1
        }
    }]
}

listView Code:-

public void getAdscendDeal() {
    String url = "http://ads.com/adwall/api/publisher/" + pubId + "/profile/" + aswallId + "/offers.json?subid1=" + m_szMobileNumber;

    JSONObject jsonObject = new JSONObject();
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, jsonObject, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.e(TAG, "Server Response::" + response);
            try {
                JSONArray post = response.optJSONArray("offers");
                for (int i = 0; i < post.length(); i++) {

                    JSONObject obj = post.getJSONObject(i);
                    m_Item = new CAdscenMediaDealStorage();
                    m_Item.setM_szHeaderText(obj.getString("name"));
                    m_Item.setM_szsubHeaderText(obj.getString("description"));
                    m_Item.setM_szDealValue(obj.getString("currency_count"));
                    m_Item.setM_szImageView(obj.getString("creative_url"));
                    m_Item.setM_Link(obj.getString("click_url"));
                    s_oDataset.add(m_Item);

                }
                if (!s_oDataset.isEmpty()) {
                    m_oAdapter = new CADscendDealAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
                    m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
                }


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

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Server Error::" + error);
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(jsonObjectRequest);
}

ListView Adapter

private class CADscendDealAdapter extends ArrayAdapter {

    private final Context m_Context;// declaring context variable
    private final ArrayList<CAdscenMediaDealStorage> s_oDataset;// declaring array list ariable

    public CADscendDealAdapter(Context m_Context, ArrayList<CAdscenMediaDealStorage> mDataList) {
        this.m_Context = m_Context;
        s_oDataset = mDataList;
    }

    @Override
    public int getCount() {// get total arraylist size
        return s_oDataset.size();
    }

    @Override
    public Object getItem(int position) {// get item position in array list
        return s_oDataset.get(position);
    }

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

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(m_Context, R.layout.sooper_sonic, null);
            viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
            viewHolder.m_Subheader = (TextView) convertView.findViewById(R.id.subHeaderText);
            viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
            viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.m_getBtn.setOnClickListener(new View.OnClickListener() {// onclick getDeal Btn
            @Override
            public void onClick(View v) {//send to deal detail page onclick getDeal Btn
                Intent i = new Intent(v.getContext(), CDealAppListingDetails.class);
                i.putExtra("DealCode", s_oDataset.get(position).getM_szsubHeaderText());// get deal code from deal data storage
                i.putExtra("headerText", s_oDataset.get(position).getM_szHeaderText());// get deal name from deal dta storage
                v.getContext().startActivity(i);
            }
        });
        CAdscenMediaDealStorage m = s_oDataset.get(position);
        viewHolder.m_Header.setText(m.getM_szHeaderText());
        viewHolder.m_Subheader.setText(m.getM_szsubHeaderText());
        viewHolder.m_getBtn.setText("GET " + m.getM_szDealValue() + " POINTS");// set deal button text
        Picasso.with(m_Context).load(m.getM_szImageView()).placeholder(R.drawable.placeholder).into(viewHolder.m_logoImage);
        Picasso.with(m_Context).load(m.getM_szImageView()).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();

                width = width * 2;
                height = height * 2;
                bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
                viewHolder.m_logoImage.setImageBitmap(bitmap);
                viewHolder.m_logoImage.requestLayout();

            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });
        // set deal logo


        return convertView;
    }

    private class ViewHolder {
        public TextView m_Header, m_Subheader, m_DummyText;
        public ImageView m_logoImage;
        public Button m_getBtn;
    }
}

Upvotes: 0

Views: 48

Answers (1)

Neerajlal K
Neerajlal K

Reputation: 6828

Set an OnItemClickListener on the ListView. The link will open in a browser if it is installed.

Try this code in your Activity,

m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            String url = s_oDataset.get(position).getM_Link();
            if (!url.startsWith("http://") && !url.startsWith("https://"))
                url = "http://" + url;
            Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(myIntent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(About.this, "No application can handle this request."
                    + " Please install a webbrowser",  Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
});

Upvotes: 1

Related Questions