0

私はこれについて考えるのに3日以上費やしました。データベースから取り込まれたリストビューを作成したい。ここでの問題は、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 Array に変換するのは非常に簡単です。

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

ArrayAdapter の代わりに SimpleCursorAdapter を使用しないのはなぜですか? 次に、SQL で WHERE 句を使用してリストを検索できます。これは非常に一般的に使用される手法です。

于 2012-11-13T05:38:32.487 に答える