From AutoComplete textbox to database search and display?

Posted by svebee on Stack Overflow See other posts from Stack Overflow or by svebee
Published on 2010-04-23T07:17:28Z Indexed on 2010/04/23 7:23 UTC
Read the original article Hit count: 726

Filed under:
|
|

Hello everyone, I have a small problem so I would be grateful if anyone could help me in any way. Thank you ;)

I have this little "application", and I want when someone type in a AutoComplete textbox for example "New" it automatically displays "New York" as a option and that (AutoComplete function) works fine. But I want when user type in full location (or AutoComplete do it for him) - that text (location) input is forwarded to a database search which then searches through database and "collects" all rows with user-typed location. For example if user typed in "New York", database search would find all rows with "New York" in it. When it finds one/more row(s) it would display them below. In images...

I have this when user is typing...

http://www.imagesforme.com/show.php/1093305_SNAG0000.jpg

I have this when user choose a AutoComplete location

(h)ttp://www.imagesforme.com/show.php/1093306_SNAG0001.jpg (remove () on the beggining)

But I wanna this when user choose a AutoComplete location

(h)ttp://www.imagesforme.com/show.php/1093307_CopyofSNAG0001.jpg (remove () on the beggining)

Complete Code

    package com.svebee.prijevoz;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
public class ZelimDoci extends Activity {

TextView lista; 

static final String[] STANICE = new String[] {
   "New York", "Chicago", "Dallas", "Los Angeles"
 };

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.zelimdoci);

  AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, STANICE);
  textView.setAdapter(adapter);

  lista = (TextView)findViewById(R.id.lista);

  SQLiteDatabase myDB= null;
  String TableName = "Database";

  String Data="";

  /* Create a Database. */
  try {
   myDB = this.openOrCreateDatabase("Database", MODE_PRIVATE, null);

   /* Create a Table in the Database. */
   myDB.execSQL("CREATE TABLE IF NOT EXISTS "
     + TableName
     + " (Field1 INT(3) UNIQUE, Field2 INT(3) UNIQUE, Field3 VARCHAR UNIQUE, Field4 VARCHAR UNIQUE);"); 

   Cursor a = myDB.rawQuery("SELECT * FROM Database where Field1 == 1", null);
   a.moveToFirst();
   if (a == null) {   
   /* Insert data to a Table*/
   myDB.execSQL("INSERT INTO "
     + TableName
     + " (Field1, Field2, Field3, Field4)"
     + " VALUES (1, 119, 'New York', 'Dallas');");
   myDB.execSQL("INSERT INTO "
     + TableName
     + " (Field1, Field2, Field3, Field4)"
     + " VALUES (9, 587, 'California', 'New York');");
   }
   myDB.execSQL("INSERT INTO "
     + TableName
     + " (Field1, Field2, Field3, Field4)"
     + " VALUES (87, 57, 'Canada', 'London');");
   }

   /*retrieve data from database */
   Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);

   int Column1 = c.getColumnIndex("Field1");
   int Column2 = c.getColumnIndex("Field2");
   int Column3 = c.getColumnIndex("Field3");
   int Column4 = c.getColumnIndex("Field4");

   // Check if our result was valid.
   c.moveToFirst();
   if (c != null) {
    // Loop through all Results
    do {
     String LocationA = c.getString(Column3);
     String LocationB = c.getString(Column4);
     int Id = c.getInt(Column1);
     int Linija = c.getInt(Column2);
     Data =Data +Id+" | "+Linija+" | "+LocationA+"-"+LocationB+"\n";
    }while(c.moveToNext());
   }
   lista.setText(String.valueOf(Data));
  }
  catch(Exception e) {
   Log.e("Error", "Error", e);
  } finally {
   if (myDB != null)
    myDB.close();
  }
 }
}

.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:textSize="20sp" android:gravity="center_horizontal" android:padding="10sp" android:text="Test AutoComplete"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:padding="5dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AutoComplete" />
    <AutoCompleteTextView android:id="@+id/autocomplete_country"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"/>
</LinearLayout>

</LinearLayout>

© Stack Overflow or respective owner

Related posts about android

Related posts about autocomplete