2012-12-05 37 views
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> 
+0

不知道我理解你正在嘗試做的100%,而是試圖尋找到 '延伸ListActivity' 你活動 – daniel

+0

這裏列出的項目顯示了我的網站的最新20個職位的標題...所以有20個項目在那裏..他們從一個feed.xml文件解析。所以無論何時我會在我的網站上發佈新內容,項目都會動態更改。當點擊鏈接項目時,我想在應用內打開網頁,而不是瀏覽器。 –

回答

3

這應該工作:

ListView控件的ListView =新的ListView(本);

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

    } 
}); 

更多文檔:

+0

@Anik Chakraborty如果有幫助,你可以接受我的回答。它會提高你的接受率,並會鼓勵人們回答你未來的問題。 –

+0

實際上,這裏所有的列表視圖項目都是我的博客帖子的標題,從一個xml提要文件解析。因此,所有列表視圖項目都已經有了urls ..我只是想創建一個webview活動,它將在這個應用程序中打開鏈接,而不是在web瀏覽器中,當任何這些listview項目將被點擊。列表視圖項目將不斷變化,因爲它們顯示我的網站的最新20個職位的標題。所以,在listview中有20個項目,每個項目都有不同的網址。我怎樣才能實現呢? (在我的應用程序中打開瀏覽器中的網頁)Plz help ....該網站針對移動視圖進行了優化 –

+0

您必須在您的活動中使用WebView。當你創建一個listItem時,爲包含一個URL的textView設置一個clickListener,然後用一個WebView開始一個新的Activity。 –

相關問題