Android自定义微调器SelectedItem

时间:2017-11-07 07:51:13

标签: android spinner

我做了一个微调器,我使用arrayadapter用图像和文本填充它。 on OnItemSelected我想只选择文本而不是图像。

这是我的MainActivity.java代码

ArrayList<ItemData> list = new ArrayList<>();

list.add(new ItemData("Select Plan Category",R.drawable.ic_bars));
list.add(new ItemData("Hookah",R.drawable.ic_006_hookah));
list.add(new ItemData("Drinks",R.drawable.ic_005_pint));
list.add(new ItemData("Gedi",R.drawable.ic_004_racing));
list.add(new ItemData("Snacks",R.drawable.ic_003_chips));
list.add(new ItemData("Shopping",R.drawable.ic_002_cart));
list.add(new ItemData("Bownling",R.drawable.ic_001_bowling));

SpinnerAdapter adapter = new SpinnerAdapter(getActivity(), R.layout.spinner_layout,R.id.categoryText,list);
categorySpinnerjava.setAdapter(adapter);

这是我的ItemData.java代码

public class ItemData {

    private String planCategorytext = "";
    Integer imageId;

    public ItemData(String text, Integer imageId)
    {
        this.planCategorytext=text;
        this.imageId=imageId;
    }

    public String getPlanCategorytext() {
        return planCategorytext;
    }

    public void setPlanCategorytext(String planCategorytext) {
        this.planCategorytext = planCategorytext;
    }

    public Integer getImageId() {
        return imageId;
    }

    public void setImageId(Integer imageId) {
        this.imageId = imageId;
    }
}

这是我的SpinnerAdapter.java代码(扩展了ArrayAdapter

int groupid;
Activity getActivity;
ArrayList<ItemData> list;

LayoutInflater inflater;

public SpinnerAdapter(Activity context, int groupid, int id, ArrayList<ItemData> list) {
    super(context, id,list);

    this.list=list;
    inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.groupid=groupid;

}

public View getView(int position, View convertView, ViewGroup parent)
{

    View itemView = inflater.inflate(groupid,parent,false);

    ImageView imageView=(ImageView)itemView.findViewById(R.id.categoryVector);
    imageView.setImageResource(list.get(position).getImageId());

    TextView textView = (TextView)itemView.findViewById(R.id.categoryText);
     textView.setText(list.get(position).getPlanCategorytext());


     return itemView;
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
    return getView(position,convertView,parent);
}

我想只选择文字而不是图像。而当我通常做

String spinnerselection = categorySpinnerjava.getSelectedItem().toString();

它给我输出“applicationpackage.ItemData@randomnumbers”

6 个答案:

答案 0 :(得分:1)

您收到此错误是因为

  

&#34; applicationpackage.ItemData@randomnumbers"

categorySpinnerjava.getSelectedItem(); 返回您的模型类 ItemData 而不是String

试试这个

ItemData itemData = (ItemData) categorySpinnerjava.getSelectedItem();
String data= itemData.getPlanCategorytext();
Integer imageId = itemData.getImageId();

答案 1 :(得分:0)

尝试使用Interface实施。

SpinnerAdapter内设置文字数据并在Interface课程中扩展Activity以获取所选文字

答案 2 :(得分:0)

尝试以下方法:

String spinnerselection = ((ItemData) categorySpinnerjava.getSelectedItem()).getPlanCategorytext();

还没有尝试过,但理论上它应该可行。

答案 3 :(得分:0)

适配器中的

Override getItem()。导致getSelectedItem()返回:

  

adapter.getItem(position);

@Override
public String getItem(int position) {
    return list.get(position);
}

答案 4 :(得分:0)

试试这个,

 ItemData spinnerselection = (ItemData)categorySpinnerjava.getSelectedItem();
String plan = spinnerselection.getPlanCategorytext();

答案 5 :(得分:0)

您可以通过以下方式获取所选微调器项目的文本:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
            String selectedText=list.get(pos).getPlanCategorytext();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

String selectedItemText=list.get(spinner.getSelectedItemPosition()).getPlanCategorytext();

其中list是带有ItemData的arraylist,您在上面用它来填充微调器中的数据。

还有关于此:

String spinnerselection = categorySpinnerjava.getSelectedItem().toString();

您必须将categorySpinnerjava.getSelectedItem().toString();替换为list.get(spinner.getSelectedItemPosition()).getPlanCategorytext();