2015-04-08 14 views
0

我有一個textview,其中包含一個亞馬遜網址。我可以使用完整的URL進行工作,並且會在瀏覽器中打開鏈接。如何爲所有textview網址創建一般標籤?

但是,我不希望整個URL在textview中,我想用文本'Buy'替換它。我希望將textview設置爲所有鏈接的購買。

我看到了每個文本視圖和URL都單獨修復的問題。但一般不是所有人。我試過 - http://jtomlinson.blogspot.co.uk/2010/03/textview-and-html.html - 我設法將文本設置爲「購買」,但它不再是可點擊的鏈接。

只是給一些更多的信息。在我的應用程序中,我將xml解析到數據庫中,並且搜索將返回包含文本視圖的列表視圖中的結果。

這裏是我的onclicklistener代碼:

myList.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       // Get the cursor, positioned to the corresponding row in the result set 
       Cursor cursor = (Cursor) myList.getItemAtPosition(position); 

       //fix this line. modify string value 
       // String searchValue = cursor.getString(cursor.getColumnIndexOrThrow("searchValue")); 
       String author = cursor.getString(cursor.getColumnIndexOrThrow("author")); 
       String title = cursor.getString(cursor.getColumnIndexOrThrow("title")); 
       String price = cursor.getString(cursor.getColumnIndexOrThrow("price")); 
       String publish_date = cursor.getString(cursor.getColumnIndexOrThrow("date")); 
       String description = cursor.getString(cursor.getColumnIndexOrThrow("description")); 
       String module = cursor.getString(cursor.getColumnIndexOrThrow("module")); 
       String buy = cursor.getString(cursor.getColumnIndexOrThrow("buy")); 

       //Check if the Layout already exists 
       LinearLayout bookLayout = (LinearLayout)findViewById(R.id.customerLayout); 
       if(bookLayout == null){ 
        //Inflate the Customer Information Vie 
        LinearLayout xbookLayout = (LinearLayout)findViewById(R.id.Layout); 

        View book = getLayoutInflater().inflate(R.layout.book_info, xbookLayout, false); 
        xbookLayout.addView(book); 
       } 

       //Get References to the TextViews 
       authorText = (TextView) findViewById(R.id.xauthor); 
       titleText = (TextView) findViewById(R.id.xtitle); 
       priceText = (TextView) findViewById(R.id.xprice); 
       publishDateText = (TextView) findViewById(R.id.xpublish_date); 
       descriptionText = (TextView) findViewById(R.id.xdescription); 
       moduleText = (TextView) findViewById(R.id.xmodule); 
       buyText = (TextView) findViewById(R.id.xbuy); 

       // Update the parent class's TextView 
       authorText.setText(author); 
       titleText.setText(title); 
       priceText.setText(price); 
       publishDateText.setText(publish_date); 
       descriptionText.setText(description); 
       moduleText.setText(module); 
       buyText.setText(buy); 


       searchView.setQuery("",true); 
+0

如果你在列表中有該項目的位置(在OnItemClick中),那麼只需要調用一個遊標和getString來再次「購買」。就像你在getView中一樣。最好稱之爲「網址」。 – greenapps

+0

然後呢?那不可能是 也目前我的鏈接可以通過這個代碼在xml中點擊 android:clickable =「true」 android:autoLink =「all」/> – user3460355

+0

你不需要鏈接。如果您已在列表視圖上設置了on on item click監聽器,則您的textview是可點擊的。因此,如果項目被點擊了extrackt根據url並交給瀏覽器。 – greenapps

回答

0

你錯過設置setMovementMethod(LinkMovementMethod.getInstance())到TextView的,沒有它的TextView將無法打開link.Do像

TextView tv =(TextView) findViewById(R.id.textView1); 
    String url ="http://jtomlinson.blogspot.co.uk/2010/03/textview-and-html.html"; 
    String htmltext ="<a href ="+url+">BUY</a>"; 
    tv.setMovementMethod(LinkMovementMethod.getInstance()); 
    tv.setText(Html.fromHtml(htmltext)); 

注意:請記住,您有互聯網許可。

+0

這確實有道理。雖然我不認爲它適用於我想要的。 在你的代碼中,你有String url。但是,我不想爲每個文本視圖添加字符串URL。我有一個包含大量不同URL的XML文件,這些文件出現在我的文本視圖中。 我想要做的,是購買任何URL的buyText textview總是被設置爲Buy。我希望這樣做有一定意義 – user3460355

+0

要進一步添加。基本上,buyText文本視圖在我的列表視圖中出現了很多次。這些URL都是從我的數據庫中檢索出來的。我希望每個URL都放在buyText texview中,我希望textview文本是'buy'。 – user3460355

+0

那麼這就是你在這裏得到的。 (如果有效)。 – greenapps