Search

Wednesday, 7 December 2016

JSON Select Data

Retrive Data into the ListView


XML File


    <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/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SELECT [MySql]"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />
      
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="432dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="20dp" >

        </ListView>
       
    </LinearLayout>



Customizing listview
cust.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@android:id/text1"
  android:background="@drawable/lv"
       
        android:padding="20dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:textColor="#FF0000FF" /> 




Java File


package mysql.select;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class select extends Activity {
InputStream IS;
private ListView lv;
String line,result;
ArrayAdapter<ArrayList> adapter;
ArrayList list;

static int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
lv=(ListView)findViewById(R.id.listView1);
      
list=new ArrayList();
adapter=new ArrayAdapter<ArrayList>(getApplicationContext(),R.layout.cust_list,list);
lv.setAdapter(adapter);
new Select().execute("");
}
  private class Select extends AsyncTask<String, String, String>
  {

private JSONArray jsonarray;

@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub

        try {
            HttpClient httpclient = new DefaultHttpClient();
          //  HttpPost httppost = new HttpPost("http://10.0.2.2/MCA/allselect.php");
  HttpPost httppost = new HttpPost("http://mca.freeoda.com/MCA/allselect.php");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            IS = entity.getContent();
            Log.e("pass 1", "connection success "+IS.available());
        } catch (Exception e) {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
        }
        try {
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(IS, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            
while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            IS.close();
            result = sb.toString();


            Log.e("pass 2", "connection success ");

        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }

        try {
       
            jsonarray = new JSONArray(result);
        
            runOnUiThread(new Runnable() {
           
@Override
public void run() {
// TODO Auto-generated method stub
try{
for(int i=0;i<jsonarray.length();i++){
// Toast.makeText(getApplicationContext(), jsonarray.getString(i), 50).show();
list.add(jsonarray.getString(i).toString());
}
}catch(Exception d){
Log.e("traackiing",d.toString());
}
       adapter=new ArrayAdapter<ArrayList>(getApplicationContext(),R.layout.nilesh_list,list);
          lv.setAdapter(adapter);
}
});
         
            Log.e("pass 3", ""+jsonarray.length());
        } catch (Exception we) {
            Log.e("Fail 3", we.toString());
        
        }


return null;
}
 
  }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.select, menu);
return true;
}

}

No comments:

Post a Comment