有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我从中得到了这个输入。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安卓应用程序中输出:

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类

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

电源。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
  安卓:orientation="vertical"
  安卓:layout_width="fill_parent"
  安卓:layout_height="fill_parent">

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

</LinearLayout>

simplerow。xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
 安卓:id="@+id/rowTextView" 
 安卓:layout_width="fill_parent" 
 安卓:layout_height="wrap_content"
 安卓:padding="10dp"
 安卓: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) 个答案