我想从spinner
数据库填充sqlite
。在我检索相关数据后,显示垃圾值。你能帮我解决这个问题。我会在这里附上我的代码。
- java文件 -
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;
}
我只是想从数据库中显示电影类型。请帮我。此代码返回对象值。但我需要它的字符串值。我试图将我的对象转换为字符串。但我找不到合适的方法来解决它。其实我对Android很新。所以请帮助我。
答案 0 :(得分:1)
您可以在 MovieType 类中覆盖 toString()
方法
public class MovieType{
//Whatever fields you have here
//Override toString()
@Override
public String toString() {
return your_field_name;
/***put the field name which you want to show in spinner
Or
You can append multiple fileds
return field1 + field2+......;
***/
}
}
然后你的所有设置,您将看到spinner
中字段的值,您在toString()
方法中设置的内容。
答案 1 :(得分:0)
您可以制作自定义适配器,也可以制作字符串类型列表。 正如您所提到的,您只想显示电影类型。您可以在getAllMovieTypes()函数中执行此操作。
public List<String> getAllMovieTypes() {
SQLiteDatabase db = getReadableDatabase();
List<String> lst = new ArrayList<String>();
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();
lst.add(type);
cursor.moveToNext();
}
}
db.close();
return lst;
}
现在,您只能显示类型。