设置OnItemClick以在webview中为rss feed打开URL

时间:2012-10-04 22:39:08

标签: android listview rss xml-parsing

我知道我已经在考虑这个或者过度思考这个但是我似乎无法弄清楚当用户点击它时如何启动rss feed的链接。我正在使用DOM Parser将xml文件解析为listview并从xml文件中获取所有信息。我遇到的问题是将其解析为自定义WebActivity,我应该加载我拥有的webview客户端。这是我正在使用的代码。

主要活动

 public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://treymorgan.net/feed";
// XML node keys
static final String KEY_ITEM = "item"; // parent node 
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_PUB_DATE = "pubDate";
static final String KEY_LINK = "guid";

ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_PUB_DATE, parser.getValue(e, KEY_PUB_DATE));
        map.put(KEY_LINK, parser.getValue(e, KEY_LINK));

        // adding HashList to ArrayList
        songsList.add(map);
    }


    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

编辑:这是我遇到麻烦的代码部分... onItemClick方法将RSS输入的URL发送到webview活动,以便可以在应用程序中查看,而不是通过本机浏览器查看设备

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Intent intent = new Intent(CustomizedListView.this, WebviewActivity.class);  
            Bundle b = new Bundle();
            intent.putExtra("KEY_LINK", b);
            startActivity(intent); 



             }

    });
}   
 }

这是我的Webview活动

 public class WebviewActivity extends Activity{
  private WebView webview;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    Bundle b = getIntent().getExtras(); 
    String KEY_LINK = b.getString("KEY_LINK");

    webview = (WebView) findViewById(R.id.webView1);
    webview.setWebViewClient(new MyWebViewClient(this));
    webview.getSettings().setAppCacheEnabled(false);
    webview.getSettings().setBuiltInZoomControls(true);
    webview.setInitialScale(1);
    webview.loadUrl(KEY_LINK);



  }


       }

基本上,我想要做的是将KEY_GUID串行到WebviewActivity,我似乎无法弄清楚如何将字符串从一个活动转移到另一个活动。在此先感谢您的任何帮助

编辑:好的我已经做了一段时间了,我已经更新了我正在使用的代码,这与此问题相关。现在我没有错误,当我点击一个项目时,它将打开WebviewActivity,但它不会加载网站,也不会将我的webview客户端连接到它。任何人都可以帮我解决这个问题吗?

编辑: 当我加载这样的代码时:

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

            Intent intent = new Intent(CustomizedListView.this,    WebviewActivity.class);  
            Bundle b = new Bundle();
            intent.putExtra("b", "http://www.google.com" );
            startActivity(intent); 

它会打开webview活动并加载谷歌就好了。所以也许我现在遇到的问题是如何从我的DOM解析器获取我需要的链接。有人有主意吗?

3 个答案:

答案 0 :(得分:0)

在您的通话活动中:

Intent i = new Intent(this, WebActivity.class);
i.putExtra(MY_KEY, "http://http://stackoverflow.com");
startActivity(i);

在您的接收活动中:

protected void onStart() {
  super.onStart();
  String myUrl = getExtras().getString(MY_KEY);
  webView.loadUrl(myUrl);
}

修改

假设您的适配器包含HashMaps(从我看到的解析XML的方式)

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  HashMap<String, String> selectedSong = (HashMap<String, String>) parent.getItemAtPosition(position);

  Intent intent = new Intent(CustomizedListView.this, WebviewActivity.class);  
  intent.putExtra("KEY_LINK", selectedSong.get(KEY_LINK));
  startActivity(intent); 
}

答案 1 :(得分:0)

用于从一个活动到另一个活动的传递字符串

第一项活动:

Intent indisplay = new Intent(FirstClass.this,SecondClass.class);
indisplay.putExtra("link",your link here);
startActivity(indisplay);

第二项活动:

 Intent indispaly = getIntent();
 String url = indispaly.getStringExtra("link");
    // your link from previous activity is stored into the url

并显示在 webview 中,例如:

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.newsdisplay);
    getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 


    Intent indispaly = getIntent();
    String url = indispaly.getStringExtra("link");

    newswebview = (WebView) findViewById(R.id.news_webView);
    newswebview.setWebViewClient(new WebViewClient());

    WebSettings webSettings = newswebview.getSettings();
    webSettings.setJavaScriptEnabled(true);

    newswebview.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this 
     //if ROM supports Multi-Touch      
    newswebview.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM


    newswebview.loadUrl(url);


    final Activity MyActivity = this;


newswebview.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) 
       {

        //Make the bar disappear after URL is loaded, and changes string to Loading...
        MyActivity.setTitle("  Loading...");
        MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

        // Return the app name after finish loading
           if(progress == 100){

               MyActivity.setTitle(R.string.app_name);

           }


    }
});

}

 @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)   
    {  
        if ((keyCode == KeyEvent.KEYCODE_BACK) && newswebview.canGoBack()) {  
            newswebview.goBack();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    }   

答案 2 :(得分:0)

好的,这是我问的问题的答案。这对我很有用,并且完全符合我的要求。这是它的工作代码

 public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://treymorgan.net/feed";
// XML node keys
static final String KEY_ITEM = "item"; // parent node 
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_PUB_DATE = "pubDate";
static final String KEY_LINK = "link";

ListView list;
LazyAdapter adapter;

ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ITEM));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_PUB_DATE, parser.getValue(e, KEY_PUB_DATE));
        map.put(KEY_LINK, parser.getValue(e, KEY_LINK));

        // adding HashList to ArrayList
        songsList.add(map);
    }


    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

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

            Intent intent = new Intent(CustomizedListView.this, WebviewActivity.class);  
            new Bundle();
            intent.putExtra( "b", songsList.get(position).get(KEY_LINK));
            startActivity(intent); 



             }

    });
}   
 }