gpt4 book ai didi

android - 如何获取对象的字符串值而不是对象值?

转载 作者:行者123 更新时间:2023-11-30 01:48:38 25 4
gpt4 key购买 nike

我想从 sqlite 数据库填充 spinner。在我检索相关数据后,它显示垃圾值。你能帮我解决这个问题吗?我将在此处附上我的代码。

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

-- 数据库助手--

 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很陌生。所以请帮助我。

最佳答案

你可以做的是在你的MovieType类中Override 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() 方法中设置的内容。

关于android - 如何获取对象的字符串值而不是对象值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33352128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com