使用ArrayAdapter填充autoCompleteTextView

时间:2014-07-17 18:18:02

标签: android android-arrayadapter autocompletetextview

我正在尝试使用ArrayAdapter将数据填充到autoCompleteTextView中,每当我运行应用程序时它告诉我“不幸的是应用程序已停止”。这部分对我来说是一个gosh“adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,words);         edit.setAdapter(adapter);“我做错了什么?请帮忙

public class MainActivity extends ActionBarActivity implements TextWatcher
{
private static final String DB_NAME = "mydata.sqlite3"; 
ArrayAdapter<String> adapter; 
private SQLiteDatabase database;
private ArrayList<String> words;
private AutoCompleteTextView edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
    database = dbOpenHelper.openDataBase();  
    edit = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    setWordList();         
}
private void setWordList() 
{               
    words = new ArrayList<String>();
    Cursor friendCursor = database.rawQuery("SELECT * FROM WordTableList", null);
    friendCursor.moveToFirst();
    if(!friendCursor.isAfterLast()) 
    {
        do 
        {
            String myWord = friendCursor.getString(1);          
            words.add(myWord);

        } while (friendCursor.moveToNext());
    }
    friendCursor.close();   
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, words);
    edit.setAdapter(adapter);       
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}    
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{        
}
@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count)
{        
}
@Override
public void afterTextChanged(Editable s)
{        
}    

0 个答案:

没有答案