two xmls
activity_main.xml
<LinearLayout 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"
android:orientation= "vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</LinearLayout>
for list view
data.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center" >
</TextView>
java
main_activity.java
package com.example.atest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
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.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent edu = new Intent(getApplicationContext(),newww.class);
startActivity(edu);
}});
}
}
newww.java
package com.example.atest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
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.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class newww extends ListActivity {
InputStream is=null;
String result=null;
String line=null;
String[] roll_no,name;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final List<String> list1 = new ArrayList<String>();
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/spinner.php");
HttpResponse response = httpclient.execute(httppost);
Log.e("Fail 1", "3");
HttpEntity entity = response.getEntity();
Log.e("Fail 1", "4");
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
finish();
}
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();
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONArray JA=new JSONArray(result);
JSONObject json= null;
roll_no = new String[JA.length()];
name = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
roll_no[i] = json.getString("roll_no");
name[i]=json.getString("name");
}
Toast.makeText(getApplicationContext(), "doneee",Toast.LENGTH_LONG).show();
// for(int i=0;i<roll_no.length;i++)
// {
// list1.add(roll_no[i]);
//// list2.add(name[i]);
// }
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
//login.this.finish();
}
};
private void spinner_fn() {
// TODO Auto-generated method stub
setListAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.data,roll_no));
//ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
// android.R.layout.simple_spinner_item, roll_no);
//
//dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner1.setAdapter(dataAdapter1);
//
//
//ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(),
// android.R.layout.simple_spinner_item, name);
//dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner2.setAdapter(dataAdapter2);
}
}
activity_main.xml
<LinearLayout 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"
android:orientation= "vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</LinearLayout>
for list view
data.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center" >
</TextView>
java
main_activity.java
package com.example.atest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
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.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent edu = new Intent(getApplicationContext(),newww.class);
startActivity(edu);
}});
}
}
newww.java
package com.example.atest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
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.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class newww extends ListActivity {
InputStream is=null;
String result=null;
String line=null;
String[] roll_no,name;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final List<String> list1 = new ArrayList<String>();
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/spinner.php");
HttpResponse response = httpclient.execute(httppost);
Log.e("Fail 1", "3");
HttpEntity entity = response.getEntity();
Log.e("Fail 1", "4");
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
finish();
}
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();
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONArray JA=new JSONArray(result);
JSONObject json= null;
roll_no = new String[JA.length()];
name = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
roll_no[i] = json.getString("roll_no");
name[i]=json.getString("name");
}
Toast.makeText(getApplicationContext(), "doneee",Toast.LENGTH_LONG).show();
// for(int i=0;i<roll_no.length;i++)
// {
// list1.add(roll_no[i]);
//// list2.add(name[i]);
// }
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
//login.this.finish();
}
};
private void spinner_fn() {
// TODO Auto-generated method stub
setListAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.data,roll_no));
//ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
// android.R.layout.simple_spinner_item, roll_no);
//
//dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner1.setAdapter(dataAdapter1);
//
//
//ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(),
// android.R.layout.simple_spinner_item, name);
//dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner2.setAdapter(dataAdapter2);
}
}
No comments:
Post a Comment