我花了三天多时间思考这件事。我想创建一个从数据库填充的列表视图。这里的问题可能是如何将ArrayList转换为String []。结果是ArrayList和SearchQueries是字符串数组。
public class SearchActivity extends ListActivity {
EditText search_box;
ListView customList;
private ArrayList<String> results = new ArrayList<String>();
private String tableName = DatabaseHandler.TABLE_SEARCH;
private SQLiteDatabase newDB;
ArrayList<String> searched_list;
String[] SearchQueries;
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_search);
DatabaseHandler dbHelper = new DatabaseHandler(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("SELECT skeys FROM " +
tableName, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String skeys = c.getString(c.getColumnIndex("skeys"));
results.add(skeys);
}while (c.moveToNext());
}
}
SearchQueries = (String[]) results.toArray();
Collections.shuffle(Arrays.asList(results));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.listviews_search, SearchQueries);
customList.setAdapter(adapter);
customList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
search_box.setVisibility(View.VISIBLE);
searched_list = new ArrayList<String>();
search_box.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = search_box.getText().length();
searched_list.clear();
for (int i = 0; i < SearchQueries.length; i++) {
if (length <= SearchQueries.length) {
if (search_box.getText().toString().equalsIgnoreCase((String)SearchQueries[i]
.subSequence(0, length))) {
searched_list.add(SearchQueries[i].toString());
}
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(),
R.layout.listviews_search, searched_list);
customList.setAdapter(adapter);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
最后,我用更简单的代码解决了这个问题,这里是为那些遇到同样问题的人提供参考的代码:
解决方案:
public class SearchActivity extends ListActivity {
ArrayAdapter<String> dataAdapter = null;
private ArrayList<String> results = new ArrayList<String>();
private String tableName = DatabaseHandler.TABLE_SEARCH;
private SQLiteDatabase newDB;
ArrayAdapter<String> adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_search);
DatabaseHandler dbHelper = new DatabaseHandler(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("SELECT skeys FROM " +
tableName, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String skeys = c.getString(c.getColumnIndex("skeys"));
results.add(skeys);
}while (c.moveToNext());
}
}
dataAdapter = new ArrayAdapter<String>(this,
R.layout.listviews_search, results);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.listviews_search, results));
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(dataAdapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
EditText myFilter = (EditText) findViewById(R.id.search);
myFilter.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
}
}
答案 0 :(得分:1)
将ArrayList []转换为String Array非常简单。
String[] searchedQueries = new String[stock_list.size()];
stockArr = stock_list.toArray(stockArr);
for(String s : stockArr)
System.out.println(s);
答案 1 :(得分:0)
为什么不使用SimpleCursorAdapter而不是ArrayAdapter?然后,您可以使用SQL中的WHERE子句搜索列表。这是一种非常常用的技术。
答案 2 :(得分:0)
在一行
String[] searchedQueries = stock_list.toArray(new String[stock_list.size()]);