你好朋友我想根据SQlite数据库ID在微调器中设置selection()所以我的鳕鱼eis如下
ArrayList<ParserCategory>mArrayListParserCategories = new ArrayList<ParserCategory>();
mArrayListParserCategories = mDatabaseConnectionAPI.getCategoryData(mStrinGetTrMode);
mSpinnerAdapterData = new SpinnerAdapterData(mArrayListParserCategories);
mSpinnerCate.setAdapter(mSpinnerAdapterData);
int id= mSpinnerCate.getIndexFromElement(mSpinnerAdapterData,mStringGetCantname);
DatabaseConnectionAPI.java
public ArrayList<ParserCategory> getCategoryData(String type) {
ArrayList<ParserCategory> mGetCategoryList = new ArrayList<ParserCategory>();
try {
String sqlQuery = "SELECT * FROM Category_Master where cat_type like "+ "'"+type+"'";
Cursor mCursorCategory = Query(sqlQuery);
if (mCursorCategory != null) {
mCursorCategory.moveToFirst();
while (!mCursorCategory.isAfterLast()) {
ParserCategory mParserCategory = new ParserCategory();
mParserCategory
.setCatId(mCursorCategory.getString(mCursorCategory
.getColumnIndex("cat_id")));
mParserCategory
.setCatName(mCursorCategory.getString(mCursorCategory
.getColumnIndex("cat_name")));
mParserCategory
.setCatType(mCursorCategory.getString(mCursorCategory
.getColumnIndex("cat_type")));
mParserCategory
.setCatMode(mCursorCategory.getString(mCursorCategory
.getColumnIndex("cat_mode")));
mGetCategoryList.add(mParserCategory);
mCursorCategory.moveToNext();
}
}
mCursorCategory.close();
} catch (Exception e) {
e.printStackTrace();
}
return mGetCategoryList;
}
SpinnerAdapterData.java
public class SpinnerAdapterData extends ArrayAdapter<ParserCategory> {
ArrayList<ParserCategory> mArrayList;
LayoutInflater mInflater;
Activity mActivity;
public SpinnerAdapterData(ArrayList<ParserCategory> list) {
super( IncomeUpdateActivity.this, R.layout.custom_spin_layout, list);
this.mArrayList = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = getLayoutInflater();
convertView = inflator.inflate(R.layout.custom_spin_layout, null);
TextView mTextView = (TextView) convertView.findViewById(R.id.txt);
mTextView.setText(mArrayList.get(position).getCatName());
return convertView;
}
}
getIndexFromElement函数
public int getIndexFromElement(ArrayAdapter<ParserCategory> adapter, String element) {
for(int i = 0; i < adapter.getCount(); i++) {
if(adapter.getItem(i).equals(element)) {
return i;
}
}
return 0;
}
当我运行上面的代码时,它会重新设置id始终为0,我知道如何解决它?
答案 0 :(得分:1)
您的适配器包含ParserCategory
个对象,您将它们与String
参数进行比较,该参数始终为false。你可能想要这样的东西(假设元素应该与CatName字段匹配):
if (((ParserCategory)adapter.getItem(i)).getCatName().equals(element))
另一种解决方案是覆盖ParserCategory类中的equals()
和hashCode()
。