在列表视图上打开一个网页点击

时间:2015-06-05 11:45:37

标签: android listview android-listview rss

我正在android studio中制作一个带有标签的应用程序,其中一个标签是RSS提要。我正在尝试这样做,以便当我点击其中一个列表的项目时,它会将我重定向到网页。

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标记为红色(非静态方法不能从静态上下文引用)

1 个答案:

答案 0 :(得分:1)

试试这个:

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);
            }
        }
    }