I want to populate spinner
from sqlite
database. After I am retrieving the relevant data it showed junk values. Could you please help me to fix this. I will attach my codes here.
--java File--
public class AddNewMovieActivity extends AppCompatActivity {
private Spinner movieTypes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_new_movie);
movieTypes = (Spinner) findViewById(R.id.spinMovieType);
loadSpinnerData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_add_new_movie, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadSpinnerData() {
// database handler
DBHelper db = new DBHelper(this);
// Spinner Drop down elements
List<MovieType> lables = db.getAllMovieTypes();
// Creating adapter for spinner
ArrayAdapter<MovieType> dataAdapter = new ArrayAdapter<MovieType>(this,android.R.layout.simple_spinner_item,lables);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
movieTypes.setAdapter(dataAdapter);
}
}
-- DBHelper--
public List<MovieType> getAllMovieTypes() {
SQLiteDatabase db = getReadableDatabase();
List<MovieType> lst = new ArrayList<MovieType>();
Cursor cursor = db.query(TABLE_MOVIE_TYPE, null, COLUMN_STATUS + "=?", new String[]{"A"}, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()){
String type = cursor.getString(1).toString();
MovieType movieType = new MovieType(type);
movieType.setTypeId(cursor.getInt(0));
lst.add(movieType);
cursor.moveToNext();
}
}
db.close();
return lst;
}
I just want to show the movie types from the database. Please help me. this code returns the object values. but I need the string value of it. I have tried to convert my object to string. but I couldn't find a proper way to fix it. Actually I am very new to Android. so please help me.
Aucun commentaire:
Enregistrer un commentaire