Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI

this is my php/mysql table

PHP
<?php
 //require_once('db-connect.php');
// $today = date('d-m-y');
 $host="localhost";
 $user="root";
 $pass="";
 $con=mysql_connect($host,$user,$pass);
 mysql_query ("set character_set_client='utf8'");
 mysql_query ("set character_set_results='utf8'");
 mysql_query ("set collation_connection='utf8_general_ci'");

 if($con)
 {
    //echo "connected to MySql"."<br>";
    $db=mysql_selectdb("dictionary");
    if($db)
    {
            $sql="SELECT * FROM dd ORDER BY word ";
            $exec_query=mysql_query($sql);

            $result = array();

        while($row = mysql_fetch_array($exec_query))
        {
             array_push($result, array('word'=> $row['word'],'meaning'=>$row['meaning']));
        }
    }
    echo json_encode($result);
 }
 else
 {
    echo json_encode(array('error'=> 'db error'));
 }
 //echo "hai";
?>


Then my Java code is

package selva.select;

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.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class DbselectActivity extends Activity {

public void onCreate(Bundle savedInstanceState)

{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String result = null;
InputStream is = null;

try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://10.0.2.2/dictionary/data-query.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

Log.e("log_tag", "connection success ");
// Toast.makeText(getApplicationContext(), "pass",
// Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
Toast.makeText(getApplicationContext(), "Connection fail",
Toast.LENGTH_SHORT).show();

}
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
// Toast.makeText(getApplicationContext(),
// "Input Reading pass", Toast.LENGTH_SHORT).show();
}
is.close();

result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
Toast.makeText(getApplicationContext(),
" Input reading fail", Toast.LENGTH_SHORT).show();

}

// parse json data
try {

JSONArray jArray = new JSONArray(result);

String re = jArray.getString(jArray.length() - 1);

TableLayout tv = (TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();

int flag = 1;

for (int i = -1; i < jArray.length() - 1; i++)

{

TableRow tr = new TableRow(DbselectActivity.this);

tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

if (flag == 1) {

TextView b6 = new TextView(DbselectActivity.this);
b6.setText("ID");
b6.setTextColor(Color.BLUE);
b6.setTextSize(15);
tr.addView(b6);

TextView b19 = new TextView(DbselectActivity.this);
b19.setPadding(10, 0, 0, 0);
b19.setTextSize(15);
b19.setText("Name");
b19.setTextColor(Color.BLUE);
tr.addView(b19);

TextView b29 = new TextView(DbselectActivity.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("no");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b29);

tv.addView(tr);

final View vline = new View(DbselectActivity.this);
vline.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);

tv.addView(vline);
flag = 0;

}

else {

JSONObject json_data = jArray.getJSONObject(i);

Log.i("log_tag",
"word: " + json_data.getString("word")
+ ", meaning: "
+ json_data.getString("meaning"));

TextView b = new TextView(DbselectActivity.this);
String stime = String.valueOf(json_data
.getString("word"));
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);

TextView b1 = new TextView(DbselectActivity.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String stime1 = json_data.getString("meaning");
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/TSCu_SaiIndira.ttf");

b1.setTypeface(tf);
b1.setText(stime1);
b1.setTextColor(Color.WHITE);
tr.addView(b1);

// TextView b2=new TextView(DbselectActivity.this);
// b2.setPadding(10, 0, 0, 0);
// String
// stime2=String.valueOf(json_data.getInt("f3"));
// b2.setText(stime2);
// b2.setTextColor(Color.RED);
// b2.setTextSize(15);
// tr.addView(b2);

tv.addView(tr);

final View vline1 = new View(DbselectActivity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);

}

}

} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail",
Toast.LENGTH_SHORT).show();
}

}
});

}

}

my layout code was ,


<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/e1"
android:text="Button" />

<scrollview>
android:layout_below="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true"
>
<horizontalscrollview>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true">
<tablelayout>

android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true"
>










Actually , my problem is i want the user to type by edit text and it needs to filter it and it should shows by list view ..

Kindly please guide me .. i tried but i don't find any source..

Thanks is advance
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900