-2

I'm trying to create a listview on an activity that contains pressable buttons that will display an image. However, I'm running into problems creating a plain list view of just text. My code compiles correctly, but when ran on the phone, it crashes when I click to open up the activity. Am I missing something important in my code, or did I forget to add something somewhere else?

public class druglist extends AppCompatActivity {

String[] fulldruglist = {"Aspirin","Acetaminophen","Activated Charcoal","Adenosine",
        "Afrin","Albuterol","Amiodarone","Aspirin"};


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

    ArrayAdapter adapter = new ArrayAdapter<String>(this,
            R.layout.activity_druglist, fulldruglist);

    ListView listView = (ListView) findViewById(R.id.druglistview);
    listView.setAdapter(adapter);
}}
beans217
  • 139
  • 2
  • 11
  • Please post your logcat log here. Also - per convention - class names should be in UpperCamelCase (druglist -> DrugList), non-final variables should be in lowerCamelCase (fulldruglist -> fullDrugList). – Ch4t4r Jan 11 '18 at 22:12

1 Answers1

0

Here lies your issue:

ArrayAdapter adapter = new ArrayAdapter<String>(this,
        R.layout.activity_druglist, fulldruglist);

here the layout you need to provide is the layout used for the item. but 2 lines above you have :

setContentView(R.layout.activity_druglist);

saying that the layout you provide is the layout used for the container

let's assume that you defined your list in layout R.layout.activity_druglist and that you defined the layout of each entry of your list in layout R.layout.activity_druglist_item, you should write:

ArrayAdapter adapter = new ArrayAdapter<String>(this,
        R.layout.activity_druglist_item, fulldruglist);

Update: Here is some minimalistic code to display your listview :

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private ListView myListView;

    String[] fulldruglist = {"Aspirin","Acetaminophen","Activated Charcoal",
        "Adenosine", "Afrin","Albuterol","Amiodarone","Aspirin"};

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

        myListView = findViewById(R.id.myListView);

        ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.item,
            R.id.textView, fulldruglist);
        myListView.setAdapter(adapter);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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">

    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Text" />

</android.support.constraint.ConstraintLayout>

activity_main is the layout of the activity containing the listview

item is the layout of the cell's content

textView is the TextView that will be used to display each entry of your fulldruglist

References: here or the official documentation

AdricoM
  • 579
  • 4
  • 11
  • Could you explain this a little more? Should I have a separate file that contains the 'list items' to call from? Also, what does the second entry of 'R.layout....' represent? – beans217 Jan 12 '18 at 17:28
  • updated my answer : `activity_main` is the layout of the activity containing the listview, `item` is the layout of the cell's content, `textView` is the `TextView` that will be used to display each entry of your `fulldruglist`, please see [here](https://stackoverflow.com/questions/19079400/arrayadapter-in-android-to-create-simple-listview) or [the official documentation](https://developer.android.com/reference/android/widget/ArrayAdapter.html) – AdricoM Jan 13 '18 at 09:59