I have a spinner that when selected should add a new spinner to the view. 但是当我尝试充气时,我在这一行得到一个错误:
LinearLayout addSpinner =(LinearLayout)selectedItemView.getContext().findViewById(R.id.addSpinnerLayout);
findViewByID 为红色并在鼠标悬停时显示此错误:
Cannot resolve method 'findViewById(int)'
我的整个java类代码是这样的:
public class Portfolio extends ActionbarMenu {
//get beer details from bundle
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_portfolio);
final Spinner portfolioType = (Spinner) findViewById(R.id.portfolioSpinner);
portfolioType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String portfolioChoice = portfolioType.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), portfolioChoice, Toast.LENGTH_LONG).show();
Log.d("portfolio" , portfolioChoice);
if( portfolioChoice.equals("All")){
//get userID
//get user data
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(selectedItemView.getContext());
String userID = prefs.getString("userID", null);
//construct url
String url = "myURL"
//async task goes here
new PortfolioGetAllBeers(selectedItemView.getContext()).execute(url);
}
else if (portfolioChoice.equals("Brewery")){
//inflate brewery spinner
//to do: change to start rater
LinearLayout ll = (LinearLayout) findViewById(R.id.addSpinnerLayout);
ll.removeAllViews();
//add spinner to layout
LayoutInflater inflater = (LayoutInflater)selectedItemView.getContext().getSystemService(selectedItemView.getContext().LAYOUT_INFLATER_SERVICE);
LinearLayout addSpinner = (LinearLayout)selectedItemView.getContext().findViewById(R.id.addSpinnerLayout);
addSpinner.addView(inflater.inflate(R.layout.addspinner_layout, null));
//todo: get breweries and fill spinner
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
}
活动组合:
<?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="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="@+id/portfolioTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:text="Your Portfolio"
android:padding="5dip"
>
</TextView>
</LinearLayout >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="@+id/sortTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:textStyle = "bold"
android:text="Sorting Options:"
android:padding="5dip"
>
</TextView>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<Spinner
android:id="@+id/portfolioSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/portfolio_array"
android:layout_weight="1"
/>
<LinearLayout
android:id="@+id/addSpinnerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</LinearLayout >
</FrameLayout>
<ListView
android:id="@+id/allYourBeersList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="0px"
android:divider="@null"
>
</ListView>
</LinearLayout>
添加pinner_layout:
<?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="match_parent"
android:orientation="vertical"
>
<Spinner
android:id="@+id/portfolioSpinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/portfolio_array"
android:layout_weight="1"
/>
</LinearLayout>