Reputation: 4422
I wanted to generate a list view using below code. But after running this code the screen just shows a blank screen and the "hello" text which is there in main layout
package com.android.test1;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
public class HelloListView extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String a[] = new String[]{"a", "b", "c", "d"};
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
}
and the main layout code is as given below
<?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" >
<TextView
android:text="hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF">
</ListView>
</LinearLayout>
Upvotes: 0
Views: 211
Reputation: 4433
Archie.bpgc see this tutorial http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGallery.php#ListView
if you extend Listactivity you do not add
setContentView(R.layout.main);
and refer it like
ListView listView = (ListView) findViewById(R.id.list_view);
and if you do it like this
public class Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.list_view);
String a[] = new String[]{"a", "b", "c", "d"};
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
android.R.id.text1 ,
a));
// ListView lv = getListView();
listView.setTextFilterEnabled(true);
}
}
you have set the list background
android:background="#FFFFFFFF"
By default the color of list text is white and you have given white background to list so the items does not appear
Upvotes: 0
Reputation: 34765
In your code Just comment the below line
setContentView(R.layout.main);
In this case use the below link for using custom adapter.
Upvotes: 0
Reputation: 2290
You are setting the adapter to the current activity, but it's view isn't a ListView but a LinearLayout. Try this to set the adapter to the correct view.
String a[] = new String[]{"a", "b", "c", "d"};
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a));
Upvotes: 0
Reputation: 13501
Your ListView in XML should have this android:id="@android:id/list"
and remove that background attribute..
Upvotes: 2