我想创建一个简单的应用程序,显示联系人列表(姓名,姓氏)。我的代码:
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();
}
}
在XML中,我有LinearLayout
和TextView
作为列表元素。当我运行它时,模拟器说"Sorry, the application [...] has stopped unexpectedly. Please try again."
LogCat告诉我必须有一个ListView
,其id为android.R.id.lista。如果我在XML文件中创建一个随机ListView
字段并给它“lista”id,它仍然不起作用。如何从XML调用ListView以匹配我的Java代码中的内容?换句话说,我知道我错了,但我在哪里以及如何解决它?
答案 0 :(得分:17)
在LinearLayout中,您必须指定ListView组件。
main.xml 应该是这样的:
<?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>
然后为listview制作一个布局: 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>
更改活动类,如下所示:
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();
}
}
答案 1 :(得分: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>
答案 2 :(得分:-3)
听起来你需要再次看一下the documentation。
那里有很好的描述。