如何使用Intent传递Spanned数据类型数据 [英] How to pass Spanned Data-type data using Intent

查看:105
本文介绍了如何使用Intent传递Spanned数据类型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些数据从一项活动传递到另一项活动.所有数据都可以正常传递,但是其中一个数据类型为Spanned的数据根本无法正常工作. 当我在第一个活动中显示它时,它可以工作,但是当我在另一个活动中显示它时,它甚至都没有出现.

I am passing some data from one activity to another. All the data are passing alright but there one with data type Spanned which isnt working at all. When I display it in the first Activity,it works but when I display it in the other activity,it doest even appear.

我像这样从json中获取数据

I took the data from json like this

public void parseJsonData(final String jsonString) {

    try {
        jArray = new JSONArray(jsonString);

        for(int i=0; i < jArray.length(); i++) {

            JSONObject jObject = jArray.getJSONObject(i);
            news news1 = new news();
            news1.setCategory("Spor");
            news1.setTitle(jObject.getString("title"));
            news1.setDate(jObject.getString("date"));
            news1.setContent(Html.fromHtml(jObject.getString("content")));//Here the data type of Content is Spanned
            news1.setShort_content(jObject.getString("short_content"));
            Sdatalist.add(news1);

        }

        if (dialog.isShowing()){
            dialog.dismiss();
        }
    } catch (JSONException e) {
        e.printStackTrace();
        dialog.dismiss();
    }
}

我从适配器发送了这样的数据

From my Adapter I sent the data like this

 myholder.myimageview1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent myIntent = new Intent(mycontext, Detailews.class);
                myIntent.putExtra("category", mydatalist.get(position).getCategory());
                myIntent.putExtra("title", mydatalist.get(position).getTitle());
                myIntent.putExtra("date", mydatalist.get(position).getDate());
                myIntent.putExtra("cont", mydatalist.get(position).getContent());
                myIntent.putExtra("image", mydatalist.get(position).getImage());
                mycontext.startActivity(myIntent);
            }
        });

在另一项活动中,我收到了这样的数据

And in the other activity I recieved the data like this

   cat = (TextView)findViewById(R.id.Dtextcategory);
    dat = (TextView)findViewById(R.id.Ddatetext);
    tit = (TextView)findViewById(R.id.Dtitletext);
    con = (TextView)findViewById(R.id.textView4);

    Intent in = getIntent();
    final  String image_url = in.getStringExtra("image");
    final  String title = in.getStringExtra("title");
    final  String date = in.getStringExtra("date");
    final  String co = in.getStringExtra("cont");//If I try making the datatype here spanned,it wont understand :)
    final  String category = in.getStringExtra("category");

    cat.setText(category);
    dat.setText(date);
    tit.setText(title);
    con.setText(co); //Here is where it does not show.

    imageView = (ImageView)findViewById(R.id.Dimage);
    new CustomAdapter.DownloadImageTask(imageView).execute(image_url);

我需要一些人来找到错误. :)帮帮我

I need some one to find the bug. :) help me out

推荐答案

getContent()将需要返回CharSequence.然后,使用getCharSequenceExtra()而不是getStringExtra(),将其保存到CharSequence变量中.

getContent() would need to return a CharSequence. Then, use getCharSequenceExtra(), not getStringExtra(), saving it to a CharSequence variable.

IOW,字符串没有跨度. CharSequence对象具有跨度.通过toString()CharSequence转换为字符串时,会丢失跨度.

IOW, strings do not have spans. CharSequence objects have spans. When you convert a CharSequence to a string via toString(), you lose the spans.

这篇关于如何使用Intent传递Spanned数据类型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆