Android 切换视图

发布于 2024-12-11 14:41:55 字数 4763 浏览 0 评论 0原文

我构建了一个 RSS 阅读器,当单击列表项时,它会切换到详细视图。我在打开新视图时遇到问题。我的代码在这里:

public void onItemClick(AdapterView parent, View v, int position, long id)
 {

     //Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");      

     Intent itemintent = new Intent(this,ShowDescription.class);

     Bundle b = new Bundle();
     b.putString("title", feed.getItem(position).getTitle());
     b.putString("description", feed.getItem(position).getDescription());
     b.putString("link", feed.getItem(position).getLink());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

 }

整个类的代码如下:

package com.CalvaryChapelMelbourne.feedparser;


import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener; 
import android.util.Log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

import org.xml.sax.XMLReader;

import android.content.Intent;

import com.CalvaryChapelMelbourne.feedparser.ShowDescription;

public class RSSReader extends Activity implements OnItemClickListener
{

public final String RSSFEEDOFCHOICE = "http://app.calvaryccm.com/mobile/android/v1/devos";

public final String tag = "RSSReader";
private RSSFeed feed = null;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    // go get our feed!
    feed = getFeed(RSSFEEDOFCHOICE);

    // display UI
    UpdateDisplay();

}


private RSSFeed getFeed(String urlToRssFeed)
{
    try
    {
        // setup the url
       URL url = new URL(urlToRssFeed);

       // create the factory
       SAXParserFactory factory = SAXParserFactory.newInstance();
       // create a parser
       SAXParser parser = factory.newSAXParser();

       // create the reader (scanner)
       XMLReader xmlreader = parser.getXMLReader();
       // instantiate our handler
       RSSHandler theRssHandler = new RSSHandler();
       // assign our handler
       xmlreader.setContentHandler(theRssHandler);
       // get our data via the url class
       InputSource is = new InputSource(url.openStream());
       // perform the synchronous parse           
       xmlreader.parse(is);
       // get the results - should be a fully populated RSSFeed instance, or null on error
       return theRssHandler.getFeed();
    }
    catch (Exception ee)
    {
        // if we have a problem, simply return null
        System.out.println(ee.getMessage());
        System.out.println(ee.getStackTrace());
        System.out.println(ee.getCause());
        return null;
    }
}
public boolean onCreateOptionsMenu(Menu menu) 
{
    super.onCreateOptionsMenu(menu);
    menu.add(Menu.NONE, 0, 0, "Refresh");
    Log.i(tag,"onCreateOptionsMenu");
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
    case 0:

        Log.i(tag,"Set RSS Feed");
        return true;
    case 1:
        Log.i(tag,"Refreshing RSS Feed");
        return true;
    }
    return false;
}


private void UpdateDisplay()
{
    TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
    TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
    ListView itemlist = (ListView) findViewById(R.id.itemlist);


    if (feed == null)
    {
        feedtitle.setText("No RSS Feed Available");
        return;
    }

    if(feedtitle != null)
        feedtitle.setText(feed.getTitle());
    if(feedpubdate != null)
        feedpubdate.setText(feed.getPubDate());


    ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());

    itemlist.setAdapter(adapter);

    itemlist.setOnItemClickListener(this);

    itemlist.setSelection(0);

}


 public void onItemClick(AdapterView parent, View v, int position, long id)
 {

     //Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");      

     Intent itemintent = new Intent(this,ShowDescription.class);

     Bundle b = new Bundle();
     b.putString("title", feed.getItem(position).getTitle());
     b.putString("description", feed.getItem(position).getDescription());
     b.putString("link", feed.getItem(position).getLink());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

 }

}

I have built a RSS reader that switches to a detail view when a list item is clicked. I am having a problem opening the new view. My code is here:

public void onItemClick(AdapterView parent, View v, int position, long id)
 {

     //Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");      

     Intent itemintent = new Intent(this,ShowDescription.class);

     Bundle b = new Bundle();
     b.putString("title", feed.getItem(position).getTitle());
     b.putString("description", feed.getItem(position).getDescription());
     b.putString("link", feed.getItem(position).getLink());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

 }

The code for the whole class is below:

package com.CalvaryChapelMelbourne.feedparser;


import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener; 
import android.util.Log;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

import org.xml.sax.XMLReader;

import android.content.Intent;

import com.CalvaryChapelMelbourne.feedparser.ShowDescription;

public class RSSReader extends Activity implements OnItemClickListener
{

public final String RSSFEEDOFCHOICE = "http://app.calvaryccm.com/mobile/android/v1/devos";

public final String tag = "RSSReader";
private RSSFeed feed = null;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    // go get our feed!
    feed = getFeed(RSSFEEDOFCHOICE);

    // display UI
    UpdateDisplay();

}


private RSSFeed getFeed(String urlToRssFeed)
{
    try
    {
        // setup the url
       URL url = new URL(urlToRssFeed);

       // create the factory
       SAXParserFactory factory = SAXParserFactory.newInstance();
       // create a parser
       SAXParser parser = factory.newSAXParser();

       // create the reader (scanner)
       XMLReader xmlreader = parser.getXMLReader();
       // instantiate our handler
       RSSHandler theRssHandler = new RSSHandler();
       // assign our handler
       xmlreader.setContentHandler(theRssHandler);
       // get our data via the url class
       InputSource is = new InputSource(url.openStream());
       // perform the synchronous parse           
       xmlreader.parse(is);
       // get the results - should be a fully populated RSSFeed instance, or null on error
       return theRssHandler.getFeed();
    }
    catch (Exception ee)
    {
        // if we have a problem, simply return null
        System.out.println(ee.getMessage());
        System.out.println(ee.getStackTrace());
        System.out.println(ee.getCause());
        return null;
    }
}
public boolean onCreateOptionsMenu(Menu menu) 
{
    super.onCreateOptionsMenu(menu);
    menu.add(Menu.NONE, 0, 0, "Refresh");
    Log.i(tag,"onCreateOptionsMenu");
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
    case 0:

        Log.i(tag,"Set RSS Feed");
        return true;
    case 1:
        Log.i(tag,"Refreshing RSS Feed");
        return true;
    }
    return false;
}


private void UpdateDisplay()
{
    TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
    TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
    ListView itemlist = (ListView) findViewById(R.id.itemlist);


    if (feed == null)
    {
        feedtitle.setText("No RSS Feed Available");
        return;
    }

    if(feedtitle != null)
        feedtitle.setText(feed.getTitle());
    if(feedpubdate != null)
        feedpubdate.setText(feed.getPubDate());


    ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());

    itemlist.setAdapter(adapter);

    itemlist.setOnItemClickListener(this);

    itemlist.setSelection(0);

}


 public void onItemClick(AdapterView parent, View v, int position, long id)
 {

     //Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");      

     Intent itemintent = new Intent(this,ShowDescription.class);

     Bundle b = new Bundle();
     b.putString("title", feed.getItem(position).getTitle());
     b.putString("description", feed.getItem(position).getDescription());
     b.putString("link", feed.getItem(position).getLink());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事↓在人 2024-12-18 14:41:55

您需要在 onItemClick 方法末尾调用 startActivity(itemintent)。另外,您要切换的是“活动”,而不是“视图”。请参阅此文档有关 UI 的文档以获得更好的理解。

You need to call startActivity(itemintent) at the end of your onItemClick method. Also, what you are switching is Activities, not Views. See this documentation and this documentation on UI to get a better understanding.

被你宠の有点坏 2024-12-18 14:41:55

您必须启动 ShowDescription 活动。

public void onItemClick(AdapterView parent, View v, int position, long id)  {
  ...
  startActivity(itemintent);
}

You have to start your ShowDescription activity.

public void onItemClick(AdapterView parent, View v, int position, long id)  {
  ...
  startActivity(itemintent);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文