Open a webpage on list view click

1.3k views Asked by At

I'm making an app in android studio which has tabs and one of those tabs is an RSS feed. I'm trying to make it so that when I click on one of the lists' items it would redirect me to the webpage.

Tab_1.class:

public class Tab_1 extends Fragment {
ListView mList;
ArrayList<Info> arrayOfInfo = new ArrayList<>();
int i=-1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

            String url = LAdapter.getItem(position).getLink();
            //Intent i = new Intent(Intent.ACTION_VIEW);
            //i.setData(Uri.parse(url));
            //startActivity(i);
        }
    });
}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.tab_1, container, false);
            mList = (ListView) v.findViewById(R.id.list);
            new GetRssFeed().execute("http://feeds.bbci.co.uk/news/rss.xml");

            return v;
        }

        private class GetRssFeed extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... params) {
                try {
                    RssReader rssReader = new RssReader(params[0]);
                    for (RssItem item : rssReader.getItems()) {
                        arrayOfInfo.add(i++, new Info(item.getTitle(), item.getDescription(), item.getLink()));
                    }
                } catch (Exception e) {
                    Log.v("Error Parsing Data", e + "");
                }
                return null;
            }


            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                LAdapter adapter = new LAdapter(getActivity(), arrayOfInfo);
                adapter.notifyDataSetChanged();
                mList.setAdapter(adapter);
            }
        }
    }

P.S. url = LAdapter.getItem(position).getLink(); getItem is marked as red(Non-static method cannot be referenced form static context)

1

There are 1 answers

0
Eddwhis On

Try this:

public class Tab_1 extends Fragment {
ListView mList;
ArrayList<Info> arrayOfInfo = new ArrayList<>();
int i=-1;
LAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

            String url = adapter.getItem(position).getLink();
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });
}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.tab_1, container, false);
            mList = (ListView) v.findViewById(R.id.list);
            new GetRssFeed().execute("http://feeds.bbci.co.uk/news/rss.xml");

            return v;
        }

        private class GetRssFeed extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... params) {
                try {
                    RssReader rssReader = new RssReader(params[0]);
                    for (RssItem item : rssReader.getItems()) {
                        arrayOfInfo.add(i++, new Info(item.getTitle(), item.getDescription(), item.getLink()));
                    }
                } catch (Exception e) {
                    Log.v("Error Parsing Data", e + "");
                }
                return null;
            }


            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                adapter = new LAdapter(getActivity(), arrayOfInfo);
                adapter.notifyDataSetChanged();
                mList.setAdapter(adapter);
            }
        }
    }