Reputation: 2725
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?
Upvotes: 3
Views: 53090
Reputation: 4580
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();
}
}
Upvotes: 17
Reputation: 527
<?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>
Upvotes: 1
Reputation: 8101
It sounds like you need to take a look at the documentation again.
It is very well described there.
Upvotes: -3