0

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)

Blue
  • 22,608
  • 7
  • 62
  • 92
Paul S
  • 37
  • 8
  • asked bazillion times ... all information you need are passed to `onItemClick` ... there is no need to use other references ... cast `AdapterView.getItemAtPosition` result to item type and use it – Selvin Jun 05 '15 at 11:53
  • String url = AdapterView.getItemAtPosition.Link; getItemAtPosition - cannot resolve symbol. – Paul S Jun 05 '15 at 12:05
  • ok.... learn java ... `getItemAtPosition` is not static ... you have to had `AdapterView` instance ... check the `onItemClick` parameters ... – Selvin Jun 05 '15 at 12:06
  • Oddly it crashes, when I have this In OnCreate `mList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { String url = (String) (mList.getItemAtPosition(position)); } });` – Paul S Jun 05 '15 at 12:19
  • `AdapterView` in `onItemClick` is `parent` ... FSM save us!: `final Item item = ((Item)parent.getItemAtPosition (position));` ... which was pointend in my first comment, all you had too do was reading with understanding – Selvin Jun 05 '15 at 12:20
  • How can I use that, to get and use the url? – Paul S Jun 05 '15 at 12:30
  • hmmm, my bad ... seems like you have to had code ready to copy&paste ... `Item` should be `Info` then all you have to do is use `item` – Selvin Jun 05 '15 at 12:33
  • Still crash with an empty log cat and even if I leave OnItemClick empty it still crashes exactly the same.. – Paul S Jun 05 '15 at 12:38
  • http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it + http://developer.android.com/guide/components/fragments.html#Creating what is called first? `onCreate` or `onCreateView`? anyway enough, I'm not your personal teacher – Selvin Jun 05 '15 at 12:39
  • ok last thing: yes, so: ... obviously `mList` is null there ... it implicates to move `mList.setOnItemClickListener` after `mList` is setted ... – Selvin Jun 05 '15 at 12:44
  • And it becomes not null in OnPostExecute which is after OnCreate. – Paul S Jun 05 '15 at 12:46
  • no, after `mList = (ListView) v.findViewById(R.id.list);` ... you can freely use it after this line ... – Selvin Jun 05 '15 at 12:47
  • Kay, thank you for that, but maybe you know why my logcat is completely blank? – Paul S Jun 05 '15 at 12:53

1 Answers1

1

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);
            }
        }
    }
Eddwhis
  • 1,124
  • 2
  • 16
  • 33