简体   繁体   中英

Spanned and html format in Android

I need format this string in html output for android application:

<a href=http://www.mywebpage.net?ID=764>My remote pics</a>

And tried the Spanned method in the ListView :

Spanned varHtml = Html.fromHtml(var.toString());

ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll(Arrays.asList(varHtml));

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
        R.id.rowTextView, planetList);
listAdapter.addAll(varHtml.toString());
mainListView.setAdapter(listAdapter);

But I've this error in this line, why?

listAdapter.addAll(varHtml.toString());

Error

The method addAll(Collection<? extends String>) in the
type ArrayList<String> is not applicable for the
arguments (List<Spanned>)

EDIT

Complete java class for ListView dynamic data:

public class news 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();

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

                    //Replace in remote string
            String RemoteStringnew = RemoteString.replaceAll("<br />", "\n");
            Log.i("RemoteStringnew", RemoteStringnew);

                    //Split in remote string
            String[] var = RemoteStringnew.split("\n");

                    //Spanned in remote string
            Spanned varHtml = Html.fromHtml(var.toString());
            Log.i("varHtml", varHtml.toString());

            ArrayList<String> planetList = new ArrayList<String>();
            planetList.addAll(Arrays.asList(varHtml.toString()));

            listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
                    R.id.rowTextView, planetList);
            listAdapter.add(varHtml.toString());
            mainListView.setAdapter(listAdapter);

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

EDIT

The remote string is:

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 />

EDIT 2

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

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

        // Replace in remote string
        // it's not needed
        RemoteString = RemoteString.replaceAll("<br />", "\n");
        Log.i("RemoteStringnew", RemoteString);

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

        // Spanned in remote string

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

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

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

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

            // List<String> urls = new ArrayList<String>();
            for (Object obj : strings) {
                if (obj instanceof URLSpan) {
                    URLSpan urlSpan = (URLSpan) obj;

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

        mainListView.setAdapter(listAdapter);

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

EDIT 3

I need this output:

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)

EDIT 4

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>

addAll wants a Collection of items to add.

You should use add method which takes a String in this case.

Appends the specified element to the end of this list.


Change

listAdapter.addAll(varHtml.toString());

to

listAdapter.add(varHtml.toString());

That's how you should do what you want

String html = "<a href=http://www.mywebpage.net?ID=764>My remote pics</a>"; // the HTML to read
Spanned spanned = Html.fromHtml(html);

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

List<String> urls = new ArrayList<String>();
for (Object obj : strings)
{
    if (obj instanceof URLSpan)
    {
        URLSpan urlSpan = (URLSpan) obj;

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

It will read every Url and add everything in urls list. You can update directly your List or do

listAdapter.addAll(urls);

This is how could you do it;

public class news 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();

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

            //Replace in remote string
            // it's not needed
            RemoteString = RemoteString.replaceAll("<br />", "\n");
            Log.i("RemoteStringnew", RemoteString);

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

            //Spanned in remote string

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

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

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

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

                List<String> urls = new ArrayList<String>();
                for (Object obj : strings)
                {
                    if (obj instanceof URLSpan)
                    {
                        URLSpan urlSpan = (URLSpan) obj;

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

            mainListView.setAdapter(listAdapter);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM