onItemClick not being invoked on ListActivity - android

I have a class that extends from ListActivity and implements OnItemClickListener
It's a very simple test class, the idea is that I select an item on the list, and it shows the selected item on a Toast.
I can see the list normally on the emulator, and I can also see the effects of clicking in the item, but then nothing happens.
I don't think the event is being fired, because I see nothing on LogCat, here's the code:
public class CarsListActivity extends ListActivity implements
OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listCars()));
}
private List<String> listCars() {
return Arrays.asList("Ferrari", "Lamborghini", "Porsche");
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
String message = "Selected car: " + textView.getText();
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
The Activitiy is defined like this on the AndroidManifest.xml file:
<activity android:name=".CarsListActivity" />
Is there anything I'm missing?
I researched this error and I found many solutions saying that this concerns clickability and focusability attributes on the layout. But I'm using Android's own android.R.layout.simple_list_item_1 so I don't really know how I could fix it.
Am I missing some configuration?

You need to register the OnItemClickListener (the activity) like this :
getListView().setOnItemClickListener(this)
Simply implementing the OnItemClickListener interface is not sufficient

add this in onCreate() below setListAdapter()
getListView().setOnItemClickListener(this);

use this on oncreate()
getListView().setOnItemClickListener(this);

Related

Android programming: I cant get data to reload into arrayadapter

I am still stuck with this issue, can anyone help. It seems that my problem is that I cant update the data list. I have tried every solution that I've searched for on google etc.. but half the time i'm not even sure that I'm doing the correct thing.
I've used the onResume() to call notifyDataSetChanged, it didn't work. I've tried putting a refresh method into the adapter which i then called in OnResume(). Again it didn't work. Some people suggest clearing the adpater (adapter.clear();) in onResume and then using the addAll() function to relist the data but nothing works.
There has to be a simple solution to this. I have literally been stuck on this for 2 days now. very frustrated.
Here's my Fragment code again...
enter code here
public class SavedAppFragment extends ListFragment {
private static final String TAG = "AppClicked"; //DEBUGGER
private ArrayList<App> mSavedApps;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Populate the ArrayList
mSavedApps = SavedAppData.get(getActivity()).getApps();
AppAdapter adapter = new AppAdapter(mSavedApps);
setListAdapter(adapter);
}
//LIST ITEM CLICKED: /*Control what happens when list item is clicked: I.E. Load up a quiz while putting an EXTRA key containg the package name of the App to be launhced should the user get the question correct */ #Override public void onListItemClick(ListView l, View v, int position,long id) { //Return the crime for the list item that was clicked App c = ((AppAdapter) getListAdapter()).getItem(position); Log.d(TAG, "was clicked");
//Start the Activity that will list the detail of the app
Intent i = new Intent(getActivity(), Quiz_Activity.class);
String name = c.getPackage();
i.putExtra("packagename", name);
startActivity(i);
}
private class AppAdapter extends ArrayAdapter {
private ArrayList<App> mSavedApps;
public AppAdapter(ArrayList<App> apps) {
super(getActivity(), 0, apps);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//If we weren't given a view, inflate one
if (null == convertView) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_app, null);
//((AppAdapter) getListAdapter()).notifyDataSetChanged();
}
((AppAdapter) getListAdapter()).notifyDataSetChanged();
//Configure the view for this crime
App c = getItem(position);
TextView nameTextView = (TextView) convertView.findViewById(R.id.app_name);
nameTextView.setText(c.getName());
// nameTextView.setText(applicationInfo.loadLabel(packageManager));
TextView packageTextView = (TextView) convertView.findViewById(R.id.app_package);
packageTextView.setText(c.getPackage());
CheckBox appCheckBox = (CheckBox) convertView.findViewById(R.id.app_checked);
appCheckBox.setChecked(c.isChecked());
//Return the view object to the ListView
return convertView;
}
}
}
THANKS!!!
When you return to Activity B, the previous Activity B hasn't been destroyed. Thus, it skips the onCreate. Move all of the stuff you want to make sure happens every time into the onResume. I think you want to make your Adapter a class variable (I'll call it mAdapter) in onCreate, and add code that will get data from the list directly. If you need to do something, put a "refresh" function in the adapter. I'm assuming you have a custom Adapter, because I've never heard of AppAdapter. If you don't, then extend AppAdapter and add that functionality. Thus, your onCreate should look like this:
mAdapter = new AppAdapter(mSavedApps);
setListAdapter(mAdapter);
Your onRefresh could update the data contained in the adapter by some new update function, like so:
mAdapter.update(SavedAppData.get(getActivity()).getApps());

onListItemClick doesn't work

So, my onListItemClick is not working for some reason. I use the same code structure in another activity and that one is working perfectly. And when I try to implement the same method here, it just doesn't work at all. The Toast doesn't work. And the intent to detailactivity also doesn't work. I'm sure my naming and labeling is fine. The list items doesn't even feel like they are clickable. Can someone help me out here, please?
public class MainActivity extends ListActivity {
String objectId;
protected List<ParseObject> mInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
//**** There is a bunch of code here that takes import the list view from an adapter. I believe they are working fine. ***//
}
//**** this part below is not working. This is the section where if you click on the list item, it brings you to the detailactivity.
#Override
protected void onListItemClick(ListView l,View v, int position, long id){
super.onListItemClick(l, v, position, id);
ParseObject infoObject = mInfo.get(position);
String objectId = infoObject.getObjectId();
Toast.makeText(getApplicationContext(),objectId, Toast.LENGTH_SHORT).show();
Intent Details = new Intent(MainActivity.this, DetailsActivity.class);
Details.putExtra("objectId", objectId);
startActivity(Details);
}
İf you use a custom adapter to fill listview and involve clickable components such as Button, İmageView. onListItemClick will don't work because it won't be able to decide which component will be listened. solution is "android:focusable:"false"" keyword. thanks to this, Listener will only focus items which are populated custom adapter by you.
Like this:
<Button
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="colorCode"
/>
I have used this button to seperate each list view items. But it caused your problem. I think if you will list items, You shouldnt use clicable components in
template xml.
Happy Coding..
Vd.
Please implement the onListItemClickListener
public class MainActivity extends ListActivity implements OnItemClickListener{
//code code code
}
Then when you set the onItemCLick use
setOnItemClickListener(this);
I found an answer to solve this by adding android:focusable="false" inside of the the views in the listview/customlayout that will be imported to the activity. I got the answer from this link:
onListItemClick is not working for listview?

OnItemClick doesn't work with custom adapter

I have a ListView with Custom Adapter. I have seen this thread where people asked if the items in the custom view had a clickable item. And Yes, I have a clickable ImageView in the listrow. So clicking anywhere else(other than that ImageView) should perform some other action. I gave an onItemClickListener to the ListView. However, it doesn't work on first click and works on two-three clicks.
Update:
In my adapter's getView method, I set onClick of ImageView like this:
holder.chatImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff here
}
});
It works fine, and in my activity, I gave onItemClickListener to listView likw this:
onlineListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
//Stuff here
}
});
PS: I didn't explicitly give any focus to anything.
Some time ago I've faced the same problem but with a CheckBox, I've resolve it by creating a onClickListener as member of the Custom Adapter. For example
public MyAdapter extends BaseAdapter{
// ... ..
// ... ...
private OnClickListener imageClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
/// your data .getTag()
// process onClickListener for image
}
};
}
And then, in your getView:
if (convertView == null){
//Create your views and register onClickListener
holder.chatImageView.setOnClickListener(imageClickListener);
}
Add android:focusable="false" to your image in xml.
Hope it helps, Here you have the entire example:

multiple Lists in a ListActivity and onListItemClick feature (or workaround?)

I know that ListActivity gives me the possibility to use onListItemClick.
On the other hand in a normal Activity i can include multiple lists and make easy switching+animation through a ViewFlipper.
So. Can i make it work alltogether?
[Solved] Yes! ... implements are the kings. Deleted my code to minimize confusion.
Have you tried having your Activity implement OnItemClickListener and then set your lists' onItemClickListener to the activity? I.e.
public class MyActivity extends Activity implements OnItemClickListener {
...
public void onCreate(...) {
...
mList1.setOnItemClickListener(this);
mList2.setOnItemClickListener(this);
...
}
...
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
if (adapter.getId() == R.id.list1) {
// Handle list1 click event
} else if (adapter.getId() == R.id.list2) {
// Handle list2 click event
}
}
}

about ListView code

I am new to android and right now I am learning about ListView.
I was reading tutorials from bogotobogo.com when I saw this code:
ListView lv = getListView();
lv.setTextFilterEnabled(true);
*** lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
Toast.LENGTH_SHORT).show();
}
}); ***
i was not able to understand code from lv.setOnItemClickListener(new OnItemClickListener(); is it an argument?
Can some one help me to understand it?
What that code is doing is implementing a new OnItemClickListener inline. The OnItemClickListener interface is basically a contract that says that an object will provide an implementation of the function onItemClick(....). Later on, when an item in your list gets clicked, the onItemClick function will be called and the AdapterView (thing that's instantiating and managing the list rows, the view - (the render code for a particular row), the position (the position in the list) and an id property which I never use so you can look up what that's for are being passed in.
Inline code like this always looks strange to me. There are a couple other ways to write this that I think make more intuitive sense. Just keep in mind that what you're doing is writing some code to get executed when a row in your list gets clicked.
1 - You can have your Activity implement OnItemClickListener
public class SomeActivity extends Activity implements OnItemClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourview);
ListView lv = (ListView)findViewById(R.id.listView);
lv.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
//Your toast code goes in here
}
}
In this code you're having your main class implement the OnItemClickListener interface so setOnItemClickListener sees the main class (this) as an instance of OnItemClickListener. When a row in your list gets clicked the onItemClick function will get called.
You can also 2 - have your click listener come from an internal class.
public class SomeActivity extends Activity implements OnItemClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourview);
ListView lv = (ListView)findViewById(R.id.listView);
lv.setOnItemClickListener(new YourInternalClass());
}
class YourInternalClass implements View.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
//Your toast code goes in here
}
}
}
And really all three methods are doing the same thing: Providing the setOnItemClickListener with an instance of a View.OnItemClickListener class that will have it's onItemClick function called when a row in the list gets clicked.

Categories

Resources