我的应用程序正在从服务器接收json并基于我正在使用谷歌的gson库创建模型(json字符串直接映射到java对象表示)。然后我需要将几个相同的数组模型传递给Fragment来创建视图。我怎样才能实现它,因为Fragments具有静态构造函数,并且参数可以通过Bundle对象传递。 这是一个非常简单的模型,它将在数组中传递给另一个:
public class SingleHomeCardItem {
private String title;
private String subtitle;
private String badgeNumber;
private String backgroundUrl;
private String backgroundGradient;
private String targetUrl;
private String target;
private String row;
public String getTitle() {
return title;
}
public String getSubtitle() {
return subtitle;
}
public String getBadgeNumber() {
return badgeNumber;
}
public String getBackgroundUrl() {
return backgroundUrl;
}
public String getBackgroundGradient() {
return backgroundGradient;
}
public String getTargetUrl() {
return targetUrl;
}
public String getTarget() {
return target;
}
public String getRow() {
return row;
}
public void setTitle(String title) {
this.title = title;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public void setBadgeNumber(String badgeNumber) {
this.badgeNumber = badgeNumber;
}
public void setBackgroundUrl(String backgroundUrl) {
this.backgroundUrl = backgroundUrl;
}
public void setBackgroundGradient(String backgroundGradient) {
this.backgroundGradient = backgroundGradient;
}
public void setTargetUrl(String targetUrl) {
this.targetUrl = targetUrl;
}
public void setTarget(String target) {
this.target = target;
}
public void setRow(String row) {
this.row = row;
}
}
这是我希望接收模型数组并处理它以创建视图的片段:
public class HomeshelfFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = createView(inflater, container, getArguments().????);
return v;
}
public static HomeshelfFragment newInstance(SingleHomeCardItem[] items) {
HomeshelfFragment f = new HomeshelfFragment();
Bundle b = new Bundle();
// ?????
f.setArguments(b);
return f;
}
private View createView(LayoutInflater inflater, ViewGroup container, SingleHomeCardItem[] items) {
View v = inflater.inflate(R.layout.home_card_3_items, container, false);
// add views to the inflated view based on models data
return v;
}
}
解决这个问题的最佳方法是什么? 提前谢谢!
答案 0 :(得分:2)
解决方案1
而是将Json对象转换为java类,您可以创建可以保存数据的Json对象。因此,你可以通过活动和片段传递你的数据,如字符串
public class SingleHomeCardItem extends JSONObject {
private static final String TAG = SingleHomeCardItem.class.getSimpleName();
public static final String subtitle = "subtitle";
public static final String badgeNumber = "badgeNumber";
public static final String backgroundUrl = "backgroundUrl";
public static final String backgroundGradient = "backgroundGradient";
public static final String targetUrl = "targetUrl";
public static final String target = "target";
public static final String row = "row";
public SingleHomeCardItem(String json) throws JSONException {
super(json);
}
public SingleHomeCardItem(JSONObject json) throws JSONException {
super(json.toString());
}
public String getSubtitle() {
return optString(subtitle, "");
}
public String getBadgeNumber() {
return optString(badgeNumber, "");
}
public String getBackgroundUrl() {
return optString(backgroundUrl, "");
}
public String getBackgroundGradient() {
return optString(backgroundGradient, "");
}
public String getTargetUrl() {
return optString(targetUrl, "");
}
public String getTarget() {
return optString(target, "");
}
public String getRow() {
return optString(row, "");
}
}
之后,您可以创建更复杂的类来包含SingleHomeCardItem实例列表
public class AccountData extends JSONObject {
private static final String TAG=AccountData.class.getSimpleName();
private static final String ACOUNT_CARDS="SingleHomeCardItemList";
public AccountData(String json) throws JSONException {
super(json);
}
public AccountData(JSONObject json) throws JSONException {
super(json.toString());
}
public ArrayList<SingleHomeCardItem> getAcountCards() throws JSONException {
ArrayList<SingleHomeCardItem> cardsArray = new ArrayList<SingleHomeCardItem>();
JSONArray cardsJSONArray = optJSONArray(ACOUNT_CARDS);
if(!optString(ACOUNT_CARDS).equals("null")){
for(int i=0; i<cardsJSONArray.length(); i++) {
cardsArray.add(new SingleHomeCardItem(cardsJSONArray.getString(i)));
}
}
return cardsArray;
}
所以你可以像Strings一样传递数据
public class HomeshelfFragment extends Fragment {
public static final String KEY_DATA = "KEY_DATA";
private ArrayList<SingleHomeCardItem> myCards;
public static HomeshelfFragment newInstance(String data) {
HomeshelfFragment f = new HomeshelfFragment();
Bundle b = new Bundle();
b.putString(KEY_DATA, data);
f.setArguments(b);
return f;
}
private View createView(LayoutInflater inflater, ViewGroup container, SingleHomeCardItem[] items) {
View v = inflater.inflate(<your layout>, container, false);
String dataString = this.getArguments().getString(KEY_DATA, "");
AccountData data = null;
myCards = new ArrayList<SingleHomeCardItem>();
try {
data = new AccountData(datastring);
} catch (JSONException e) {
e.printStackTrace();
}
try {
// a list with your SingleHomeCardItem instances
myCards = data.getAcountCards();
} catch (JSONException e) {
e.printStackTrace();
}
// add views to the inflated view based on models data
return v;
}
}
解决方案2
创建一个AcountCards类但是没有扩展JsonObject但是实现了Parcable接口并使用了像this这样的getParcelableExtra()
解决方案3
创建一个AcountCards类,但是没有扩展JsonObject但是实现了Serialiazible接口并使用像this这样的getSerializableExtra()