0

我花了三天多的时间思考这个问题。我想制作一个从数据库填充的列表视图。这里的问题可能在于如何将 ArrayList 转换为 String[]。results 是 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());
          }
      });
}
}
4

3 回答 3

1

将 ArrayList[] 转换为字符串数组非常简单。

String[] searchedQueries = new String[stock_list.size()];
stockArr = stock_list.toArray(stockArr);
for(String s : stockArr)
    System.out.println(s);
于 2012-11-13T04:55:47.897 回答
0

为什么不使用 SimpleCursorAdapter 而不是 ArrayAdapter?然后,您可以在 SQL 中使用 WHERE 子句来搜索列表。这是一种非常常用的技术。

于 2012-11-13T05:38:32.487 回答
0

在一行中

String[] searchedQueries = stock_list.toArray(new String[stock_list.size()]);

于 2013-12-21T16:39:35.307 回答