Android ListView - click handler not working - android

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)

Related

Listview Click doesn't fire

I have a listview, whick get its data from a SQLite db. I would like to let user change value of field by click on items. But with following code, the click event is not fired. Could some one help on it? Thanks!
public class BDXDeviceActivity extends Activity {
private DeviceListControl deviceListControl;
private ListView listViewDevice;
private DeviceListAdapter2 deviceListAdapter2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// using a custom layout, otherwise a system default of List
setContentView(R.layout.activity_device_list);
listViewDevice = (ListView) findViewById(R.id.listViewDevice);
deviceListControl = new DeviceListControl(this);
// setListAdapter(new DeviceListAdapter2(this, deviceListControl.getDevices()));
deviceListAdapter2 = new DeviceListAdapter2(this, deviceListControl.getDevices());
listViewDevice.setAdapter(deviceListAdapter2);
listViewDevice.setClickable(true);
listViewDevice.setOnItemClickListener(onItemClickListener);
}
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), "clicked", Toast.LENGTH_SHORT).show();
DeviceName deviceName = (DeviceName) listViewDevice.getAdapter().getItem(position);
deviceListControl.deleteDevice(deviceName.getId());
// after delete, reread the list adapter and get names list via namechange
deviceListAdapter2 = new DeviceListAdapter2(getBaseContext(), deviceListControl.getDevices());
listViewDevice.setAdapter(deviceListAdapter2);
// Display success information
Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_LONG).show();
}
};
... layout file
<EditText
android:id="#+id/txtSearchDevice"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#f7f5b1a3"
android:focusable="false"
/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/btnSearchDevice"
android:textColor="#ff151515"
android:background="#android:drawable/ic_menu_search"
android:focusable="false"/>
</LinearLayout>
<ListView
android:id="#+id/listViewDevice"
android:layout_width="match_parent"
android:layout_height="300dp">
</ListView>

Not able to use custom layout in ArrayAdapter

I'm new to android, I want to use my custom layout file instead of simple_list_item_1 but its not allowing to do so. I want to add an backgroud image(which is defined in layout->custom_view.xml) to each items in the list OR help me to set the backgroud for the whole page, In the below code where should I use the setContentView Method. Also let me know if any changes are to be made in the below .xml file.
public class Planet extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
//setContentView(R.layout.custom_view); Tried to set the view from here but the application is crashing...
String[] planet_names = new String[] {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Sun"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planet_names);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
Intent myIntent = new Intent(Planet.this, Galaxy.class);
startActivityForResult(myIntent, 0);
}
}
});
}
custom_view.xml file is as follows,
<?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"
android:background="#FFFFFF">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Simply use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.yourcustomview_withonetextview,R.id.yourcustomlayouttextviewid, planet_names);
You don't set the contentview in a ListActivity. The ListActivity already does that for you. Use an Activity, if you want to customize your layout.
You need to call super.onCreate() before setContentView(). And if you are going to use a custom layout in a ListActivity, you need to give the ListView the id #android:id/list.

Change image in ListView

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.

Click on item in android ListActivity not working

I have ListActivity with custom ArrayAdapter and following layout for list items:
<?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="horizontal"
android:paddingTop="5dip"
android:paddingBottom="5dip" >
<ImageView
android:id="#+id/image"
android:layout_width="48dip"
android:layout_height="48dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="24.5sp" />
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="12sp"/>
</LinearLayout>
My problem is that, I am unable to click any of the list items. What I've tried so far is
setting focusable, clickable to false (which I found might help) to my ImageView
but, unfortunately, problem remains. Please help, this has been bugging me all day. Thank you.
EDIT: OK, so William's answer helped my out with this one, only problem now is that click on item doesn't highlight it, when I change background colour to black (by setting new theme in manifest for this list activity).
This is my custom style for activity theme <style name="ContentsListTheme">
<item name="android:background">#color/black</item>
</style> from res/values/styles/ folder.
To make it clear, once again, default ListActivity background colour is white. I want it black. When I apply the style from above, list activity item highlighting when clicked is disabled. How can I enable it again, to the default highlighting colour?
In your oncreate try this
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
});
OR you can write this function which will handle the clicking on the list items
protected void onListItemClick(ListView l, View v, int position, long id) {}
I have this working code, and I think in your Java code is the problem.
public class NewsListActivity extends Activity implements OnItemClickListener
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new NewsDataArrayAdapter(NewsListActivity.this, headlines));
listView.setOnItemClickListener(this);
...
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if (v == null) {
return;
}
try this
ListView lv=(ListView)findViewById(yourlistview);
Lv.setonItemClickListener(this);
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
paret.getItemAtPositon(position);
//this returns you an object cast it into the same object you passed to your adapter cast to string if values passed were string
}

Android Activity with Listview keeps throwing NullPointerException

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!

Categories

Resources