I have a ListView
where I displayed data from a database using mysql and json but by doing some research I found the RecyclerView
widget is a more advanced and flexible version of ListView
. This widget is a container for displaying large data sets and that with the RecyclerView
I can have a result like this. So how I can correct my code? How can I extract an image from a database and fetch it into the RecyclerView
Here's how I've done it in ListView
private void showarticles(String json){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Description");
HashMap<String,String> articles= new HashMap<>();
articles.put("name",name);
articles.put("ref",ref);
list.add(articles);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new SimpleAdapter(
getActivity(), list, R.layout.list_row2,
new String[]{"name","ref"},
new int[]{R.id.name, R.id.description});
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(), "Téléchargement", "Veuillez patientez...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showarticles(s);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
Update
public class recyclerFragment extends Fragment {
private RecyclerView mRecyclerView;
ListAdapter adapter;
private String JSON_STRING;
public static final String URL = "http://aaaa.com/Back/getArticles.php";
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.recycler_fragment, container, false);
mRecyclerView = (RecyclerView) rootview.findViewById(R.id.my_list);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(10));
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(null);
mRecyclerView.setAdapter(mAdapter);
getJSON();
return rootview;
}
private void showarticles(String json){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Description");
HashMap<String,String> articles= new HashMap<>();
articles.put("name",name);
articles.put("ref",ref);
list.add(articles);
}
} catch (JSONException e) {
e.printStackTrace();
}
mAdapter = new SimpleAdapter(
getActivity(), list, R.layout.list_row2,
new String[]{"name","ref"},
new int[]{R.id.nom, R.id.email2});
mRecyclerView.setAdapter(mAdapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(), "Téléchargement", "Veuillez patientez...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showEmployee(s);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewProduit.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String tid = map.get("ref").toString();
intent.putExtra("ref", tid);
startActivity(intent);
}
}
Error:(31, 27) error: cannot find symbol variable myArrayList
Error:(76, 13) error: cannot find symbol variable myArrayList
Error:(77, 20) error: cannot find symbol variable myArrayList
Error:(41, 45) error: cannot find symbol class SpacesItemDecoration
Error:(44, 20) error: constructor MyAdapter in class MyAdapter cannot be applied to given types;
required: no arguments
found: <null>
reason: actual and formal argument lists differ in length
Error:(74, 20) error: incompatible types: SimpleAdapter cannot be converted to MyAdapter
Error:(106, 5) error: method does not override or implement a method from a supertype
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.