Contoh kode android untuk menampilkan data json array di listview multi column dari php dengan volley library

Berikut kode android untuk menampilkan data json array di listview multi column dari php dengan volley library.

File Proses .java

package com.example.test;

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

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

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class Penjualan extends Activity {
	String server_url = "http://192.168.1.11/api/penjualan.php";
	
	private ListView list_view;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.penjualan);
		
		makeJsonArrayRequest();		
	}

	private void makeJsonArrayRequest() {
		 
		final RequestQueue requestQueue = Volley.newRequestQueue(Penjualan.this);
 
        JsonArrayRequest req = new JsonArrayRequest(server_url,
		    new Response.Listener<JSONArray>() {
	        @Override
	        public void onResponse(JSONArray response) {
	            try {

                	list_view = (ListView) findViewById(R.id.lv_id_penjualan);
                	
                	ArrayList<HashMap<String, String>> feedList= new ArrayList<HashMap<String, String>>();
                    HashMap<String, String> map = new HashMap<String, String>();
                    
                    for (int i = 0; i < response.length(); i++) {                    	
                   		JSONObject person = (JSONObject) response.get(i);
	            		//al_nama_customer.add(person.getString("nama_customer"));
                    	map = new HashMap<String, String>();
	            		map.put("penjualan_id", person.getString("penjualan_id"));
	                    map.put("nama_customer", person.getString("nama_customer"));
	                    feedList.add(map);
                    }                    
                     
                    SimpleAdapter simpleAdapter = new SimpleAdapter(Penjualan.this, feedList, 
                    		R.layout.penjualanlist, 
                    		new String[]{"penjualan_id", "nama_customer"}, 
                    		new int[]{R.id.tv_id_penjualan_id, R.id.tv_id_nama_customer});

                    list_view.setAdapter(simpleAdapter);
                    
                	list_view.setAdapter(simpleAdapter);
                	list_view.setOnItemClickListener(new OnItemClickListener() {
            			@Override
            			public void onItemClick(AdapterView<?> parent, View view,
            				int position, long id) {
            				// TODO Auto-generated method stub
            				String value = ((TextView) view.findViewById(R.id.tv_id_penjualan_id)).getText().toString(); 
            						//(String) list_view.getItemAtPosition(position);
            				Toast.makeText(Penjualan.this, "Position : " + position + " Value : " + value, 
            						Toast.LENGTH_LONG).show();
            			}    		
            		});                
	 				
                	
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {                        
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();                        
            }
        });
		 
        // Adding request to request queue        
        requestQueue.add(req);
    }
 
}

File Layout Utama .xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top">

    <TextView
        android:id="@+id/tv_id_penjualan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/tv_penjualan"
        android:textSize="20sp" 
    />

    <ListView
        android:id="@+id/lv_id_penjualan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_id_penjualan" >
    </ListView>

</RelativeLayout>

 

File Layout List.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="horizontal" >
 
    <TextView
        android:id="@+id/tv_id_penjualan_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_weight="1"
          android:gravity="center" 
        android:textAppearance="?android:attr/textAppearanceMedium"  />
  
     <TextView
        android:id="@+id/tv_id_nama_customer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_weight="1"
        android:gravity="center" 
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
</LinearLayout>

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s