2012-11-13 107 views
0

我花了三天以上的時間思考這個問題。我想做一個從數據庫填充的listview。這裏的問題可能是如何將ArrayList轉換爲String []。結果是ArrayList和SearchQueries是字符串數組。從數據庫中填充Android填充ListView和文本過濾器

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) { 
     } 
    }); 
    }  
} 

最後我解決這個問題的一個簡單的代碼,這裏是爲那些誰得到了同樣的問題REFFERENCE代碼:

解決方案:

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()); 
      } 
     }); 
} 
} 

回答

1

要轉換一個ArrayList []以一個字符串數組非常簡單。

String[] searchedQueries = new String[stock_list.size()]; 
stockArr = stock_list.toArray(stockArr); 
for(String s : stockArr) 
    System.out.println(s); 
+0

由於Hennaz。現在解決了 – Nebliena

0

爲什麼不使用SimpleCursorAdapter而不是ArrayAdapter?然後,您可以通過在SQL中使用WHERE子句來搜索列表。這是一種非常常用的技術。

0

在一個線

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