2017-06-07 22 views
1

Ich bin neu in der Programmierung in Android. Ich bin auf ein Problem gestoßen, bei dem ich drei Elemente in einer Listenansicht drucken muss und dabei nur ein einzelnes Element erhalte. In vielen Antworten habe ich gesehen, dass ListView nicht in einem ScrollView platziert werden soll, ich habe meinen Code nochmals überprüft, und das ist eigentlich nicht das Problem.Android: ArrayAdapter zeigt nur ein Element, obwohl das ListView nicht in einem ScrollView platziert wird

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-Dateien: - 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> 

Was der Fehler auftritt tatsächlich ist?

Antwort

0

Wenn Sie nicht mit ArrayAdapter gehen, um Ihre Klasse zu erweitern und getView Methode anpassen, aber Sie möchten, dass Ihre angepassten Layout verwenden, als Ihr TextView müssen id android:id="@android:id/text1" Oder können Sie android.R.layout.simple_list_item_1 in Ihrem ArrayAdapter wie verwenden:

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

auch für Ihre ListView

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

Dank .. es ist ein sehr dummer Fehler war, den ich das Listview tat, konnte ich das nicht fangen. –

+0

Großartig! Ihre Begrüßung. – Yupi

Verwandte Themen