//Activity
public class SixthActivity extends AppCompatActivity implements SixthApiAdapter.OnItemClickListner {
RequestQueue requestQueue;
StringRequest stringRequest;
ArrayList<DataItem> list = new ArrayList<>();
AppCompatButton btn_request;
RecyclerView recyclerView;
SixthApiAdapter sixthApiAdapter;
DataItem dataItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sixth);
btn_request = findViewById(R.id.btn_request);
recyclerView = findViewById(R.id.recyclerView);
sendReqAndgetRes();
}
private void sendReqAndgetRes() {
requestQueue = Volley.newRequestQueue(SixthActivity.this);
stringRequest = new StringRequest(Request.Method.POST, Config.API_URL_Third, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
list.clear();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
Gson gson = new Gson();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
dataItem = gson.fromJson(jsonObject1.toString(), DataItem.class);
list.add(dataItem);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SixthActivity.this);
RecyclerView.LayoutManager layoutManager = linearLayoutManager;
recyclerView.setLayoutManager(layoutManager);
sixthApiAdapter = new SixthApiAdapter(SixthActivity.this, list, SixthActivity.this);
recyclerView.setAdapter(sixthApiAdapter);
} catch (JSONException jsonException) {
jsonException.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("packagename", "com.aytel.aytelallremote");
return params;
}
};
requestQueue.add(stringRequest);
}
@Override
public void onItemClick(int position) {
Toast.makeText(this, "Position : " + position, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Data: " + list.get(position).getAppName(), Toast.LENGTH_SHORT).show();
if (position == 0) {
Toast.makeText(this, "Data : " + list.get(0).getAppName(), Toast.LENGTH_SHORT).show();
}
if (position == 1) {
Toast.makeText(this, "Data : " + list.get(1).getAppName(), Toast.LENGTH_SHORT).show();
}
}
}
//Adapter
public class SixthApiAdapter extends RecyclerView.Adapter<SixthApiAdapter.ViewHolder> {
Context context;
ArrayList<DataItem> list;
DataItem dataItem;
OnItemClickListner listner;
public SixthApiAdapter(Context context, ArrayList<DataItem> list, OnItemClickListner onItemClickListner) {
this.context = context;
this.list = list;
listner = onItemClickListner;
}
@NonNull
@Override
public SixthApiAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.thirdjsonitem, parent, false);
ViewHolder viewHolder = new ViewHolder(view,listner);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull SixthApiAdapter.ViewHolder holder, int position) {
dataItem = list.get(position);
holder.appname.setText(dataItem.getAppName());
holder.packagename.setText(dataItem.getPackageName());
Glide.with(context).load(dataItem.getAppIcon()).into(holder.icon);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView icon;
TextView appname, packagename;
OnItemClickListner onItemClickListner;
public ViewHolder(@NonNull View itemView, OnItemClickListner onItemClickListner) {
super(itemView);
icon = itemView.findViewById(R.id.icon);
appname = itemView.findViewById(R.id.appname);
packagename = itemView.findViewById(R.id.packagename);
this.onItemClickListner = onItemClickListner;
icon.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onItemClickListner.onItemClick(getAdapterPosition());
}
}
public interface OnItemClickListner {
void onItemClick(int position);
}
}
// Json Data
public class DataItem{
@SerializedName("app_name")
private String appName;
@SerializedName("app_icon")
private String appIcon;
@SerializedName("admob_banner")
private String admobBanner;
@SerializedName("privacypolicy")
private Object privacypolicy;
@SerializedName("admob_appid")
private Object admobAppid;
@SerializedName("admob_inter")
private String admobInter;
@SerializedName("fb_banner")
private Object fbBanner;
@SerializedName("fb_native")
private Object fbNative;
@SerializedName("admob_native")
private Object admobNative;
@SerializedName("package_name")
private String packageName;
@SerializedName("fb_inter")
private Object fbInter;
@SerializedName("moreapps")
private Object moreapps;
public void setAppName(String appName){
this.appName = appName;
}
public String getAppName(){
return appName;
}
public void setAppIcon(String appIcon){
this.appIcon = appIcon;
}
public String getAppIcon(){
return appIcon;
}
public void setAdmobBanner(String admobBanner){
this.admobBanner = admobBanner;
}
public String getAdmobBanner(){
return admobBanner;
}
public void setPrivacypolicy(Object privacypolicy){
this.privacypolicy = privacypolicy;
}
public Object getPrivacypolicy(){
return privacypolicy;
}
public void setAdmobAppid(Object admobAppid){
this.admobAppid = admobAppid;
}
public Object getAdmobAppid(){
return admobAppid;
}
public void setAdmobInter(String admobInter){
this.admobInter = admobInter;
}
public String getAdmobInter(){
return admobInter;
}
public void setFbBanner(Object fbBanner){
this.fbBanner = fbBanner;
}
public Object getFbBanner(){
return fbBanner;
}
public void setFbNative(Object fbNative){
this.fbNative = fbNative;
}
public Object getFbNative(){
return fbNative;
}
public void setAdmobNative(Object admobNative){
this.admobNative = admobNative;
}
public Object getAdmobNative(){
return admobNative;
}
public void setPackageName(String packageName){
this.packageName = packageName;
}
public String getPackageName(){
return packageName;
}
public void setFbInter(Object fbInter){
this.fbInter = fbInter;
}
public Object getFbInter(){
return fbInter;
}
public void setMoreapps(Object moreapps){
this.moreapps = moreapps;
}
public Object getMoreapps(){
return moreapps;
}
@Override
public String toString(){
return
"DataItem{" +
"app_name = '" + appName + '\'' +
",app_icon = '" + appIcon + '\'' +
",admob_banner = '" + admobBanner + '\'' +
",privacypolicy = '" + privacypolicy + '\'' +
",admob_appid = '" + admobAppid + '\'' +
",admob_inter = '" + admobInter + '\'' +
",fb_banner = '" + fbBanner + '\'' +
",fb_native = '" + fbNative + '\'' +
",admob_native = '" + admobNative + '\'' +
",package_name = '" + packageName + '\'' +
",fb_inter = '" + fbInter + '\'' +
",moreapps = '" + moreapps + '\'' +
"}";
}
}
// URL
public static String API_URL_Third = "https://localads.fuegomasters.com/localadservice/getalladsnew.php";