I'm trying to create a ListView that displays 2 TextViews per row.
However I keep getting a NullPointerException when opening the Activity and I have no idea why (logCat logs don't tell me which line(s) on my end create the problem).
Here's the relevant code:
The main class EventViewActivity
public class EventViewActivity extends ListActivity {
public String[] eventTitles;
#Override
public void onCreate(Bundle savedInstanceState) {
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.eventview_layout);
eventTitles = new String[]{
getResources().getString(R.string.eventtitle1),
getResources().getString(R.string.eventtitle2),
getResources().getString(R.string.eventtitle3),
getResources().getString(R.string.eventtitle4),
getResources().getString(R.string.eventtitle5)
};
setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_title, eventTitles));
setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_descr, eventTitles));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "You have clicked an item", Toast.LENGTH_SHORT).show();
}
}
layout.xml is:
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView android:id="#+id/android:eventlist" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/eventview_background"/>
</LinearLayout>
Thanks!
does your eventview_layout has a ListView with id android:id="#android:id/list" ..? if not add it.. i assume its not done
a small correction..
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/eventview_background"/>
</LinearLayout>
its necessary that the contentView of ListACtivity has a listView with exact same Id..
I think the way you are approaching for ListView is not the correct.
Please have A look on this sample LINK to have complete view how create a ListView with custom adapter.
The above link has sample application created with listview and each listitem having two text fields as your requirement.
You should call the:
super.onCreate();
first!
Related
I'm trying to change the image of my listview dynamically.
But I can't figure how to change it because using setImageResource doesn't work.
Here's my code snippet:
public void onCreate(Bundle savedInstanceState) {
ImageView imgIco=(ImageView)findViewById(R.id.imgIco);
Integer id =getIntent().getExtras().getInt("id");
if(id==2) {
String[] valenzeComposti = getResources().getStringArray(R.array.int_valenze);
// I cannot do this ---------------> imgIco.setImageResource(R.drawable.ico1);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2,R.id.txtItem, valenzeComposti));
ListView lv2 = getListView();
lv2.setTextFilterEnabled(true);
lv2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v,int position, long id) {
Intent intent = new Intent(getApplicationContext(),InterClassi.class);
intent.putExtra("id",(int)id);
startActivity(intent);
}
});
}
The listview are created as follows -
The listview xml (R.layout.list_item2)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="5dip">
<ImageView
android:id="#+id/imgIco"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginRight="5dp"
>
</ImageView>
............
</RelativeLayout>
Please help me.. Thank you in advance
Maybe you should make you own implementation of ListAdapter. The most common way is extending BaseAdapter. List8 in the API Demos is a good example for making custom ListAdapters.
u may create your custom list view by extending baseadapter class to dynamically pass resources to listview. Have a look at this link-http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
hope this helps.
following is my ListActivity, onCreate code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fakeTripBuilder();
setContentView(R.layout.cutom_list_view);
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.custom_row_view,
new String[] {"stop","distance","time","icon","mode"},
new int[] {R.id.text_stop,R.id.text_distance, R.id.text_time, R.id.icon, R.id.text_mode});
populateList();
setListAdapter(adapter);
}
inside the the same class i have the following method
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Log.d("Travel","You choosed!");
Object o = this.getListAdapter().getItem(position);
Toast.makeText(this, "You Choosed"+o.toString(), Toast.LENGTH_LONG).show();
}
and here is the "custom_list_view.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="wrap_content"
android:clickable="false"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
/>
</LinearLayout>
Problem is, onListItemClick method do not call when i click on it. Help !!
To get a reference to the ListView in a ListActivity you should call:
ListView lv = getListView();
You do not need to set a click listener, it already has one. Simply override:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Object item = lv.getAdapter().getItem(position);
}
And in your xml file, the ListView should have the id:
android:id="#android:id/list"
Edit
You need to remove
android:clickable="false"
from the layout to allow any child views to be clicked
A lot has changed since this question was asked in 2012. This is an update to the accepted answer
To use onItemClickListener in your ListActivity, just override the onListItemClick(...) method like this:
public void onListItemClick(ListView listView, View view, int position, long id) {
ListView myListView = getListView();
Object itemClicked = myListView.getAdapter().getItem(position);
Toast.makeText(getApplicationContext(), "Item Clicked: " + item + " \nPosition: " + id,
Toast.LENGTH_SHORT).show();
}
Notice the getItem() method instead of getItemAtPosition() in the accepted answer
Remember: for this to work, you need to have a listview with id list, else your app will crash.
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
/>
Try overriding the onListItemClick method if you want to catch click events on your list.
ListView lv = (ListView) findViewById(R.layout.cutom_list_view);
You should use R.id.listview_id instead, that should be defined in R.layout.cutom_list_view
like so:
<ListView
android:id="#+id/listview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
EDIT:
R.layout.cutom_list_view is id of your layout file, but you should find listview by its own id, defined like android:id="#+id/listview_id", so the id will be R.id.listview_id
I wanted to create a search view like the one Google uses. For this I created the following XML layout, which basically is a search bar and a button in the upper section of the screen and a ListView at the bottom of it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutSearch"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FF394952">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<EditText android:layout_height="wrap_content" android:id="#+id/searchTextBar" android:layout_width="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/searchButton" android:text="Buscar"></Button>
</LinearLayout>
<ListView
android:id="#+id/searchResultList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0" />
</LinearLayout>
And this is the code of the textViewResource that the ArrayAdapter demands on its constructor:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
Now, this is the code of the activity. So far, I just want to display the view with the contents (that's why I'm using a static String array for now).
public class SearchActivity extends Activity{
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item, COUNTRIES);
ListView lv = (ListView)this.findViewById(R.id.searchResultList);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
However, when I run the activity I see the search bar but it doesn't display the ListView.
I've tried changing the extension of SearchActivity to ListActivity, but the program just crashes when I try to start it. I'm also aware of the existence of the Search Interface, but I just want to see this particular method work.
Why it doesn't display the contents of the ListView? Is there a way to fix this?
Thanks in advance
If you are going to use ListActivity you should be aware that ListActivity already has a ListView instance. You need to call its setListAdapter method to set the adapter for its ListView instead of instantiating your own ListView and setting the adapter on it. You can call getListView to get a handle on ListActvity's ListView and then set the click listener on that.
If you want to extend ListActivity then you must have a ListView with id #android:id/list. Change the id of your ListView, that should fix the crash when extending ListActivity.
I want to change text color of the list item ,below is my code
public class HelloListView extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, name));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}); }}
My List.xml
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="Empty set"
/>
< /LinearLayout >
Thanks :)
You are currently using an android built-in row component as a view for each row :
android.R.layout.simple_list_item_1
If you want to customize it, pick the code and put your own version in your app. The original code can be found on google code project for android :
Then you can customize xml and change the color attribute or change for a new selector state.
If you want to something even more custom, override your list's adapter getView method and provide a custom component inflating this xml and providing additional methods to be filled and display your data for each row.
Regards,
Stéphane
If you want to change the appearance of the elements in the list, Use the CustomListView and CustomAdapter.
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html
This Links may help you......
I'm still having a problem with a ListView click handler. The event is not being fired, so
I do not see the debugging message.
I want to use Activity instead of ListActivity
Here again is my code:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Capture our button from layout
//appState = ((MyApp)getApplicationContext());
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.listr);
//setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.fishicon);
setupDB();
populateList3();
ListView lv = (ListView) findViewById(R.id.list);
// lv.setClickable(true);
lv.setOnItemClickListener(new OnItemClickListener() {
//#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListRecords.this,"Clicked!", Toast.LENGTH_LONG).show();
}
});
Here is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="fill_parent">
<!--Put form controls here-->
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="400dp" />
<Button
android:id="#+id/previousbutton"
android:gravity="center_horizontal"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:text="Previous Menu"/>
</LinearLayout>
I cannot figure out why the click listener is not working.
Thanks
Mike
Don't use inline listeners unless you're sure of how to use them - they can go out of scope and cause a variety of problems.
Try this...
public class MyActivity extends Activity
implements AdapterView.onItemClickListener {
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listr);
ListView lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(this);
// Do whatever else you need to do
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do whatever
}
}
I would uncomment the #Override. It is there to guarantee that your signature matches the method you're trying to override.
If your method signature is correct, then I suspect the problem lies in populateList3(). Could you post that code as well?
You're not registering the list for the context menu: http://developer.android.com/reference/android/app/Activity.html#registerForContextMenu(android.view.View)