MySql + MultiAutoCompleteTextView

Step 1 :Follow the first 7 steps of Static and Dynamic TextView Creation Application
 
Step 2 :Add the following Code to activity_main.xml :
<RelativeLayoutxmlns:android=http://schemas.android.com/apk/res/android&#8221;
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<MultiAutoCompleteTextView
android:id=“@+id/multiAutoCompleteTextView1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“true”
android:layout_centerHorizontal=“true”
android:layout_marginTop=“100dp”
android:hint=“MultiAutoCompleteTextView”>
<requestFocus/>
</MultiAutoCompleteTextView>
</RelativeLayout>
 
 
Step 3 :Add the following Code to mainActivity.java 
package com.example.multiautocomplete_ex3;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
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.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
publicclass MainActivity extends Activity {
InputStream is=null;
String result=null;
String line=null;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(http://10.0.2.2/text.php&#8221;);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e(“Pass 1”, “connection success “);
}
catch(Exception e)
{
Log.e(“Fail 1”, e.toString());
}
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(is,“iso-8859-8”),8);
StringBuilder sb=new StringBuilder();
while((line=br.readLine())!=null)
{
sb.append(line+“\n”);
}
is.close();
result=sb.toString();
Log.e(“Pass 2”, “Success”);
}
catch(Exception e)
{
Log.e(“Fail 2”, e.toString());
}
try
{
JSONArray ja=new JSONArray(result);
JSONObject jo=null;
String[] str=new String[ja.length()];
for(int i=0;i<ja.length();i++)
{
jo=ja.getJSONObject(i);
str[i]=jo.getString(“name”);
}
MultiAutoCompleteTextView auto=(MultiAutoCompleteTextView)
findViewById(R.id.multiAutoCompleteTextView1);
auto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
ArrayAdapter<String> adp= new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_dropdown_item_1line,str);
adp.setDropDownViewResource(android.R.layout.simple_expandable_list_item_1);
auto.setThreshold(1);
auto.setAdapter(adp);
}
catch(Exception e)
{
Log.e(“Fail 2”, e.toString());
}
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
returntrue;
}
}

 

Step 4 : Add the Code to AndroidManifest.xml :
<uses-permissionandroid:name=“android.permission.INTERNET”/>
Step 5 :Add the following code to text.php :
<?php
$host=’127.0.0.1′;
$uname=’root’;
$pwd=’password’;
$db=’android’;
$con = mysql_connect($host,$uname,$pwd) or die(“connection failed”);
mysql_select_db($db,$con) or die(“db selection failed”);
$r=mysql_query(“select * from class”,$con);
while($row=mysql_fetch_array($r))
{
$cls[]=$row;
}
print(json_encode($cls));
mysql_close($con);
?>
 
Step 6 : Now save and execute the text.php in your web browser. Your output will be like this : 
[{“0″:”082IT105″,”roll_no”:”082IT105″,”1″:”Anand Babu”,”name”:”Anand Babu”},
{“0″:”082IT108″,”roll_no”:”082IT108″,”1″:”Arul Kumar”,”name”:”Arul Kumar”},
{“0″:”082IT110″,”roll_no”:”082IT110″,”1″:”Ashok kumar”,”name”:”Ashok kumar”},
{“0″:”082IT111″,”roll_no”:”082IT111″,”1″:”Balaji”,”name”:”Balaji”},
{“0″:”082IT113″,”roll_no”:”082IT113″,”1″:”Boopathy”,”name”:”Boopathy”},
{“0″:”082IT123″,”roll_no”:”082IT123″,”1″:”Godwin”,”name”:”Godwin”}]
 
Step 7 :Now you can save and execute the Application. Yours output will be like this :
 

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s