데이터베이스에서 검색 한 데이터에서 Android 스피너를 채우려고합니다. 나는 회에 데이터를 바인딩하려고 코드 여기Android Spinner를 동적으로 채우기
private ArrayList<CategoryModel> catList = new ArrayList<CategoryModel>();
public ArrayList <CategoryModel> getAllCatName() {
try {
String sql = "SELECT categoryName FROM category";
Cursor mCur = mDb.rawQuery(sql, null);
Log.e(TAG, "Data Grab Success");
if (mCur.getCount() != 0) {
if (mCur.moveToFirst()) {
do {
CategoryModel cm = new CategoryModel();
cm.setCategoryName((mCur.getString(mCur
.getColumnIndex("categoryName"))));
catList.add(cm);
} while (mCur.moveToNext());
}
}
return catList;
} catch (SQLException mSQLException) {
throw mSQLException;
}
}
그리고 :
spinnerCat = (Spinner) dialogView.findViewById(R.id.spinnerCat);
ArrayList<CategoryModel> cat_list = cc.getAllCatName();
ArrayAdapter<CategoryModel> adapterFrom = new ArrayAdapter<CategoryModel>(this,
android.R.layout.simple_spinner_item, cat_list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// Get item at position
CategoryModel catModel = getItem(position);
// Set textview's value with the category name
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(catModel.getCategoryName());
return view;
}
};
adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCat.setAdapter(adapterFrom);
그리고 내 엔티티 클래스 :
public String categoryName;
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
다음은 데이터를 검색하기 위해 내 SQL 문이다
그러나 지금 문제는 내가 검색 한 첫 번째 항목이 정상적으로 작동하는 것 같습니다. 그러나 회 전자의 다음 항목은이 형식으로 나타났습니다 [email protected]
. 왜 그런지 궁금해.
미리 감사드립니다.
편집
DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
CategoryController cc = new CategoryController(
mDbHelper.open());
Cursor cursor = cc.getAllCatName();
if(cursor.getCount()>0){
String[] from = new String[]{"categoryName"};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item,
cursor, from, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCat.setAdapter(mAdapter);
}
그리고 내 SQL 문 방법 :
public Cursor getAllCatName() {
try {
String sql = "SELECT categoryName FROM category";
Cursor mCur = mDb.rawQuery(sql, null);
Log.e(TAG, "Data Grab Success");
if (mCur.getCount() != 0) {
if (mCur.moveToFirst()) {
do {
CategoryModel cm = new CategoryModel();
cm.setCategoryName((mCur.getString(mCur
.getColumnIndex("categoryName"))));
catList.add(cm);
} while (mCur.moveToNext());
}
}
return mCur;
} catch (SQLException mSQLException) {
throw mSQLException;
}
}
그리고 새로운 오류 메시지가 열 _id가 존재하지 않는 것입니다. 아무데도 _id를 선언하지 않은 이유는 무엇인지 모르겠습니다.
스피너 객체 루프 –
에 mulipal 때 만들어가 (I = 0을 나타내는 int i가() cat_list.size를 <; 내가 ++) { adapterFrom = 새로운 ArrayAdapter와 (이, android.R.layout.simple_spinner_item, cat_list.get (i) .getCategoryName()); adapterFrom.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); spinnerCat.setAdapter (adapterFrom); }이 줄의 문제점 –
예,하지만 회 전자에 개체를 추가하는 방법을 모르겠습니다. 너는 어떤 생각을 가지고 있니? –