Skip to content

Instantly share code, notes, and snippets.

@agusibrahim
Created July 3, 2017 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agusibrahim/308746049c5dfb1a470c70f56394b281 to your computer and use it in GitHub Desktop.
Save agusibrahim/308746049c5dfb1a470c70f56394b281 to your computer and use it in GitHub Desktop.
package com.zadi.volley;
/**
* Created by Muh. Zadi on 6/9/2017.
*/
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import android.widget.ImageSwitcher;
public class AdapterList extends RecyclerView.Adapter<AdapterList.ViewHolder> {
Context context;
ImageSwitcher imageSwitcher;
ArrayList<HashMap<String, String >> list_data;
public AdapterList(RecycleView mainActivity, ArrayList<HashMap<String, String >>list_data, ImageSwitcher imgs){
this.context = mainActivity;
this.list_data = list_data;
this.imageSwitcher=imgs;
}
@Override
public AdapterList.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_images, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(AdapterList.ViewHolder holder, final int position) {
/* Picasso.with(context.getApplicationContext())
.load("http://200.200.200.197/app_blogvolley/img/" + list_data.get(position).get("gambar"))
.error(R.drawable.no_available)
.into(holder.imghape);*/
Glide.with(context)
.load("http://192.168.43.228/app_blogvolley/img/" + list_data.get(position).get("gambar"))
.crossFade()
.placeholder(R.drawable.no_available)
.into(holder.imghape);
// holder.txthape.setText(list_data.get(position).get("merk"));
/*holder.imghape.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "satu" +list_data.get(position), Toast.LENGTH_SHORT).show();
// imageSwitcher.setImageResource(list_data.get(position));
}
});*/
HashMap<String, String> data = list_data.get(position);
}
@Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// TextView txthape;
ImageView imghape;
public ViewHolder(final View itemView) {
super(itemView);
imghape = (ImageView) itemView.findViewById(R.id.imghp);
imghape.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// Kalo dibawah ini error, atur sendiri ya. Yg penting ImageSwitcher sudah didapatkan
Glide.with(context)
.load("http://192.168.43.228/app_blogvolley/img/" + list_data.get(getAdapterPosition()).get("gambar"))
.crossFade()
.placeholder(R.drawable.no_available)
.into((ImageView)imageSwitcher.getCurrentView());
}
});
}
}
}
package com.zadi.volley;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import android.widget.ImageSwitcher;
public class RecycleView extends AppCompatActivity {
private ProgressDialog pDialog;
private ImageView imghp;
private TextView txtmerk, txttipe, txtketerangan;
private RecyclerView lvhape;
private RequestQueue requestQueue;
private StringRequest stringRequest;
private ImageSwitcher imgs;
ArrayList<HashMap<String, String>> list_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url = "http://192.168.43.228/app_blogvolley/getdata.php";
lvhape = (RecyclerView)findViewById(R.id.lvhape);
imgs=(ImageSwitcher) findViewById(R.id.switcher);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.HORIZONTAL);
lvhape.setLayoutManager(llm);
/* imghp = (ImageView)findViewById(R.id.imghp);
txtmerk = (TextView)findViewById(R.id.txtmerk);
txttipe = (TextView)findViewById(R.id.txttipe);
txtketerangan = (TextView)findViewById(R.id.txtketerangan);*/
requestQueue = Volley.newRequestQueue(RecycleView.this);
list_data = new ArrayList<HashMap<String, String>>();
stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){
@Override
public void onResponse(String response) {
Log.d("response ", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("handphone");
for (int a = 0; a < jsonArray.length(); a++){
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", json.getString("idhp"));
map.put("merk", json.getString("merk"));
map.put("tipe", json.getString("tipe"));
map.put("gambar", json.getString("gambar"));
map.put("keterangan", json.getString("keterangan"));
list_data.add(map);
AdapterList adapter = new AdapterList(RecycleView.this, list_data, imgs);
lvhape.setAdapter(adapter);
}
}catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RecycleView.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment