Android parse json to gridview in aide ide

aide ide tutorials


Tutorials on how parse json files into gridview using android volley library in aide ide. In this tutorial we use two important android library one is android volley and second is picasso.

This two library is important, the volley library is to parse json files and the picasso library is for the image display.

Now lets start coding, first goto your "build.gradle" and add this two librarys
compile 'com.android.volley:volley:1.1.0-rc1'
compile 'com.squareup.picasso:picasso:2.4.0'

Next goto your "AndroidManifest.xml" and add two permission:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

In your "MainActivity.java" paste this code [This a full code of MainActivity.java]
import android.app.*;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.net.*;
import android.os.*;
import android.support.v4.view.*;
import android.support.v4.widget.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.text.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import com.android.volley.*;
import com.android.volley.toolbox.*;
import java.io.*;
import java.util.*;
import org.json.*;

import android.app.AlertDialog;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;

public class MainActivity extends Activity {


final Context context=this;

ImageView im11,im22;
Button abt1,abt2,erb;
TextView tt1;

String HI ="https://malitanyo.website/mydata/aide/community.php"; 
    private ArrayList<communityData>list_data;
private GridView gridView;
communityAdapter adapter; 

LinearLayout errh,prggg;
ProgressBar sprg;

@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.main);

gridView=(GridView)findViewById(R.id.grid_result);
        list_data=new ArrayList<>();
getDataUpdate();

    }
private void getDataUpdate() {

StringRequest stringRequest =new StringRequest(Request.Method.GET, HI, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
    //Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
try {
String sttt= response.substring(0, response.length() - 3);//anime title
String fff = sttt+ "]}";

JSONObject jsonObject=new JSONObject(fff);
JSONArray array=jsonObject.getJSONArray("data");
for (int i=0; i<array.length(); i++){
JSONObject ob=array.getJSONObject(i);
communityData listData=new communityData( ob.getString("pt_title"), ob.getString("pt_date"), ob.getString("pt_desc"), ob.getString("pt_icon"), ob.getString("pt_track"),ob.getString("pt_author"));
list_data.add(listData);

}
adapter=new communityAdapter(getApplicationContext(),R.layout.community_item,list_data);
gridView.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"No tutorial found!",Toast.LENGTH_LONG).show();

}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Connection Error",Toast.LENGTH_LONG).show();

}
});
        RequestQueue requestQueue= Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }


}

Next create class data and name into "communityData.java"
class communityData {
private String title;
    private String date;
    private String desc;
    private String icon;
    private String track;
    private String author;
    public communityData(String title,String date, String desc, String icon, String track, String author) {

this.title = title;
this.date = date;
this.desc = desc;
this.icon = icon;
this.track = track;
this.author = author;
    }

public void setOnItemClickListener(Object onItemClick)
{
// TODO: Implement this method
}
    public String getDtitle() {
        return title;
    }

public String getDdate() {
        return date;
    }
public String getDdesc() {
        return desc;
    }
public String getDicon() {
        return icon;
    }
public String getDtrack() {
        return track;
    }
public String getDauthor() {
        return author;
    }

}

Next create new adapter and name into "communityAdapter.java"
import android.app.*;
import android.content.*;
import android.support.annotation.*;
import android.support.design.*;
import android.text.*;
import android.view.*;
import android.widget.*;
import com.squareup.picasso.*;
import java.util.*;

import android.support.design.R;

public class communityAdapter extends ArrayAdapter<communityData> {
    ArrayList<communityData> listdata;
    Context context;
    int resource;
    public communityAdapter(@NonNull Context context, int resource, @NonNull ArrayList<communityData> listdata) {
        super(context, resource, listdata);
        this.listdata=listdata;
        this.context=context;
        this.resource=resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView==null){
            LayoutInflater layoutInflater=(LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView=layoutInflater.inflate(R.layout.community_item,null,true);
        }
        final communityData listdata=getItem(position);
        final TextView txx5=(TextView)convertView.findViewById(R.id.com_title);
        final TextView txx6=(TextView)convertView.findViewById(R.id.com_date);
        final TextView txx7=(TextView)convertView.findViewById(R.id.com_desc);
        final ImageView img=(ImageView)convertView.findViewById(R.id.com_icon);

String dpic = listdata.getDicon();
String newString = dpic.replace("/", "\/");

Picasso.with(context)
.load(newString)
.into(img);
img.setVisibility(View.VISIBLE);

txx5.setText(listdata.getDtitle());
txx6.setText(listdata.getDdate());
txx7.setText(listdata.getDdesc());

        return convertView;
    }
}

In your "main.xml" add this code
<GridView
android:id="@+id/grid_result"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="1"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"
android:paddingLeft="5dp"
android:scrollbars="none"
android:paddingRight="5dp"
android:layout_margin="2dp"
android:layout_weight="1.0"/>

In your layout folder located at "res/layout" create new xml files and name to "community_item.xml" and paste this code below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="vertical"
android:background="@drawable/main_post_bg">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_margin="3dp">

<ImageView
android:id="@+id/com_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_margin="5dp"
android:src="@drawable/ic_launcher"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="vertical"
android:layout_marginLeft="41dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chrollo"
android:maxLines="3"
android:id="@+id/com_title"
android:textSize="17sp"
android:textColor="#626262"
android:gravity="left|center"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/com_author"
android:text="Medium Text"/>

</LinearLayout>

</RelativeLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:maxLines="6"
android:id="@+id/com_desc"
android:textSize="20sp"
android:textColor="#3B3B3B"
android:gravity="left|center"
android:layout_marginTop="14dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/com_date"
android:text="Medium Text"/>
</LinearLayout>

</LinearLayout>

Done..

Comments

Popular posts from this blog

Implement in-app purchase in aide ide

Implement google admob interstitial ads in aide ide

Implement a-ads on blogger or website

Android play .gif file on imageview in aide ide

Aide ide tutorial - Add line graph


Malitanyo Developer blog & tutorial 2020
Refund Policy    Facebook Page    Google Play

Blogger.com