-2

i'm working baseadapter.i have custom baseadapter.i use my adapter in spinner.i have one problem.mybaseadapter's OnItemClickedListener returned all my custom class

carbon.widget.Spinner$CustomClass@535ca050s

this is a result this is a my code

  public static class CustomClass {
    private String Name;

    public void setName(String name)
    {
        this.Name=name;
    }
    public String getName()
    {
        return Name;
    }
    public CustomClass(String name)
    {
        this.Name=name;
    }
}

public static class Adapter extends RecyclerView.Adapter<ViewHolder, CustomClass> {

   private ArrayList<CustomClass>items=new ArrayList<>();

    @Override
    public CustomClass getItem(int position) {
        return items.get(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.carbon_popup_row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        holder.tv.setText(items.get(position).getName());
        Log.e("position String", items.get(position).getName() + "s");
    }

    @Override
    public int getItemCount() {
        return items.size();
    }


    public void setItems(ArrayList<CustomClass> items) {
        this.items = items;
        notifyDataSetChanged();
    }
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    TextView tv;

    public ViewHolder(View itemView) {
        super(itemView);
        tv = (TextView) itemView.findViewById(R.id.carbon_itemText);

    }
}

i inserted some values in my baseadapter like this

       for (int i = 0; i < 40; i++) {
        CustomClass customClass = new CustomClass("item" + i);
        // customClass.setName("beka" + i);
        list.add(customClass);
    }

    Spinner day = (Spinner) findViewById(R.id.day);
    day.setItems(list);

this is a my setItem method

 public void setItems(ArrayList<CustomClass> items) {
    popupMenu.setAdapter(defaultAdapter);
    defaultAdapter.setOnItemClickedListener(onItemClickedListener);
    defaultAdapter.setItems(items);
}




  RecyclerView.OnItemClickedListener onItemClickedListener = new RecyclerView.OnItemClickedListener() {
    @Override
    public void onItemClicked(int position) {
        setText(popupMenu.getAdapter().getItem(position).toString());
        Log.e("position String",popupMenu.getAdapter().getItem(position).toString()+"s");
        popupMenu.dismiss();
    }
};

as i said i can't return my custom class's getName. how i can solve my problem? if anyone knows solution please help me p.s i try to rewrite this example

https://github.com/ZieIony/Carbon

in this example autor used String arrays but i want to use my Custom Array how i can solve my problem?

donoachua
  • 193
  • 2
  • 16
  • Ask your question properly – Jas Oct 26 '15 at 10:30
  • @Jas when i log in my log i can't show myCustomClass'getName value (pleasee see where i use Log.e).carbon.widget.Spinner$CustomClass@535ca050s this is a output – donoachua Oct 26 '15 at 10:37

1 Answers1

0

popupMenu.getAdapter().getItem(position) is returning CustomClass in your, and since you didn't override toString() in it, you are seeing the default implementation of Object.toString(). For the sake oh showing the name you could override it and return name. E.g.

@Override
public String toString() {
   return name != null ? name : "no name set";
}

Alternatively you can simply call popupMenu.getAdapter().getItem(position).getName()

E.g.

CustomClass objectAtPos = popupMenu.getAdapter().getItem(position);
 if (objectAtPos != null) {
    setText(objectAtPos.getName());
    Log.e("position String",objectAtPos.getName()+"s");
 }
 popupMenu.dismiss();
Blackbelt
  • 156,034
  • 29
  • 297
  • 305