Wednesday, August 3, 2016

Data call in list view through JSON in android

package com.veer.myapplication;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.veer.myapplication.Utils.JSONParser;


public class Clientlist extends Activity {
    ListView list;
    TextView ver;
    TextView name;
    TextView api;
    Button Btngetdata;
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

    //URL to get JSON Array    private static String url = "http://veggies24x7.com/API/dispatch.php?dsa=Amin";

    //JSON Node Names    private static final String TAG_OS = "dispach";
    private static final String TAG_VER = "sale_code";
    private static final String TAG_NAME = "sale_date";
    private static final String TAG_API = "username";

    JSONArray android = null;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_clientlist);
        oslist = new ArrayList<HashMap<String, String>>();

        Btngetdata = (Button)findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {

            @Override            public void onClick(View view) {
                new JSONParse().execute();

            }
        });

    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        @Override        protected void onPreExecute() {
            super.onPreExecute();
            ver = (TextView)findViewById(R.id.vers);
            name = (TextView)findViewById(R.id.name);
            api = (TextView)findViewById(R.id.api);
            pDialog = new ProgressDialog(Clientlist.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override        protected JSONObject doInBackground(String... args) {


            JSONParser jParser = new JSONParser();

            // Getting JSON from URL            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }
        @Override        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            try {
                // Getting JSON Array from URL                android = json.getJSONArray(TAG_OS);
                for(int i = 0; i < android.length(); i++){
                    JSONObject c = android.getJSONObject(i);

                    // Storing  JSON item in a Variable                    String ver = c.getString(TAG_VER);
                    String name = c.getString(TAG_NAME);
                    String api = c.getString(TAG_API);

                    // Adding value HashMap key => value
                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_VER, ver);
                    map.put(TAG_NAME, name);
                    map.put(TAG_API, api);

                    oslist.add(map);
                    list=(ListView)findViewById(R.id.list);

                    ListAdapter adapter = new SimpleAdapter(Clientlist.this, oslist,
                            R.layout.list_v,
                            new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
                            R.id.vers,R.id.name, R.id.api});

                    list.setAdapter(adapter);
                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(Clientlist.this, "Ready to Deliver "+oslist.get(+position).get("username"), Toast.LENGTH_SHORT).show();
                            Intent intmam=new Intent(Clientlist.this,RegisterActivity.class);
                            intmam.putExtra("INVOICE_ID", oslist.get(+position).get("sale_code"));
                            intmam.putExtra("ADDRESS", oslist.get(+position).get("ship_add"));
                            intmam.putExtra("USERNAME", oslist.get(+position).get("username"));
                            intmam.putExtra("PHONE", oslist.get(+position).get("phone"));
                            intmam.putExtra("AMOUNT", oslist.get(+position).get("grand_total"));
                            startActivity(intmam);
                        }
                    });

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

}




<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.veer.admin.listview.MainActivity">


    <ListView        android:id="@+id/list"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_above="@+id/getdata" />

    <Button        android:id="@+id/getdata"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="23dp"        android:text="Get Data" />
</RelativeLayout>





list_v.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >

    <TextView        android:id="@+id/vers"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />

    <TextView        android:id="@+id/name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />

    <TextView        android:id="@+id/api"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />




    <TextView        android:id="@+id/add"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />
</LinearLayout>

No comments:

Post a Comment

Dharamart.blogspot.in