Wai Yan Hein
Wai Yan Hein

Reputation: 14771

Why ListView with custom adapter showing nothing?

I am absolute beginner to Android Studio. I am now learning about list view. When I use list view with just simple array adapter. It is showing fine. Now I am trying to create a custom adapter with custom row layout.

But when I run the app, it is not throwing error. But activity is showing nothing on the screen. I just got the blank screen and no data is bind to list view. What is wrong with my code?

This is my activity class

public class CustomAdapterActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.custom_adapter);

        ListItem item_1 = new ListItem();
        item_1.Name = "Name 1";
        item_1.Email = "Email 1";
        item_1.Phone = "Phone 1";

        ListItem item_2 = new ListItem();
        item_1.Name = "Name 2";
        item_1.Email = "Email 2";
        item_1.Phone = "Phone 2";

        ListItem item_3 = new ListItem();
        item_1.Name = "Name 3";
        item_1.Email = "Email 3";
        item_1.Phone = "Phone 3";

        ListItem item_4 = new ListItem();
        item_1.Name = "Name 4";
        item_1.Email = "Email 4";
        item_1.Phone = "Phone 4";

        ListItem[] items = { item_1 , item_2,item_3 , item_4 };

        ArrayAdapter adapter = new ArrayAdapter<ListItem>(this,R.layout.custom_row,items);
        ListView listView = (ListView)findViewById(R.id.custom_listview);
        listView.setAdapter(adapter);
    }
}

This is my row model class named ListItem

public class ListItem {

    public String Name;
    public String Email;
    public String Phone;

}

This is main layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/custom_listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

This is the layout for row.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="name"
        android:id="@+id/row_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>

    <TextView
        android:layout_below="@+id/row_name"
        android:id="@+id/row_email"
        android:text="email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>

    <TextView
        android:layout_below="@+id/row_email"
        android:id="@+id/row_phone"
        android:text="phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>
</RelativeLayout>

This is my custom adapter class.

public class MyCustomAdapter extends ArrayAdapter<ListItem> {
    private final Context context;
    private final ListItem[] values;

    public MyCustomAdapter(Context context,ListItem[] values)
    {
        super(context,-1,values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.custom_row, parent, false);
        TextView name_tf = (TextView)rowView.findViewById(R.id.row_name);
        TextView email_tf = (TextView)rowView.findViewById(R.id.row_email);
        TextView phone_tf = (TextView)rowView.findViewById(R.id.row_phone);
        name_tf.setText(values[position].Name);
        email_tf.setText(values[position].Email);
        phone_tf.setText(values[position].Phone);
        return rowView;
    }
}

No data is bind to ListView while I run the app.

Then I followed to Sankar V answer. It worked. But it still has a slight problem like in image

enter image description here

Why only item_4 is bind to listview?

Upvotes: 0

Views: 847

Answers (1)

Sankar V
Sankar V

Reputation: 4863

Please use the public void onCreate(Bundle savedInstanceState) version of onCreate method instead of public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

The later is available only in Lollipop and above.

And also create instance of MyCustomAdapter instead of Generic ArrayAdapter.

ArrayAdapter adapter = new MyCustomAdapter<ListItem>(this,R.layout.custom_row,items);

Set data properly. You are creating 4 different objects but setting values for only first object. Change it like below

        ListItem item_1 = new ListItem();
        item_1.Name = "Name 1";
        item_1.Email = "Email 1";
        item_1.Phone = "Phone 1";

        ListItem item_2 = new ListItem();
        item_2.Name = "Name 2"; // This need to be changed from item_1 to item_2
        item_2.Email = "Email 2"; // This need to be changed from item_1 to item_2
        item_2.Phone = "Phone 2"; // This need to be changed from item_1 to item_2

        ListItem item_3 = new ListItem();
        item_3.Name = "Name 3"; // This need to be changed from item_1 to item_3
        item_3.Email = "Email 3"; // This need to be changed from item_1 to item_3
        item_3.Phone = "Phone 3"; // This need to be changed from item_1 to item_3

        ListItem item_4 = new ListItem();
        item_4.Name = "Name 4"; // This need to be changed from item_1 to item_4
        item_4.Email = "Email 4"; // This need to be changed from item_1 to item_4
        item_4.Phone = "Phone 4"; // This need to be changed from item_1 to item_4

Upvotes: 1

Related Questions