0

This My Main Class With Adapter

 ListView listView;
 Bean bean;
 ArrayList<Bean> arrayList;
 ArrayAdapter<Bean> arrayAdapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.list_item);
    arrayList = new ArrayList<>();
    bean= new Bean("demo");
   arrayList.add(bean);
   arrayAdapter= new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(arrayAdapter); }

This My Bean Class

  public class Bean {

     String name;


     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public Bean(String name)
     {
        this.name=name;
     }
}

The list is shown as below on my app:

Please help me in resolving this issue am trying many of time but still same error

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • You're seeing the default implementation of [`toString()`](https://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) - Override it and change it to something more useful – PPartisan Mar 21 '19 at 15:18
  • Possible duplicate of [How to override toString() properly in Java?](https://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java) – PPartisan Mar 21 '19 at 15:19

2 Answers2

0

Override toString method for your Bean class.

Tomislav29
  • 78
  • 1
  • 6
0

To fully customize the way you want to display your data, you can override the ArrayAdapter definition to create yours.

Custom layout : item_bean.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >
    <!-- Here, you can display index for example -->
    <TextView
      android:id="@+id/tvIndex"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:style="@style/styleIndex" />
    <TextView
      android:id="@+id/tvName"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:style="@style/styleName" /> <!-- You can use your own style -->
</LinearLayout>

Custom Adapter: BeansAdapter

public class BeansAdapter extends ArrayAdapter<Bean> {
    public UsersAdapter(Context context, ArrayList<Bean> beans) {
       super(context, 0, beans);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // Get the data item for this position
       Bean bean = getItem(position);    
       // Check if an existing view is being reused, otherwise inflate the view
       if (convertView == null) {
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_bean, parent, false);
       }
       // Lookup view for data population
       TextView tvIndex = (TextView) convertView.findViewById(R.id.tvIndex);
       TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
       // Populate the data into the template view using the data object
       tvIndex.setText(position);
       tvName.setText(bean.getName());
       // Return the completed view to render on screen
       return convertView;
   }
}
Bruno
  • 3,872
  • 4
  • 20
  • 37