我有这个小“应用程序”,我想当有人输入自动完成文本框例如“新建”时,它会自动显示“纽约”作为选项,并且(自动完成功能)正常工作。但我希望当用户输入完整位置(或自动完成为他执行)时 - 文本(位置)输入被转发到数据库搜索,然后搜索数据库并“收集”具有用户类型位置的所有行。例如,如果用户在“纽约”中键入,则数据库搜索将查找其中包含“纽约”的所有行。当它找到一行/多行时,它会在下面显示它们。在图像......
我在用户输入时有这个...
http://www.imagesforme.com/show.php/1093305_SNAG0000.jpg
当用户选择自动完成位置
时,我会这样做http://www.imagesforme.com/show.php/1093306_SNAG0001.jpg
但是,当用户选择自动完成位置
时,我想这样做http://www.imagesforme.com/show.php/1093307_CopyofSNAG0001.jpg
完整代码
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文件
<?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>
答案 0 :(得分:3)
我找到了解决方案,但没有找到 AutoCompleteTextBox - 但它非常接近,我会说同样的问题以两种不同的方式解决(你可以用 LIKE'参数调用你的数据库) %someText%',基本上它是AutoCompleteTextBox)。解决方案是 - 只需添加
addTextChangedListener
在您的EditText和
上onTextChanged
只需调用一些方法refreshList() - 它将调用数据库并提取结果(并且当然会在列表中显示它们)
someTextView = (EditText) findViewById(R.id.someTextView);
someTextView.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String enteredText = someTextView.getText().toString();
refreshList(enteredText);
}
});
这就是refreshList()的样子
public void refreshList(String text) {
items = SomeClassWithStaticMethods.getSomeData(text);
list.setAdapter(new ArrayAdapter<String>(this, R.layout.single_row, R.id.singleRow, items));
}
答案 1 :(得分:1)
我找到了自动完成的解决方案,listview显示如下: -
namelist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search here"
/>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SearchActivity.java
public class SearchActivity extends Activity {
/** Called when the activity is first created. */
ListView listView;
List<String> listItems=new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nameslist);
// editTextSearch=(EditText)findViewById(R.id.editTextSearch);
listView=(ListView)findViewById(R.id.listView);
refreshList("");
}
public void refreshList(String text) {
listItems.clear();
DatabaseHandler db = new DatabaseHandler(this);
List<Contact> mContactList= db.getAllContactsBySearch("");
for(int i=0;i<mContactList.size();i++)
{
listItems.add(mContactList.get(i).getName());
}
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, listItems);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.textView);
textView.setAdapter(adapter);
}
}
Databasehandler.java
// Getting All Contacts
public List<Contact> getAllContactsBySearch(String searchStr) {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + " where name LIKE '%"+searchStr+"%'";
SQLiteDatabase db = this.getWritableDatabase();
try
{
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
}
catch(Exception e)
{
}
// return contact list
return contactList;
}
谢谢!我希望这会有效。
答案 2 :(得分:0)
autocompletetextview
我遵循的最简单的方法就是..
main.java
DBController dbadpter= new DBController(this);
private SQLiteDatabase db;
ArrayAdapter<String> adapter ;
....
super.onCreate(savedInstanceState);
...
AutoCompleteTextView text=(AutoCompleteTextView)
findViewById(R.id.text);
String[] srr=dbadpter.getplant();
adapter = new ArrayAdapter<String>(this, R.layout.list_view, srr);
text.setThreshold(1);
text.setAdapter(adapter);
DBController.java
public String[] getplant(){
Cursor cursor = getReadableDatabase().rawQuery("SELECT DISTINCT YOURCOLUMNNAME FROM YOURTABLENAME", null);
cursor.moveToFirst();
ArrayList<String> text = new ArrayList<String>();
while(!cursor.isAfterLast()) {
text.add(cursor.getString(cursor.getColumnIndex("YOURCOLUMNNAME")));
cursor.moveToNext();
}
cursor.close();
return text.toArray(new String[text.size()]);
}
main.xml中
<AutoCompleteTextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search here"
/>
list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</TextView>