这是我面临的一个奇怪的问题。数据来自SQLite数据库。它显示包名:com.dzinesunlimited.foodzines.free.Administration.Menu.MenuAdministrationActivity
而不是“选择一个类别”(在strings.xml
中定义)。 Spinner在XML中定义:
<Spinner
android:id="@+id/spnDishCategory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:popupBackground="@color/panel_medium"
android:prompt="@string/menu_admin_category_spinner_prompt" >
</Spinner>
这是Java代码:
MenuAdministratorCategoryAdapter adapMenuCategories = new MenuAdministratorCategoryAdapter(
getActivity(),
android.R.layout.simple_spinner_item,
arrMenuCategory);
spnDishCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
if (position != -0) {
String strMenucategory = arrMenuCategory.get(position).getCatID();
SELECTED_CATEGORY_ID = strMenucategory;
Log.e("CAT ID", SELECTED_CATEGORY_ID);
Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_SHORT).show();
/** CLEAR THE CURRENT GRIDVIEW AND THE ARRAYADAPTER **/
gridDishes.invalidate();
adapter.notifyDataSetChanged();
arrDishes.clear();
/** CHANGE THE PROMPT **/
spnDishCategory.setPrompt(arrMenuCategory.get(position).getCatName());
/** FETCH THE MENUS IN THE SELECTED CATEGORY **/
new fetchMenusInTheCategory().execute();
/* INSTANTIATE THE ADAPTER */
adapter = new MenuAdministrationAdapter(getActivity(), arrDishes);
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
这是从数据库中提取数据的AsyncTask
:
private class loadMenuCategories extends AsyncTask<Void, Void, Void> {
/** AN INTEGER INSTANCE TO HOLD THE TOTAL NUMBER OF ITEMS RETURNED BY THE DATABASE **/
int intMenuCategories;
@Override
protected void onPreExecute() {
super.onPreExecute();
// CONSTRUCT THE QUERY TO FETCH ALL TWEETS FROM THE DATABASE
String strQueryData = "SELECT * FROM categories";
// CAST THE QUERY IN THE CURSOR TO FETCH THE RESULTS
cursor = db.selectAllData(strQueryData);
intMenuCategories = cursor.getCount();
}
@Override
protected Void doInBackground(Void... params) {
if (intMenuCategories > 0) {
if (cursor != null && cursor.getCount() != 0) {
/* AN INSTANCE OF THE MenuAdministratorCategoryData HELPER CLASS */
MenuAdministratorCategoryData menuCategory;
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
/***** INSTANTIATE THE MenuAdministratorCategoryData INSTANCE "menuCategory" *****/
menuCategory = new MenuAdministratorCategoryData();
/** GET THE CATEGORY ID **/
if (cursor.getString(cursor.getColumnIndex(db.CATEGORY_ID)) != null) {
String strCatID = cursor.getString(cursor.getColumnIndex(db.CATEGORY_ID));
menuCategory.setCatID(strCatID);
} else {
String strCatID = null;
menuCategory.setCatID(strCatID);
}
/** GET THE CATEGORY NAME **/
if (cursor.getString(cursor.getColumnIndex(db.CATEGORY_NAME)) != null) {
String strCatName = cursor.getString(cursor.getColumnIndex(db.CATEGORY_NAME));
menuCategory.setCatName(strCatName);
} else {
String strCatName = null;
menuCategory.setCatName(strCatName);
}
/** ADD THE COLLECTED DATA TO THE ARRAYLIST **/
arrMenuCategory.add(menuCategory);
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// CLOSE THE CURSOR
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
// CLOSE THE DATABASE
db.close();
// SET THE ADAPTER TO THE GRIDVIEW
spnDishCategory.setAdapter(adapMenuCategories);
new showDefaultMenuCategory().execute();
}
}
这是适配器:
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
row = inflater.inflate(R.layout.menu_fragment_category_spn_row, parent, false);
}
TextView txtCategoryCode = (TextView) row.findViewById(R.id.txtCategoryCode);
Typeface fntCategoryCode = Typeface.createFromAsset(activity.getAssets(), "fonts/RobotoCondensed-Regular.ttf");
txtCategoryCode.setTypeface(fntCategoryCode);
/** SET THE CATEGORY NAME **/
String strCatName = arrMenuCategory.get(position).getCatName();
if (strCatName != null) {
txtCategoryCode.setText(strCatName);
}
return row;
}
它看起来如何:
感谢任何帮助,帮助我解决这个问题。
P.S。:如果需要任何其他代码,请告诉我。
编辑:差点忘了。这是一个自定义Spinner。该行的自定义布局为:
menu_fragment_category_spn_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtCategoryCode"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:background="#ff171717"
android:gravity="left|center"
android:maxLines="1"
android:padding="11dp"
android:text="INVENTORY CATEGORY CODE"
android:textColor="#F9F9F9"
android:textSize="18sp" >
</TextView>
答案 0 :(得分:0)
也许是为时已晚,但也许这个答案可以帮助任何其他面临这个问题的开发人员。
只需使用与下拉视图相同的实现覆盖适配器中的getView方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getDropDownView(position, convertView, parent);
}