3

I want to make a simple app that shows a list of contacts (name, surname). My code:

package lista.android;

import java.util.*;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.*;

class Contact{
    String nume;
    String prenume;

    Contact(String nume, String prenume){
        this.nume=nume;
        this.prenume=prenume;
    }

    public String toString(){
        return prenume +" "+ nume;
    }
}

public class Lista1Activity extends ListActivity {
    /** Called when the activity is first created. */
    ArrayList <Contact> lista;
    ArrayAdapter <Contact> adaptor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setListAdapter(adaptor);

        lista=new ArrayList<Contact>();
        adaptor=new ArrayAdapter<Contact>(this, R.id.element, lista);

        adaugaContact("Florian", "Iancu");
        adaugaContact("Ioana", "Constantina");

    }

    public void adaugaContact(String nume, String prenume){
        lista.add(new Contact(nume, prenume));
        adaptor.notifyDataSetChanged();
    }
}

In the XML I have the LinearLayout and a TextView that is the list element. When I run it, the simulator says "Sorry, the application [...] has stopped unexpectedly. Please try again." The LogCat tells me I have to have a ListView whose id is android.R.id.lista. If I create a random ListView field in the XML file and give it the "lista" id, it still doesn't work. How to I call that ListView from XML to match something in my Java code? In other words, I know I'm wrong, but where and how do I fix it?

2
  • 2
    You are trying to set List Adapter first and then creating it. Don't you think this is the wrong!! Commented Nov 21, 2011 at 9:42
  • 1
    You need to understand the concept of creating listview first i think. Commented Nov 21, 2011 at 9:42

3 Answers 3

17

In the LinearLayout you have to specify the ListView component.

This is how your main.xml should be:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/sampleListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:divider="#CCCCCC"
        android:dividerHeight="1dp"
        android:paddingLeft="2dp" >
    </ListView>

</LinearLayout>

Then for the listview make a layout: list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="10dp"    android:textSize="16sp" >
</TextView>

Change your activity class like this:

public class Lista1Activity extends ListActivity {
    ArrayList<Contact> lista;
    ArrayAdapter<Contact> adaptor;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lista = new ArrayList<Contact>();
    adaugaContact("Florian", "Iancu");
    adaugaContact("Ioana", "Constantina");

     adaptor = new ArrayAdapter<String>(this, R.layout.list_item, lista);

    setListAdapter(adaptor);
    }

    public void adaugaContact(String nume, String prenume) {
    lista.add(new Contact(nume, prenume));
    adaptor.notifyDataSetChanged();
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

As I stated, I have tried adding a listview field in my XML, but it has to be tied to my Java code. Where do I tie to it and how do I do it? Thanks!
Ok, so you suggest instantiating "adaptor" before using it. It's a good point, but it still doesn't work, same exception: "need to have a listview whose id is android.R.id.list". The documentation says something about ListView lv = getListView(); How do I get that one into my code?
I have edited the answer. Since you have extended a ListActivity, ListView lv = getListView(); would instantiate the ListView in your activity.
You can use list item designs provided by the platform instead of defining your own layout file (list_item.xml ) for the ListAdapter. For example, try using android.R.layout.simple_list_item_1 instead of R.layout.list_item.
And I also should have overwritten the onListItemClick() function. Thanks for your time and answers! :D
1
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="#000000"
    android:dividerHeight=".5dpdp"
    android:paddingLeft="1dp" />
</LinearLayout>

1 Comment

As I stated, I have tried adding a listview field in my XML, but it has to be tied to my Java code. Where do I tie to it and how do I do it? Thanks!
-3

It sounds like you need to take a look at the documentation again.

It is very well described there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.