I'm just starting with Android and can't find how to display a list in my activity. The activity and the layout main.xml are shown below. How can I display the country array in the ListView 'list' of my layout?
thank you
Jul
public class Atable extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] COUNTRIES = new String[] { "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra"};
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, COUNTRIES)); //I tried this but it does not work
}
main.xml
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/search" />
<EditText android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="@id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
&开发者_如何转开发lt;TextView android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/noresults"/>
</LinearLayout>
</LinearLayout>
Here is a good working example
http://coderzheaven.com/index.php/2011/03/android-image-listview/
I think the easiest way is to override toString method in your restaurant class and use an ArrayAdapter< Restaurant> as your list content and then set it with setAdapter
Replace R.layout.main with android.R.layout.simple_list_item_1
http://developer.android.com/reference/android/app/ListActivity.html
I guess this is what you where trying to do:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class LayoutTest extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] COUNTRIES = new String[] {"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra"};
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, COUNTRIES)); //I tried this but it does not work
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/search"/>
<EditText android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/noresults"/>
</LinearLayout>
</LinearLayout>
精彩评论