如何实施Parcelable

时间:2018-07-01 09:00:16

标签: java android parcelable

美好的一天。我需要在Model类中实现parcelable,目前它是可序列化的。目前,只有setdate和set title(如果有帮助)可以帮助。请编辑代码。

MainActivity.java

  Document document = Jsoup.connect("http://feeds.bbci.co.uk/urdu/rss.xml").ignoreHttpErrors(true).get();
            Elements itemElements = document.getElementsByTag("item");
            for (int i = 0; i < itemElements.size(); i++) {
                Element item = itemElements.get(i);

                NewsItem newsItem = new NewsItem();
                newsItem.setDate(item.child(4).text());
                newsItem.setTitle(item.child(0).text());
                newsItemsList.add(newsItem);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                adapter = new NewsAdaptor(Main2Activity.this, 
               newsItemsList);
                lvRss.setAdapter(adapter);

            }
        });
        return null;
    }

NewsItem.java //模型类

public class NewsItem implements Serializable {



String imagePath;
String title;
String link;
String date;

public NewsItem () {

}

public String getImagePath () {
    return imagePath;
}

public void setImagePath ( String imagePath ) {
    this.imagePath = imagePath;
}

public String getTitle () {
    return title;
}

public void setTitle ( String title ) {
    this.title = title;
}

public String getLink () {
    return link;
}

public void setLink ( String link ) {
    this.link = link;
}

public String getDate () {
    return date;
}

public void setDate ( String date ) {
    this.date = date;


}

NewsAdapter.java

public class NewsAdaptor extends BaseAdapter {
private int textSize;
TextView tvtitle;
private int color;
Context context;

public NewsAdaptor ( Context context, ArrayList <NewsItem> newsList ) {
    this.context = context;
    this.newsList = newsList;
    this.color = Color.RED;
}

ArrayList<NewsItem> newsList;

@Override
public int getCount () {

    return newsList.size();
}

@Override
public Object getItem ( int position ) {
    return newsList.get(position);
}

@Override
public long getItemId ( int position ) {
    return 0;
}

@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
    if (convertView == null){
        convertView=View.inflate(context, R.layout.newsitemlist_layout,null);
    }
    NewsItem currentNews = newsList.get(position);
    ImageView iv1 = (ImageView) convertView.findViewById(R.id.mainimg);
    TextView tvdate = (TextView) convertView.findViewById(R.id.pubDateid);
    Picasso.with(context).load(currentNews.getImagePath()).placeholder(R.drawable.expressimg).into(iv1);
    tvdate.setText(currentNews.getDate());
    tvtitle = (TextView) convertView.findViewById(R.id.textView1id);
    tvtitle.setText(currentNews.getTitle());
    tvtitle.setTextColor(color);
    return convertView;
}
public void setTextColor(int color) {
    this.color = color;

}

1 个答案:

答案 0 :(得分:0)

检查此Parcelable NewsItem:

public class NewsItem implements Parcelable {


    String imagePath;
    String title;
    String link;
    String date;

    public NewsItem() {

    }

    protected NewsItem(Parcel in) {
        imagePath = in.readString();
        title = in.readString();
        link = in.readString();
        date = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(imagePath);
        dest.writeString(title);
        dest.writeString(link);
        dest.writeString(date);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<NewsItem> CREATOR = new Creator<NewsItem>() {
        @Override
        public NewsItem createFromParcel(Parcel in) {
            return new NewsItem(in);
        }

        @Override
        public NewsItem[] newArray(int size) {
            return new NewsItem[size];
        }
    };

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;


    }

}