当我点击列表视图内容时,他们正在使用不在我的应用程序内的Web浏览器打开

时间:2012-12-05 20:22:25

标签: android listview webview android-listview

我的Android应用程序在列表视图中显示我的网站的RSS源。首先,我的应用程序显示一个“加载”页面,最后20个帖子的标题出现在列表视图中。当我点击主题名称时,主题是使用不在我的应用程序中的Web浏览器打开。

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>

1 个答案:

答案 0 :(得分: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);

    }
});

进一步的文件: