因此,我尝试制作一个下拉菜单,其中菜单上的每个项目均对应城市名称(纽约,洛杉矶,旧金山等)。对于每个用户,下拉菜单上的城市/项目数量是不同的,因此我不能使用switch语句。因此,让我引导您阅读代码,这样才有意义。
1)我首先制作了这个CitySpinner ArrayAdapter,在该类中,我有一个名为getCustomView的方法,该方法返回一个视图。
public View getCustomView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View layout = inflater.inflate(R.layout.spinner_cityfolder,parent,false);
TextView cityName = (TextView) layout.findViewById(R.id.tvLanguage);
ImageView cityImage = (ImageView) layout.findViewById(R.id.imgLanguage);
cityName.setText(foldersArrayList.get(position).getName());
cityImage.setImageResource(R.drawable.small_icon_bar);
return layout;
}
// It gets a View that displays in the drop down popup the data at the specified position
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
2)现在,在显示下拉菜单的活动中,我将一个名为dropDownSpinner的微调器作为实例,并传入citySpinner适配器。
citySpinner = new CitySpinner(getApplicationContext(),R.layout.spinner_cityfolder,Global_Class.getInstance().getValue().cityFoldersArrayList);
dropDownspinner.setAdapter(citySpinner);
3)然后我做onItemSelectedListener,在这里我试图获取他们单击的文件夹名称的文本。然后将其与我在某些全球数组列表中拥有的所有城市名称进行比较。如果它们匹配,请给我一些信息。但是问题是,当我在下面使用folderName.getText()时,它引用的是null。无论如何,我可以获得当前视图的文本吗?
dropDownspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)//Create a class that returns a custom View that's a textView of the city Name.
{
check++;
if(check > 1)
{
TextView folderName = (TextView) findViewById(R.id.tvLanguage);
for(CityFolders TempFolders: Global_Class.getInstance().getValue().cityFoldersArrayList)
{
if(adapterView.getAdapter().equals(citySpinner))
{
if(((CitySpinner) adapterView.getAdapter()).getCityName(i).equals(TempFolders.getName()))
{
//Toast.makeText(getApplicationContext(),((CitySpinner) adapterView.getAdapter()).getCityName(i),Toast.LENGTH_SHORT).show();
Spinner newDropDownSpiner = (Spinner) findViewById(R.id.spinner);
newDropDownSpiner.setAdapter(new CardSpinner(getApplicationContext(),R.layout.spinner_cards,TempFolders.getCards()));
//dropDownspinner.setAdapter(new CardSpinner(getApplicationContext(), R.layout.spinner_cards, TempFolders.getCards()));
break;
}
else
{
//Do nothing
}
}
else
{
Toast.makeText(getApplicationContext(),"Not the same type of spinner",Toast.LENGTH_SHORT).show();
}
}
}
else
{
//Do nothing
}
}
这是我的堆栈跟踪:
12-22 13:58:29.627 9553-9553 / com.lorentzos.swipecards.example E / AndroidRuntime:FATAL EXCEPTION:main
Process: com.lorentzos.swipecards.example, PID: 9553
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.lorentzos.swipecards.MyActivity$1$1.onItemSelected(MyActivity.java:176)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:914)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:903)
at android.widget.AdapterView.access$300(AdapterView.java:48)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5430)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
tvLanguage的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="75dp"
android:padding="3dip"
android:background="#434343">
<ImageView
android:id="@+id/imgLanguage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/tvLanguage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:text="Text Here"
android:textColor="#86f6e7">
</TextView>
</LinearLayout>
最佳答案
CitySpinner
是您的适配器,因此您可以通过以下方式进行操作:
dropDownspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Override
public void onItemSelected(AdapterView <? > adapterView, View view, int i, long l) //Create a class tha returns a custom View that's a textView of the city Name.
{
check++;
if (check > 1) {
TextView folderName = (TextView) view.findViewById(R.id.tvLanguage);
for (CityFolders TempFolders: Global_Class.getInstance().getValue().cityFoldersArrayList) {
if ( (adapterView.getAdapter() instanceof CitySpinner) && ((CitySpinner)adapterView.getAdapter()).getCityAt(i).equals(TempFolders.getName()))
{
dropDownspinner.setAdapter(new CardSpinner(getApplicationContext(), R.layout.spinner_cards, TempFolders.getCards()));
break;
} else {
//Do nothing
}
}
} else {
//Do nothing
}
}
并在适配器中提供
getCityAt
://...
public String getCityAt(int position){
return foldersArrayList.get(position).getName();
}
//...