创建片段后,调用
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout=inflater.inflate(R.layout.fragment_item_fragment1, container, false);
populatePage();
return layout;
}
并在其中调用populatepage()方法。在该方法中,调用web服务。我已经验证数据退出并且所有内容都运行正常。
public void populatePage(){
String globalKey = "key";
String url = "";
//shared preferences
SharedPreferences settings = getActivity().getSharedPreferences(sharedPreferences, 0);
final String SESSION_KEY = settings.getString("SESSION_KEY", "nothing is here");
String FILE_KEY = settings.getString("FILE_KEY", "");
String page = settings.getString("PAGE", "Part");
final String group = settings.getString("FRAG_1", "");
String record = settings.getString("RECORD", "");
//BUILD THE URL WE NEED
url = "https://url.com/"
//loading page
//Declare the headers and add the pairs
Headers header = new Headers();
header.add("X-SESSION-KEY", SESSION_KEY);
header.add("X-GLOBAL-KEY", globalKey);
//Layout for building upon
final LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
$.ajax(new AjaxOptions().url(url)
.type("GET")
.dataType("json")
.headers(header)
.contentType("application/json")
.context(getActivity()).success(new Function() {
@Override
public void invoke($ droidQuery, Object... params) {
JSONObject json = (JSONObject) params[0];
try {
Boolean session = json.getBoolean("valid");
if (session) {
JSONArray recordsArray = json.getJSONArray("field");
String groupName = ""; //we compare group to this before adding it as a preference since we dont want dupes
for (int i = 0; i < recordsArray.length(); i++) {
JSONObject record = recordsArray.getJSONObject(i);
//BIG MESSY IF STATEMENT TO PARSE DATA! HOORAY!
if (record.getString("group").equals(group)) {
if (record.getString("valueType").equals("Text")) {
TextView label = new TextView(getActivity());
label.setText(record.getString("label"));
label.setPadding(20, 20, 20, 0);
label.setTextSize(20);
label.setTypeface(null, Typeface.BOLD);
label.setTextColor(Color.BLACK);
ll.addView(label);
TextView value = new TextView(getActivity());
value.setPadding(20, 0, 20, 20);
value.setTextSize(17);
value.setTextColor(Color.BLACK);
value.setText(record.getString("value"));
ll.addView(value);
} else if (record.getString("valueType").equals("Image")) {
TextView label = new TextView(getActivity());
label.setText(record.getString("label"));
label.setPadding(20, 20, 20, 20);
label.setTextSize(20);
label.setTypeface(null, Typeface.BOLD);
label.setTextColor(Color.BLACK);
ll.addView(label);
byte[] decodedString = Base64.decode(record.getString("value"), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ImageView image = new ImageView(getActivity());
image.setImageBitmap(decodedByte);
image.setScaleType(ImageView.ScaleType.FIT_START);
}
}
}
progressDialog.dismiss();
progressDialog.cancel();
} else {
droidQuery.toast("Session Expired", Toast.LENGTH_LONG);
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
}
} catch (JSONException e) {
droidQuery.toast(e.toString(), Toast.LENGTH_SHORT);
}
}
}
).
error(new Function() {
@Override
public void invoke($ droidQuery, Object... params) {
Log.e("$", "broke good");
//DELETE THIS
Context context = getActivity();
CharSequence text = "Could not connect to Server";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
));
}
}
由于某些原因,当应用程序启动时,片段是空白的,好像什么都没有添加,我无法找出原因。我已经尝试将线性布局添加到片段xml并通过它的ID获取它但是我得到一个空引用错误,即使我在程序中的其他片段中执行此操作并且它仍然有效。
答案 0 :(得分:2)
您好像正确地将视图添加到您已创建的线性布局中,但我没有看到您将新创建的布局添加到片段中的位置& #39; s视图。
取自示例:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
根据您的具体情况进行修改:
populatePage(layout);
public void populatePage(View v){
...other code here...
RelativeLayout relativeLayout = (RelativeLayout)v. findViewById(R.id.id_that_exists_in_your_xml);
relativeLayout.addView(ll, 0, new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
}
你是对的,关于你最近的编辑,我犯了一个错误。获取XML文件中的基本布局对象(相对/线性/帧布局等),然后在其上调用addView方法。
答案 1 :(得分:0)
从示例中,您提供了刚刚创建的ll
对象,但从未添加到片段中。您可以从LinearLayout
函数返回populatePage()
对象,并将其添加到onCreateView
中的片段视图中。