Issue
How can I get array list back to fragment after parsing to set into RecyclerView
.
I tried to do that but when I call api it run background thread, till I get ArrayList
from background my fragment created and it set null
array to recycler adapter.
Then, I tried this way its working but I think it is a wrong approach. Please guide me -
How can I set adapter in Fragment
after parsing data and send ArrayList
back to fragment and set to RecyclerView
adapter
?
Here is my code fragment code
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);
}
}
And Here it is my parser class
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();
}
}
}
Now I am setting recycler view in parser class not in fragment my question is .. how can I get ArrayList back to fragment after parsing then set into recylerview adapter in fragment.
Solution
How can I set adapter in Fragment after parsing data and send ArrayList back to fragment and set to RecyclerView adapter?
Simple,
Step 1: Create an interface
public interface OnDataRetrievalCallback {
void onDataRetrieval(ArrayList<DataStored> dataSet);
}
Step 2: Create interface instance and setter in your Parser class
public class ParseDataClass extends AsyncTask<...> {
....
...
arrayList = new ArrayList<DataStored>();//Your arrayList to be filled
private OnDataRetrievalCallback onDataRetrievalCallback;
public void setOnDataRetrievalCallback(OnDataRetrievalCallback callback) {
this.onDataRetrievalCallback = callback;
}
// You can send the data back to Fragment via onDataRetrievalCallback
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// After parsing and filling up the data into arrayList
onDataRetrievalCallback.onDataRetrieval(arrayList); //arrayList to be sent
}
}
Step 3: set DataRetrievalCallback from your Fragment
public class MyFragment extends Fragment {
@Override
onCreateView(.....) {
ParseDataClass mparser = new ParseDataClass(getActivity(),recyclerView,mswipeRefreshLayout);
mparser.setOnDataRetrievalCallback(new OnDataRetrievalCallback{
public void onDataRetrieval(ArrayList<DataStored> dataSet) {
//dataSet is your arrayList, use it to set the adapter
}
});
mparser.execute("http://192.168.8.100/fetchtext.php", "1");
}
}
Answered By - Paresh P.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.