Java 当数据来自json时,不会出现片段
我在同一个视图中有两个Java 当数据来自json时,不会出现片段,java,android,json,android-fragments,Java,Android,Json,Android Fragments,我在同一个视图中有两个Fragment,Fragment在水平循环视图中检索json数据 我从MainActivity打开我的第一个片段 我可以从第一个片段打开片段,但是如果第二个片段从json检索,我就不能从第一个片段打开片段 下面的代码是我从MainActivity打开的第一个片段 主舱 public class MainFrog extends Fragment { // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds pr
Fragment
,Fragment在水平循环视图中检索json数据
我从MainActivity
打开我的第一个片段
我可以从第一个片段打开片段,但是如果第二个片段
从json
检索,我就不能从第一个片段
打开片段
下面的代码是我从MainActivity
打开的第一个片段
主舱
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;
}
repkaceFragment
方法替换为第一个布局下方的第二个布局
,
但当我用第一个布局(firstfragment)替换它时,它就成功了
当从静态数据打开时,第二个片段工作正常,但当position==0
它不起作用
CarFrog.class
有我在MainFrog.class中使用的相同代码
在我的Log
CarFrog.class
中,可以很好地检索数据,但是片段没有出现
更新:
调试后打开CarFrog
mAdapter返回空值
但是,如果我打开MainFrog
时它返回空值,但它在那里工作。日志上说如果你能上传它会有帮助。
我认为你试图从asyntask触摸UI线程是不可能的,
在这种情况下,您需要在ui线程中使用do
同样没有日志,这是一个很难猜测的问题,因为这是一个非常普遍的问题
也许可以尝试查看何时收到数据,然后何时调用此标志,一个将在UI线程上运行并将新片段上载到视图的函数,日志会说什么,如果您可以上载它,它会有所帮助。
我认为你试图从asyntask触摸UI线程是不可能的,
在这种情况下,您需要在ui线程中使用do
同样没有日志,这是一个很难猜测的问题,因为这是一个非常普遍的问题
可能会尝试查看何时收到数据,然后何时调用此标志将在UI线程上运行并将新片段上载到视图的函数声明布局项后的onCreateView
中的code>。并移除RecyclerView芯片RecyclerView=(RecyclerView)findViewById(R.id.RecyclerView)代码>来自AsyncTask
。删除onCreate
,因为片段具有onCreateView
所以添加newasynchlogin().execute()代码>在onCreateView
mRVMainFrog = (RecyclerView) inflatedMainView.findViewById(R.id.mainRecycleView);
//Execute AsyncTask here
new AsyncLogin().execute();
希望这对你有帮助。如果还有任何问题,请随时询问。调用新建AsyncLogin().execute()声明布局项后的onCreateView
中的code>。并移除RecyclerView芯片RecyclerView=(RecyclerView)findViewById(R.id.RecyclerView)代码>来自AsyncTask
。删除onCreate
,因为片段具有onCreateView
所以添加newasynchlogin().execute()代码>在onCreateView
mRVMainFrog = (RecyclerView) inflatedMainView.findViewById(R.id.mainRecycleView);
//Execute AsyncTask here
new AsyncLogin().execute();
希望这对你有帮助。如果还有任何问题,请随时询问。mRVMainFrog在哪里声明?在顶部@ZakiPathancall new AsyncLogin().execute();在声明布局项后的onCreateView中。并移除RecyclerView芯片RecyclerView=
(RecyclerView)findViewById(R.id.RecyclerView);从异步任务。试试这个@Ibrahimali在@Ibrahimali下面检查我的答案请检查更新mRVMainFrog声明在哪里?在顶部@ZakiPathancall new AsyncLogin().execute();在声明布局项后的onCreateView中。并移除RecyclerView芯片RecyclerView=
(RecyclerView)findViewById(R.id.RecyclerView);从异步任务。尝试此@Ibrahimalia检查下面的我的答案@Ibrahimalia请检查Updateyea数据接收良好,片段打开但存在逻辑错误soyea数据接收良好,片段打开但存在逻辑错误so@AndroidDeveloper你有什么错误吗?请调试并检查哪一行的主要问题是您是谁@AndroidDeveloper?这个问题不是我的问题you@AndroidDeveloper你有什么错误吗?请调试并检查哪一行的主要问题是您是谁@AndroidDeveloper?这个问题不是你问的