Listview not showing?

I am trying to display a list of java lessons and created a listview for a button in my main java class in Android Studio but when I try to run and click the button, it opens another window but the listview doesn't show yet it is on the preview pane on Android Studio.

Here is my java class code:

package unitysoftwenteam.mymainuidesign;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.List;

public class JAVA_ACTIVITY extends AppCompatActivity {

ListView listview;
List list = new ArrayList();
ArrayAdapter adapter;

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

    listview = (ListView)findViewById(R.id.jtutoriallist);

    list.add("About Java");
    list.add("Print Hello Java");
    list.add("Variables");
    list.add("Updating Variables");
    list.add("Displaying the Output");
    list.add("String Variables");
    list.add("String Concatenation");
    list.add("Variable Names");
    list.add("Data Types");
    list.add("Variable Arithmetic");
    list.add("Casting");
    list.add("Control Flow");
    list.add("If Statement");
    list.add("Variable Scope");
    list.add("Else Statement");
    list.add("Else-If");
    list.add("Boolean Expressions");
    list.add("Logical Operators");
    list.add("Nested If Statements");
    list.add("Switch Statement");

    adapter = new ArrayAdapter(JAVA_ACTIVITY.this, 
    android.R.layout.simple_list_item_1);
    listview.setAdapter(adapter);
}
}

This is my XML code:

<?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"
tools:context="unitysoftwenteam.mymainuidesign.JAVA_ACTIVITY">

  <ListView
    android:id="@+id/jtutoriallist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />
</RelativeLayout>

Upvotes: 0

Views: 75

Answers (3)

A2N
A2N

Reputation: 160

You have missed to send your data to the adapter. the 'list' that you had created. a adapter requires data and view to bind and populate the same. you have specified the layout but data is missed.

just change it into,

adapter = new ArrayAdapter(JAVA_ACTIVITY.this, 
    android.R.layout.simple_list_item_1,list);
    listview.setAdapter(adapter);

Upvotes: 0

Ramesh Yogu
Ramesh Yogu

Reputation: 135

Try this Code

ListView listview;
List list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = new ArrayList();
    listview= (ListView) findViewById(R.id.jtutoriallist);

    lists = (ListView) findViewById(R.id.lists);
    list.add("About Java");
    list.add("Print Hello Java");
    list.add("Variables");
    list.add("Updating Variables");
    .
    .
    .

   ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list);
   lists.setAdapter(adapter);
  }

Upvotes: 0

Amit Darji
Amit Darji

Reputation: 457

Actually you done every thing right, but you just have to specify the list that you want to adapt with the ListView:

adapter = new ArrayAdapter(JAVA_ACTIVITY.this, android.R.layout.simple_list_item_1, list);

Upvotes: 1

Related Questions