Android: Listview not showing any results

timbats1993

I have included my code to help clear things up. Basically I have two screens on my app, one will display a list, and the other will allow the user to add a new item to that list. Eventually I will incorporate database data to save the list view, but I do not require help with that. I have not yet coded the second screen, so I have not included the code for it.

The problem is that I cannot get the list view to display. I have even tried adding data to the array through code, with no luck. Can anyone see a problem here?

Issues.java

public class Issues extends Activity {

    public static ArrayList<String> strArray;
    public static ArrayAdapter<String> arrayAdapter;
    public static ListView serieslistview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.issues);

        strArray = new ArrayList<String>();

        Button btaddnewseries = (Button) findViewById(R.id.button1);
        serieslistview = (ListView) findViewById(R.id.listView1);

        //opens the add new series menu
        btaddnewseries.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(ComicIssues.this, AddSeries.class);
                Issues.this.startActivity(myIntent);
            }
        });

        strArray.add("Item One");

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

        serieslistview.setAdapter(arrayAdapter);
        arrayAdapter.notifyDataSetChanged();

}

issues.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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="+"
        tools:ignore="HardcodedText" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="30dp" >

    </ListView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="21dp"
        android:text="Issues"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Sami Eltamawy

Actually, I can't see any problem in your code. I will give you two things I have noticed.

1)Instead of listing your data into an ArrayList use String[]

2)You don't have to call notifyDataSetChanged() when you first give the adapter the input data. Only when you change the list of data later after. So instead of re-initialize the adapter, just call notifyDataSetChanged

Give this code a try and let me know if you still have a problem.

public class Issues extends Activity {

public static ArrayAdapter<String> arrayAdapter;
public static ListView serieslistview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.issues);

    Button btaddnewseries = (Button) findViewById(R.id.button1);
    serieslistview = (ListView) findViewById(R.id.listView1);

    //opens the add new series menu
    btaddnewseries.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(ComicIssues.this, AddSeries.class);
            Issues.this.startActivity(myIntent);
        }
    });

String[] values = new String[] { "Item 1", 
                                         "Item 2",
                                         "Item 3",
                                         "Item 4"
                                        };

    arrayAdapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1,values);

    serieslistview.setAdapter(arrayAdapter);

}

And use this .xml instead of yours

<?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" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="+"
    tools:ignore="HardcodedText" />

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_alignParentLeft="true"
    android:text="Issues"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/textView1" />

</RelativeLayout>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive