我尝试用自定义适配器工具栏和巡航能力的IM这个错误添加微调: 指定的孩子已经有一个父。您必须先调用子对象的父对象的removeView()。 这是我的微调适配器类:指定的孩子已经有一位家长。您必须先调用子对象的父对象的removeView()。 (与工具栏)
public class SpinnerAdapter extends BaseAdapter {
ArrayList<ShopCartModel> list;
Context context;
LayoutInflater inflater;
public SpinnerAdapter (Context c , ArrayList<ShopCartModel> list){
this.context = c;
this.list = list;
inflater = (LayoutInflater.from(c));
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.custom_spinner_item , null);
TextView name = (TextView)convertView.findViewById(R.id.textView);
ShopCartModel tmp = list.get(position);
name.setText(tmp.getName());
return convertView;
}
}
,这是我的主类时,即时通讯试着微调添加到工具栏:
public class ShopCartScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spin;
ArrayList<ShopCartModel> shopCarts;
SpinnerAdapter spinnerAdapter;
DBHelper db;
ApartmentModel apartment;
GetShopLists g;
SharedPreferences preferences;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop_carts_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
items = (ListView)findViewById(R.id.shop_carts_lists);
spin = (Spinner)findViewById(R.id.spinner);
db = DBHelper.getInstance(this);
preferences = getSharedPreferences("appData", 0);
boolean flag = preferences.getBoolean("ShopCartListsLoadedFromDB" , false); //
int apartmentNumber = preferences.getInt("apartmentNumber" , 0);
apartment = new ApartmentModel(apartmentNumber);
if(flag == true){
shopCarts = db.getshopLists(apartment);
spinnerAdapter = new SpinnerAdapter(getApplicationContext(),shopCarts);
spin.setAdapter(spinnerAdapter);
}
else{
g = new GetShopLists(this, shopCarts , spin , spinnerAdapter);
g.execute("this is where i set the items on the spinner");
preferences = getSharedPreferences("appData", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("ShopCartListsLoadedFromDB" , true);
editor.apply();
}
toolbar.addView(spin, 0); //this is where the application crushes
spin.setOnItemSelectedListener(this);
}
编辑:我主要的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--list of all shop carts of an apartment-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#ff1e8622"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:weightSum="1"
android:id="@+id/newList"
android:baselineAligned="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/newList"
android:id="@+id/textView"
android:layout_weight="0.12"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@android:drawable/ic_input_add"
android:background="#ffffff"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_gravity="center"
android:onClick="createNewList"
/>
</LinearLayout>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/shop_carts_lists"
android:layout_gravity="center_horizontal"
android:layout_below="@id/newList"
android:paddingTop="10dp"/>
</LinearLayout>
and my spinner item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:text="Demo"
android:textColor="#000" />
</LinearLayout>
如果'Spinner'已经在'Toolbar'在你的布局,你并不需要将其添加到'Toolbar'在你的代码。 –
'spin'已经通过XML添加上的布局。你不能再添加它。只要删除'toolbar.addView(旋转,0);'线。 –