gpt4 book ai didi

java - 以编程方式添加的元素未出现在 fragment 中

转载 作者:行者123 更新时间:2023-11-29 19:44:25 25 4
gpt4 key购买 nike

当我的 fragment 被创建时它调用

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

));
}
}

出于某种原因,当应用程序启动时,该 fragment 是空白的,就好像没有添加任何内容一样,我不明白为什么。我已经尝试将线性布局添加到 fragment xml 并通过它的 ID 获取它,但是我得到了一个空引用错误,即使我在程序的其他地方的另一个 fragment 中这样做并且它有效。

最佳答案

看起来您正确地将 View 添加到您创建的线性布局中,但我没有看到您将新创建的布局添加到 fragment View 中的位置。

这是 documentation和一个 example

摘自示例:

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 方法。

关于java - 以编程方式添加的元素未出现在 fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com