gpt4 book ai didi

java - 当数据来自json时, fragment 不出现

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:14 25 4
gpt4 key购买 nike

我在同一个 View 中有两个 Fragment,Fragment 在水平回收 View 中检索 json 数据。

我从 MainActivity 打开我的第一个 fragment 。

并且我可以从第一个 fragment 打开 fragment ,但如果第二个 fragment json检索,我无法从第一个 fragment 打开 fragment 。

下面的代码位于我从 MainActivity 打开的第一个 fragment 中。

MainFrog.class

public class MainFrog extends Fragment {

// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
private static final String TAG = "MainFrog";
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private RecyclerView mRVMainFrog;
private AdapterMain mAdapter;
String url = "URL";
List<String> data = new ArrayList<>();
JSONParser jParser = new JSONParser();
JSONArray sectionArr = null;
String sections = "sections";
String sectionName = "section_name";
Context context;
TextView searchOnSelect;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//Make call to AsyncTask
new AsyncLogin().execute();
}


private class AsyncLogin extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url, "GET", params);

// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());

try {

sectionArr = json.getJSONArray(sections);

// looping through All Items
Log.e(TAG, "doInBackground: " + sectionArr);
for (int i = 0; i < sectionArr.length(); i++) {
JSONObject c = sectionArr.getJSONObject(i);

Log.e(TAG, "doInBackground: " + c);
// Storing each json item in variable
String name = c.getString(sectionName);
Log.e(TAG, "doInBackground: " + name);
//HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
//map.put("", name);

// adding HashList to ArrayList
data.add(String.valueOf(name));

}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}


@Override
protected void onPostExecute(String result) {

//
//`RecyclerView chipRecyclerView =`
// (RecyclerView)findViewById(R.id.recyclerView);
mAdapter = new AdapterMain(getActivity(), data);
// updating listview
//mRVFishPrice.setAdapter(mAdapter);

// Setup and Handover data to recyclerview
LinearLayoutManager layoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);

mRVMainFrog.setLayoutManager(layoutManager);
mRVMainFrog.setAdapter(mAdapter);
}
}


Fragment fragment;

public MainFrog() {


}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View inflatedMainView = inflater.inflate(R.layout.mainfrog, null);
View infaltedMain = inflater.inflate(R.layout.activity_main, null);
View mainLayout = infaltedMain.findViewById(R.id.detailsContent);
mRVMainFrog = (RecyclerView) inflatedMainView.findViewById(R.id.mainRecycleView);
searchOnSelect = (TextView) mainLayout.findViewById(R.id.searchOnSelect);
ItemClickSupport.addTo(mRVMainFrog).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
// do it
if (position == 0) {
fragment = new CarFrog();
replaceFragment(fragment);
searchOnSelect.setVisibility(View.VISIBLE);
} else if (position == 1) {
fragment = new BuildFrog();
replaceFragment(fragment);
searchOnSelect.setVisibility(View.VISIBLE);
} else if (position == 2) {
fragment = new DeviceFrog();
replaceFragment(fragment);
searchOnSelect.setVisibility(View.VISIBLE);
} else if (position == 3) {
fragment = new DeviceFrog();
replaceFragment(fragment);
searchOnSelect.setVisibility(View.VISIBLE);
}
}
});

return inflatedMainView;
}

我的replaceFragment看起来像:

public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.carLayout, someFragment);
transaction.addToBackStack("abc");
transaction.commit();
}

repkaceFragment 方法替换为第一个 layout 下面的第二个 layout,但是当我用第一个布局(firstfragment)替换它时,它就可以工作了

当从静态数据打开时,第二个 fragment 工作完美,但当 position == 0它不起作用。

CarFrog.class 

与我在MainFrog.class中使用的代码相同

在我的日志中,CarFrog.class可以很好地检索数据,但 fragment 没有出现。

更新:

打开 CarFrog mAdapter 调试后返回 null。?

recycleView return null too

但在某些情况下,当我打开 MainFrog 时它返回 null,但它在那里工作。

最佳答案

日志说了什么,如果您可以上传它会有帮助。我认为你试图从异步任务中触摸 UI 线程,但这是不可能的,在这种情况下,您需要用 do in ui 线程包围自己。

同样,如果没有日志,就很难猜测,因为这是一个非常普遍的问题。

也许尝试查看何时收到数据,然后何时调用此标志,该函数将在 UI 线程上运行并将新 fragment 上传到 View

关于java - 当数据来自json时, fragment 不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42414922/

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