Commit a9c71840 authored by Inès EL HADRI's avatar Inès EL HADRI 💤

fix add annonce on home page

parent 9d8f582b
...@@ -11,11 +11,13 @@ import androidx.recyclerview.widget.RecyclerView; ...@@ -11,11 +11,13 @@ import androidx.recyclerview.widget.RecyclerView;
import com.example.tpleboncoin.R; import com.example.tpleboncoin.R;
import com.example.tpleboncoin.models.Annonce; import com.example.tpleboncoin.models.Annonce;
import java.util.ArrayList;
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter"; private static final String TAG = "CustomAdapter";
private Annonce[] mDataSet; private ArrayList<Annonce> mDataSet;
private boolean mIsGrid; private boolean mIsGrid;
/** /**
...@@ -53,7 +55,7 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { ...@@ -53,7 +55,7 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
* @param dataSet String[] containing the data to populate views to be used by RecyclerView. * @param dataSet String[] containing the data to populate views to be used by RecyclerView.
* @param isGrid boolean = le layout actuel (grille ou linéaire) * @param isGrid boolean = le layout actuel (grille ou linéaire)
*/ */
public HomeAdapter(Annonce[] dataSet, boolean isGrid) { public HomeAdapter(ArrayList<Annonce> dataSet, boolean isGrid) {
mDataSet = dataSet; mDataSet = dataSet;
mIsGrid = isGrid; mIsGrid = isGrid;
} }
...@@ -82,14 +84,14 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { ...@@ -82,14 +84,14 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
// Get element from dataset at this position and replace the contents of the view // Get element from dataset at this position and replace the contents of the view
// with that element // with that element
viewHolder.getTitreTextView().setText(mDataSet[position].getTitre()); viewHolder.getTitreTextView().setText(mDataSet.get(position).getTitre());
viewHolder.getAdresseTextView().setText(mDataSet[position].getAdresse()); viewHolder.getAdresseTextView().setText(mDataSet.get(position).getAdresse());
} }
// Return the size of dataset (invoked by the layout manager) // Return the size of dataset (invoked by the layout manager)
@Override @Override
public int getItemCount() { public int getItemCount() {
return mDataSet.length; return mDataSet.toArray().length;
} }
} }
......
...@@ -19,6 +19,9 @@ import com.example.tpleboncoin.R; ...@@ -19,6 +19,9 @@ import com.example.tpleboncoin.R;
import com.example.tpleboncoin.models.Annonce; import com.example.tpleboncoin.models.Annonce;
import com.example.tpleboncoin.databinding.FragmentHomeBinding; import com.example.tpleboncoin.databinding.FragmentHomeBinding;
import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
private static final String TAG = "RecyclerViewFragment"; private static final String TAG = "RecyclerViewFragment";
...@@ -39,7 +42,7 @@ public class HomeFragment extends Fragment { ...@@ -39,7 +42,7 @@ public class HomeFragment extends Fragment {
protected RecyclerView mRecyclerView; protected RecyclerView mRecyclerView;
protected HomeAdapter mAdapter; protected HomeAdapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager; protected RecyclerView.LayoutManager mLayoutManager;
protected Annonce[] mDataset; protected ArrayList<Annonce> mDataset;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
...@@ -72,6 +75,14 @@ public class HomeFragment extends Fragment { ...@@ -72,6 +75,14 @@ public class HomeFragment extends Fragment {
} }
setRecyclerViewLayoutManager(mCurrentLayoutManagerType); setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
// On récupère l'annonce qui vient d'être créée s'il y en a une
if(getArguments() != null && getArguments().getParcelable("nouvelleAnnonce") != null){
Annonce newAnnonce = getArguments().getParcelable("nouvelleAnnonce");
Log.d("HomeFragment", newAnnonce.getTitre());
mDataset.add(newAnnonce);
}
// adapter pour gérer le visu de l'annonce // adapter pour gérer le visu de l'annonce
mAdapter = new HomeAdapter(mDataset, false); mAdapter = new HomeAdapter(mDataset, false);
mRecyclerView.setAdapter(mAdapter); mRecyclerView.setAdapter(mAdapter);
...@@ -93,12 +104,6 @@ public class HomeFragment extends Fragment { ...@@ -93,12 +104,6 @@ public class HomeFragment extends Fragment {
} }
}); });
// On récupère l'annonce qui vient d'être créée s'il y en a une
if(getArguments() != null && getArguments().getParcelable("nouvelleAnnonce") != null){
Annonce newAnnonce = getArguments().getParcelable("nouvelleAnnonce");
Log.d("HomeFragment", newAnnonce.getTitre());
}
return rootView; return rootView;
} }
...@@ -155,10 +160,10 @@ public class HomeFragment extends Fragment { ...@@ -155,10 +160,10 @@ public class HomeFragment extends Fragment {
* from a local content provider or remote server. * from a local content provider or remote server.
*/ */
private void initDataset() { private void initDataset() {
mDataset = new Annonce[DATASET_COUNT]; mDataset = new ArrayList<Annonce>();
for (int i = 0; i < DATASET_COUNT; i++) { for (int i = 0; i < DATASET_COUNT; i++) {
Annonce annonce = new Annonce("Titre " + i, "Adresse " + i, 0, 12.99); Annonce annonce = new Annonce("Titre " + i, "Adresse " + i, 0, 12.99);
mDataset[i] = annonce; mDataset.add(annonce);
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment