see all post about this and understand in all cases people explain about pass data from adapter to fragment with interface and i use that but my question is how can pass data from fragment to adapter ? and how can pass value of data from those?
this is my adapter :
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private Context context;
List<jsonContent> jcontent;
private AdapterCallback callback;
private String etpass;
public DataAdapter(Context context, List<jsonContent> jcontent, AdapterCallback callback) {
this.context=context;
this.jcontent=jcontent;
this.callback=callback;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view ;
if(i == R.layout.card_row) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
}else {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.button_card, viewGroup, false);
}
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
if(i == jcontent.size()) {
viewHolder.buttonnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
jcontent.removeAll(jcontent);
callback.MethodCallbacknext();
}
});
viewHolder.buttonprev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jcontent.removeAll(jcontent);
callback.MethodCallbackprev();
}
});
viewHolder.go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etpass=String.valueOf(viewHolder.editText.getText());
maghalat f1 = new maghalat ();
Bundle args1 = new Bundle();
args1.putString("sag",etpass);
f1.setArguments(args1);
callback.MethodCallbackgo();
}
});
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
viewHolder.pages.setText(Integer.toString(jcontent.get(i - 1).pages));
}
else {
viewHolder.title.setText(jcontent.get(i).title);
viewHolder.title.setTypeface(viewHolder.title.getTypeface(), Typeface.BOLD);
Picasso.with(context).load(jcontent.get(i).imgurl).resize(300, 400).into(viewHolder.imageView);
}
}
@Override
public int getItemCount() {
return jcontent.size()+1;
}
@Override
public int getItemViewType(int position) {
return (position == jcontent.size()) ? R.layout.button_card : R.layout.card_row;
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView title,pages,current;
private ImageView imageView;
private Button buttonnext,buttonprev,go;
private CardView cardView;
private EditText editText;
public ViewHolder(final View view) {
super(view);
title = (TextView)view.findViewById(R.id.post_title);
imageView=(ImageView)view.findViewById(R.id.img);
buttonnext =(Button)view.findViewById(R.id.next);
buttonprev=(Button)view.findViewById(R.id.prev);
go=(Button)view.findViewById(R.id.go);
editText=(EditText)view.findViewById(R.id.et_go_page);
cardView=(CardView)view.findViewById(R.id.cvv);
pages=(TextView)view.findViewById(R.id.number_pages);
current=(TextView)view.findViewById(R.id.current);
}
}
}
this is my interface :
public interface AdapterCallback {
void MethodCallbackgo();
void MethodCallbacknext();
void MethodCallbackprev();
}
and this is my fragment :
public class maghalat extends Fragment {
private View myFragmentView;
private RecyclerView recyclerView;
private DataAdapter adapter;
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
List<jsonContent> listcontent=new ArrayList<>();
public int dog=1;
public String url = "http://memaraneha.ir/category/%d9%85%d9%82%d8%a7%d9%84%d8%a7%d8%aa/page/"+String.valueOf(dog)+"/?json=get_posts";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.maghalat, container, false);
if(isNetworkConnected()) {
asyncRun();
}else
{
Toast.makeText(getActivity().getApplicationContext(), "دستگاه شما به اینترنت متصل نیست!", Toast.LENGTH_LONG).show();
}
return myFragmentView;
}
public void asyncRun(){
new GetContacts().execute();
}
public class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
int id=jsonObj.getInt("pages");
JSONArray posts = jsonObj.getJSONArray("posts");
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
jsonContent jsonContent=new jsonContent();
jsonContent.title=c.getString("title");
//img
JSONObject post_img=c.getJSONObject("thumbnail_images");
for (int j=0;j<post_img.length();j++)
{
JSONObject v=post_img.getJSONObject("mom-portfolio-two");
jsonContent.imgurl=v.getString("url");
}
jsonContent.pages=id;
listcontent.add(jsonContent);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
recyclerView=(RecyclerView)myFragmentView.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
adapter=new DataAdapter(getActivity(), listcontent, new AdapterCallback() {
@Override
public void MethodCallbackgo() {
String gg;
Bundle extras = getArguments();
gg = extras.getString("sag");
dog= Integer.parseInt(gg);
url = "http://memaraneha.ir/category/%d9%85%d9%82%d8%a7%d9%84%d8%a7%d8%aa/page/"+String.valueOf(dog)+"/?json=get_posts";
new GetContacts().execute();
}
@Override
public void MethodCallbacknext() {
dog+=1;
url = "http://memaraneha.ir/category/%d9%85%d9%82%d8%a7%d9%84%d8%a7%d8%aa/page/"+String.valueOf(dog)+"/?json=get_posts";
new GetContacts().execute();
}
@Override
public void MethodCallbackprev() {
if (dog!=1)
{
dog-=1;
url = "http://memaraneha.ir/category/%d9%85%d9%82%d8%a7%d9%84%d8%a7%d8%aa/page/"+String.valueOf(dog)+"/?json=get_posts";
new GetContacts().execute();
}else{
Toast.makeText(getActivity().getApplicationContext(),"این اولین صفحه است",Toast.LENGTH_SHORT).show();
}
}
});
recyclerView.setAdapter(adapter);
}
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else
return true;
}
}
want inside adapter in go.setOnClickListener
get edit text value and pass it to fragment in public void MethodCallbackgo() {
but didnt work . and another question is how can send dog variable value inside fragment to adapter ?
If it understood correctly, you want to pass data from the callback method call to the interface implementation.
Use parameters
void MethodCallbackgo(String data);
Pass it back
etpass=String.valueOf(viewHolder.editText.getText());
callback.MethodCallbackgo(etpass);
Also: suggestion rewrite the AsyncTask to take a URL as a parameter. The URL does not need to be a member variable of your Fragment
@Override
public void MethodCallbackgo(String data) {
// here you have data
//use it ...
new GetContacts().execute(url); // pass url here, for example
}
This does nothing but create a brand new Fragment which is never displayed
maghalat f1 = new maghalat ();
Bundle args1 = new Bundle();
args1.putString("sag",etpass);
f1.setArguments(args1);
Personally, I don't think the adapter should control the page number, though. The Fragment (or just some other class) should contain the previous/next/go button actions and update the adapter accordingly. You've given" too much responsibility" to a class that's only purpose is to display data within a list.