【发布时间】:2018-04-13 13:32:16
【问题描述】:
解析后如何将数组列表返回片段以设置为RecyclerView。
我尝试这样做,但是当我调用 api 时,它运行后台线程,直到我从后台获得我创建的片段的 ArrayList 并将 null 数组设置为回收器适配器。
然后,我尝试了这种方法,但我认为这是一种错误的方法。请指导我 -
如何在解析数据后在Fragment 中设置适配器并将ArrayList 发送回片段并设置为RecyclerView adapter?
这是我的代码片段代码
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view=inflater.inflate(R.layout.first_fragment,null);
recyclerView=(RecyclerView)view.findViewById(R.id.mFirstRecyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
mswipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swipelayoutm);
ParseDataClass mparser = new ParseDataClass(getActivity(),recyclerView,mswipeRefreshLayout);
mparser.execute("http://192.168.8.100/fetchtext.php", "1");
recyclerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollX == 0) {
mswipeRefreshLayout.setEnabled(true);
} else mswipeRefreshLayout.setEnabled(false);
}
});
return view;
}
@Override
public String toString() {
return "FirstFragment";
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.rightimageitem).setVisible(false);
super.onPrepareOptionsMenu(menu);
}
}
这是我的解析器类
public ParseDataClass(Context context,RecyclerView recyclerView,SwipeRefreshLayout swipeRefreshLayout) {
this.context = context;
progressDialog = new ProgressDialog(this.context);
progressDialog.setCancelable(false);
progressDialog.setMessage("Retrieving data...");
progressDialog.setTitle("Please wait");
progressDialog.setIndeterminate(true);
arrayList = new ArrayList<DataStored>();
getArraylist = new ArrayList<DataStored>();
this.recyclerView=recyclerView;
this.swipeRefreshLayout=swipeRefreshLayout;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
private static final String TAG = "Parser";
@Override
protected String doInBackground(String... strings) {
URL url = null;
int arryLength = strings.length;
HttpURLConnection httpURLConnection = null;
try {
url = new URL(strings[0]);
String catId = strings[1];
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("catId", "UTF-8") + "=" + URLEncoder.encode(catId, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
// RestaurantData restaurantData;
if (s != null) {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
String branchname = jsonObject.getString("brancname");
String branchaddress = jsonObject.getString("branchaddres");
String brandname = jsonObject.getString("brandsNae");
String brandlogo = jsonObject.getString("brndlogo");
String branchlogo = jsonObject.getString("brnchlogo");
String discAmount = jsonObject.getString("distAmount");
dataStored = new DataStored(branchname, branchaddress, brandname, brandlogo, branchlogo, discAmount);
arrayList.add(dataStored);
}
myAdapter = new MyRecyclerAdapter(context, arrayList, swipeRefreshLayout);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
recyclerView.setAdapter(myAdapter);
} else {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
connected = true;
} else {
connected = false;
}
if (connected == false) {
Toast.makeText(context, "Null Data", Toast.LENGTH_LONG).show();
recyclerView.setAdapter(null);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "Error in Parsing =" + e, Toast.LENGTH_LONG).show();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
现在我在解析器类中设置回收器视图而不是在片段中我的问题是.. 解析后如何将 ArrayList 返回到片段,然后在片段中设置到 recylerview 适配器。
【问题讨论】:
标签: android android-fragments android-recyclerview android-asynctask background