user5679590
user5679590

Reputation:

Android: ArrayAdapter showing only one item even though the ListView isn't placed in a ScrollView

I am new to programming in Android. I encountered a problem where I need to print three items in a list View, yet getting only a single item. In many answers, I saw that ListView should't be placed in a ScrollView, I have rechecked my code, and that isn't actually the problem.

package com.apress.gerber.reminder;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class RemindersActivity extends AppCompatActivity {
private ListView mListView;
private String records[] = {"first record", "second record", "third record"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reminders);
    mListView = (ListView) findViewById(R.id.reminders_list_view);

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            R.layout.reminders_row,
            R.id.row_text,
            records);
    mListView.setAdapter(arrayAdapter);
}
}

Xml files:- Reminders_row.xml

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

    <view
        android:id="@+id/row_tab"
        class="android.view.View"
        android:layout_width="10dp"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_light"
        />


    <TextView
        android:id="@+id/row_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="@string/reminder"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:layout_gravity="center_vertical"
        android:padding="10dp"
        android:ellipsize="end"
        android:maxLines="1"/>

</LinearLayout>
</LinearLayout>

activity_reminders.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.apress.gerber.reminder.RemindersActivity">

<ListView
    android:id="@+id/reminders_list_view"
    android:layout_width="373dp"
    android:layout_height="50dp"
    android:background="@color/dark_grey"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"
    tools:listitem="@layout/reminders_row"/>

</RelativeLayout>

What actually is the error occuring?

Upvotes: 1

Views: 1455

Answers (1)

Yupi
Yupi

Reputation: 4470

If you are not going to extend your class with ArrayAdapter and customize getView method but you want to use your customized layout than your TextView must have id android:id="@android:id/text1" Or you can use android.R.layout.simple_list_item_1 in your ArrayAdapter like:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
        this,
        android.R.layout.simple_list_item_1,
        records);
mListView.setAdapter(arrayAdapter);

Also for your ListView

android:layout_width="match_parent"
android:layout_height="wrap_content"

Upvotes: 1

Related Questions