1

我的 android 应用程序在列表视图中显示我网站的 rss 提要。起初,我的应用程序会在最新的 20 篇文章的标题出现在列表视图中之后显示一个“加载”页面。当我单击主题名称时,主题正在使用不在我的应用程序中的网络浏览器打开。

this is my MainActivity.java

    public class MainActivity extends ListActivity {

 private MyFeed myRssFeed = null;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  new MyTask().execute();

 }
 private class MyTask extends AsyncTask<Void, Void, Void>{

  @Override
  protected Void doInBackground(Void... arg0) {
   try {
    URL rssUrl = new URL("http://mywindows8.org/windows-8-tutorials/feed/");
    SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
    SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
    XMLReader myXMLReader = mySAXParser.getXMLReader();
    MyWindows myRSSHandler = new MyWindows();
    myXMLReader.setContentHandler(myRSSHandler);
    InputSource myInputSource = new InputSource(rssUrl.openStream());
    myXMLReader.parse(myInputSource);

    myRssFeed = myRSSHandler.getFeed(); 
   } catch (MalformedURLException e) {
    e.printStackTrace(); 
   } catch (ParserConfigurationException e) {
    e.printStackTrace(); 
   } catch (SAXException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace(); 
   }

   return null;
  }

  @Override
  protected void onPostExecute(Void result) {
   if (myRssFeed!=null)
   {

  ArrayAdapter<Topic> adapter =
          new ArrayAdapter<Topic>(getApplicationContext(), R.layout.custom_list_item,myRssFeed.getList());
    setListAdapter(adapter); 

   }else{

    TextView textEmpty = (TextView)findViewById(android.R.id.empty);
    textEmpty.setText("No Feed Found!");
   }

   super.onPostExecute(result);
  }

 }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {

  Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink());
  Intent myIntent = new Intent(Intent.ACTION_VIEW, feedUri);
  startActivity(myIntent);

 }

}

这是 activity_main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" 
        android:textSize="20sp"
        android:background="@layout/button5"
        android:orientation="vertical"
        android:textColor="#D5D5D5"
        android:gravity="center"/>

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:textColor="#000000"
        android:background="@layout/back2"
        android:orientation="vertical"
        android:divider="@layout/list_divider"
        android:dividerHeight="2dp"


        />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/loading"
        android:gravity="center"
       />
</LinearLayout>
4

1 回答 1

3

这应该有效:

ListView listView = new ListView(this);

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View listItemView, int positionOfItem, long idOfItem) {
        // Here you put what you want to do when a listItem is clicked
        Intent k = new Intent(this, Contenturl.class);
                k.putExtra(org.core.mywindows8.Contenturl.URL, "http://mywindows8.org/windows-8-themes/");
                MainActivity.this.finish();
                startActivity(k);

    }
});

更多文档:

于 2012-12-05T20:34:44.640 回答