Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

Edit text to Spinner - Android Studio

I have 3 activities.

In the first activity, I can edit 4 string type values which are saved in a database.userid,name and phone.

The idea is that a userid can be used multiple times.

So if i give the value userid the string "James" multiple times,in the 2nd Activity i can type in an edit text the string "james" and every value from my database has as userid string "James" will appear in a spinner

public class SecondActivity extends AppCompatActivity {

public DBHelper helper;
public ArrayList<String> theList;

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

    String input =((EditText)findViewById(R.id.editText)).getText().toString();
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    helper = new DBHelper(SecondActivity.this);
    SQLiteDatabase database = helper.getReadableDatabase();
    theList = new ArrayList<>();

    Cursor cursor = database.query(DBHelper.TABLE_NAME, new String[]{DBHelper.FIELD_4}, DBHelper.FIELD_1+"=?", new String[]{input}, null, null, null);
    if (cursor.moveToFirst())
    {
        do
        {
            theList.add(cursor.getString(4));
        } while (cursor.moveToNext());
    }

    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(SecondActivity.this, android.R.layout.simple_spinner_item, theList);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerAdapter);
}

}

That is my code so far and I thought it would work but it doesn't.

NOTE: I can't use raw queries

question from:https://stackoverflow.com/questions/65602597/edit-text-to-spinner-android-studio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code of searching database seems OK, but you need to make sure of:

  1. You searched the database with the value of the EditText String in onCreate() without giving a change to the user to input their text in the EditText; so if you didn't set a value to this EditText in XML layout, then input will contain an empty String resulting in an empty list in the Spinner

  2. Make sure not to use abstract numbers in queries and use the final values; so you would change:

theList.add(cursor.getString(4));

into

theList.add(cursor.getColumnIndex(DBHelper.FIELD_4));

Also you can try to match your query with LIKE and remove the selection arguments as an alternative solution

Cursor cursor = database.query(DBHelper.TABLE_NAME, 
                                new String[]{DBHelper.FIELD_4},  // columns to return (projection)
                                DBHelper.FIELD_1 + " LIKE '%" + input + "%' " // selection
                                null,  // selection args
                                null, null, null);  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...