Einfache Frage. ListView wird in meiner Vorschau für Android Studio angezeigt, aber wenn ich die App starte, erscheint sie leer.ArrayAdapter nicht in ListView angezeigt - Android-Studio
Ich habe einen Arrayadapter, der einige Daten aus einem Array zieht und dann in die ListView in der Aktivität drückt. Es soll eine Liste von Menüpunkten anzeigen, durch die der Benutzer in der Aktivität blättern kann. Kein solches Glück.
Ich habe versucht, mit ein paar Features der XML-Datei zu spielen, aber nichts wurde behoben, also hier bin ich.
Hier ist meine XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.ericleeconklin.sleepnoise2.ListSounds"
tools:showIn="@layout/activity_list_sounds">
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ListActivity">
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
Und hier ist mein Java
package com.ericleeconklin.sleepnoise2;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListSounds extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_sounds);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public class ListDisplay extends Activity {
// Array of strings...
String[] mobileArray = {"Waterfall","Fire","Fan","River","Heavy `Rain","Light Rain","Lawnmower","Restaurant"};`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_list_sounds);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
}
nur versuchen, Ihren Code in erster onCreate zu kopieren und zweiten entfernen Klasse .. es ist nutzlos .. sonst machen Schnittstelle oder rief Ihre List-Klasse zu Ihrer MainClass, Ihr Code ist in Ordnung, aber Sie rufen List Aktivität zu inneren Klasse, deshalb seine leere –
Ah ich sehe. Ich habe das zweite Mal nicht bemerkt. Guter Fang. –