Search code examples
androidsqliteandroid-sqliteandroid-listfragment

How can I get the data of sqlite and put it in FragmentList and click on item


That I want to do is read the data from a sqlite and display the data in a listview. Then, when you click in the item, open a webview to send the user to the url.

I'm extends of ListFragment

This is my database sqlite that works:

 private static final String DATABASE_CREATE = "create table "
            + TABLENAME + "("
            + ID + " integer primary key autoincrement, "
            + URL + " text not null, "
            + NAME + " text not null); ";

At the moment, I can get and display the name in the listfragment, but I don't know how to do it to can pass throught the item the url to can open the webview (In this code I not implemented the part of the webView because I want first can pass the url to the current item).

This is that I'm trying:

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

        ViewGroup rootView = null;


            dataBase.getAllData();


            String[] name = new String[dataBase.getAllData().size()];
            String[] url = new String[dataBase.getAllData().size()];

            for (int i = 0; i < dataBase.getAllData().size(); i++) {
                name[i] = dataBase.getAllData().get(i).getName();
                url[i] = dataBase.getAllData().get(i).getUrl();
                Log.e("Total", name[i] + "" +url[i]);

                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_listview, container, false);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.favourites, R.id.newspaperNameFavourite, name);
                setListAdapter(adapter);
                setRetainInstance(true);


                mAdView = (AdView) rootView.findViewById(R.id.adViewListFragment);
                adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);

            }
            return rootView;
    }

    @Override
    public void onListItemClick(ListView l, View view, int position, long id){
        ViewGroup viewg=(ViewGroup)view;
        TextView tv=(TextView)viewg.findViewById(R.id.newspaperNameFavourite);
        Toast.makeText(getActivity(), tv.getText().toString(), Toast.LENGTH_LONG).show();

    }

Solution

  • Add this code in onListItemClick()method:

    String url = "http://www.google.com"; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); startActivity(intent);

    In your case:

    TextView urlText = (TextView) viewg.findViewById(R.id.url); // instancing intent intent.setData(Uri.parse(urlText.getText.toString()); startActivity(intent);