更改微调项文本
Change spinner items text
我有一个操作栏微调器示例项目。它工作正常。现在我正在尝试改变一些事情,比如:
当我打开我的应用程序时,微调器显示以下内容:
默认选择本地子项目,这很好,但我想在选择项目时将本地文本更改为其他内容 本地 这可能吗?
我什么时候打开应用程序?默认情况下,它显示第一项文本,如上面的屏幕。我想改变一些东西,比如 TITLE 将是 ITEMS 而 Sub title 将是 SubItems (第一个项目的默认选择很好 - 只是我不我不想显示本地文本,而是想显示 SubItems.
现在显示 ITEMS 和 Local - 只有在我单击 Local[= 时才应更改此本地文本47=] 来自微调器的项目。
微调器随项目展开时的文本应该相同。如下所示:
我认为它可以在 Adapter 中实现,但不确定如何:
这是我的自定义适配器 java 文件:
public class SubItemNavigationAdapter extends BaseAdapter
{
private TextView txtTitle;
private ArrayList<SpinnerNavItem> spinnerNavItem;
private Context context;
private TextView txtHeading;
public SubItemNavigationAdapter(Context context, ArrayList<SpinnerNavItem> spinnerNavItem)
{
this.spinnerNavItem = spinnerNavItem;
this.context = context;
}
@Override
public int getCount()
{
return spinnerNavItem.size();
}
@Override
public Object getItem(int index)
{
return spinnerNavItem.get(index);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtTitle.setPadding(20, 20, 0, 20);
txtHeading = (TextView) convertView.findViewById(R.id.txtheading);
txtHeading.setVisibility(View.GONE);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
}
告诉我!
谢谢!
只需使用您的 getView()
方法来显示选定的下拉项。
所以你会有类似的东西
txtTitle.setText(spinnerNavItem.get(position).getDisplayTitle()); // <--- extend SpinnerNavItem with displaytitle
在您的 getView()
方法中。而已。 :-)
我有一个操作栏微调器示例项目。它工作正常。现在我正在尝试改变一些事情,比如:
当我打开我的应用程序时,微调器显示以下内容:
默认选择本地子项目,这很好,但我想在选择项目时将本地文本更改为其他内容 本地 这可能吗?
我什么时候打开应用程序?默认情况下,它显示第一项文本,如上面的屏幕。我想改变一些东西,比如 TITLE 将是 ITEMS 而 Sub title 将是 SubItems (第一个项目的默认选择很好 - 只是我不我不想显示本地文本,而是想显示 SubItems.
现在显示 ITEMS 和 Local - 只有在我单击 Local[= 时才应更改此本地文本47=] 来自微调器的项目。
微调器随项目展开时的文本应该相同。如下所示:
我认为它可以在 Adapter 中实现,但不确定如何:
这是我的自定义适配器 java 文件:
public class SubItemNavigationAdapter extends BaseAdapter
{
private TextView txtTitle;
private ArrayList<SpinnerNavItem> spinnerNavItem;
private Context context;
private TextView txtHeading;
public SubItemNavigationAdapter(Context context, ArrayList<SpinnerNavItem> spinnerNavItem)
{
this.spinnerNavItem = spinnerNavItem;
this.context = context;
}
@Override
public int getCount()
{
return spinnerNavItem.size();
}
@Override
public Object getItem(int index)
{
return spinnerNavItem.get(index);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtTitle.setPadding(20, 20, 0, 20);
txtHeading = (TextView) convertView.findViewById(R.id.txtheading);
txtHeading.setVisibility(View.GONE);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
}
告诉我!
谢谢!
只需使用您的 getView()
方法来显示选定的下拉项。
所以你会有类似的东西
txtTitle.setText(spinnerNavItem.get(position).getDisplayTitle()); // <--- extend SpinnerNavItem with displaytitle
在您的 getView()
方法中。而已。 :-)