Android中的ListView,带有可链接的文本

时间:2014-04-13 09:53:15

标签: java android

我从.NET Web服务获得此输入:

https://www.LinkFromImageonTheGoogle.net/myImage.png<br />23/03/2014<br /><a href=http://www.mywebpage.net?ID=764>My remote pics</a><br /><br />

https://www.LinkFromImageonTheGoogle.net/myImage1.png<br />22/03/2014<br /><a href=http://www.mywebpage.net?ID=765>My remote pics 1</a><br /><br />

https://www.LinkFromImageonTheGoogle.net/myImage2.png<br />21/03/2014<br /><a href=http://www.mywebpage.net?ID=766>My remote pics 2</a><br /><br />

我需要在 ListView android应用程序中输出

https://www.LinkFromImageonTheGoogle.net/myImage.png ( I need show the image not link to image )
23/03/2014
My remote pics (with link active http://www.mywebpage.net?ID=764)


https://www.LinkFromImageonTheGoogle.net/myImage1.png ( I need show the image not link to image )
22/03/2014
My remote pics 1 (with link active http://www.mywebpage.net?ID=765)


https://www.LinkFromImageonTheGoogle.net/myImage2.png ( I need show the image not link to image )
21/03/2014
My remote pics 2 (with link active http://www.mywebpage.net?ID=766)

并尝试了以下代码,但在输出中我只在文本中链接,而不是三行不同的html格式:

<a href=http://www.mywebpage.net?ID=764> 
<a href=http://www.mywebpage.net?ID=765> 
<a href=http://www.mywebpage.net?ID=766>

java class

public class news2 extends Activity {

    private static final String SOAP_ACTION = "http://www.xxxxx.com/WebService/GetNews";
    private static final String OPERATION_NAME = "GetNews";
    private static final String WSDL_TARGET_NAMESPACE = "http://www.xxxxx.com/GetNews";
    private static final String SOAP_ADDRESS = "http://www.xxxxx.com/GetNews.asmx";


    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mains);

        mainListView = (ListView) findViewById(R.id.mainListView);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                OPERATION_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelope.dotNet = true;
        envelope.implicitTypes = false;
        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        envelope.setOutputSoapObject(request);

        try {

            httpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

            String RemoteString = response.toString();
            Log.i("RemoteString", RemoteString.toString());

            RemoteString = RemoteString.replaceAll("<br />", "\n");
            Log.i("RemoteStringnew", RemoteString);

            String[] var = RemoteString.split("\\\\r?\\\\n");// new line

            ArrayList<String> planetList = new ArrayList<String>();

            listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
                    R.id.rowTextView, planetList);

            for (String html : var) {
                Spanned varHtml = Html.fromHtml(html);

                Object[] strings = varHtml.getSpans(0, html.length(),
                        Object.class);

                for (Object obj : strings) {
                    if (obj instanceof URLSpan) {
                        URLSpan urlSpan = (URLSpan) obj;

                        planetList.add(urlSpan.getURL());
                    }
                }
            }

            mainListView.setAdapter(listAdapter);

        } catch (Exception exception) {
            Log.e("Error:  ", exception.toString());
        }
    }
}

mains.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">

    <ListView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/mainListView">
    </ListView>

</LinearLayout>

simplerow.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rowTextView" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:padding="10dp"
 android:textSize="16sp" >
</TextView>

编辑1

    for (String html : var) {

        SpannableString text = new SpannableString(html);

        Object[] strings = text.getSpans(0, html.length(),
                Object.class);

        for (Object obj : strings) {
            if (obj instanceof URLSpan) {
                URLSpan urlSpan = (URLSpan) obj;

                planetList.add(urlSpan.getURL());
            }
        }
    }

    mainListView.setAdapter(listAdapter);

0 个答案:

没有答案