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?
Related
Can anyone help me to solve my problem. I am new in android. I have a ListView which is show a list of data. And i want to do that user can select item from that ListView and that data will set in another ListView. I don't know how to do it because i'm totally new to android. Please help me.Thank you.
You can use ListView#onItemClickListener to achive this. Implement the onItemClickListener for first ListView to populate ArrayAdapter of the second ListView. Something like this
public void onCreate(Bundle savedInstanceState) {
...
firstListView.setOnItemClickListener(new Android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Add new items based on clicked position to your second ListView
})
...
}
I am new to android development so may be i am missing something elementary here, but after almost 2 days of googling I am still unable to figure out the cause.
I have followed the following article to create my project, except that I am invoking this activity from another activity when a button is clicked.
I have also created my own List adopter public class MyListAdapter extends ArrayAdapter<MyData> and in the getView() method of this adapter I am actually providing a different representation like below.
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_list_view, parent, false);
}
this all works fine I am able to bind the data and its displaying the results on the list without any issues. Now I want to show the details of selected MyData when the user clicks on the list item, but this is just not working no matter what I try.
I have ensured that my activity is implementing the callback interface like below:
public class MyItemListActivity extends FragmentActivity
implements MyItemListFragment.Callbacks
and the callback itself is also very simple:
public interface Callbacks {
public void onItemSelected(MyData item);
}
also on my MyItemListFragment the onAttched is correctly hooked up mCallbacks = (Callbacks) activity;
What I really notice is that the following function is just not called when I click on the list item:
#Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
MyData data = DataContent.ITEMS.get(position);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(data);
}
my fragment that displays the list item looks like this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myitem_list"
android:name="path.MyItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context="path.MyItemListActivity"
tools:layout="#android:layout/list_content" />
and the actual custom adapter view my_list_view looks like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >....</RelativeLayout>
I can see that its missing the android:onClick tag here, but not sure if that is the cause and also who needs to implement this handler as there is no direct context on this layout.
Any help is really appreciated.
Regards
Kiran
I used to dynamically populate a Table Layout with different elements per row, among them a Spinner.
To handle the initialization of spinner values i used what most recommended: a boolean flag variable. The code went something like this
public class spinnerHandling implements OnItemSelectedListener{
public void dynamicallyPopulateTableLayout(int a){
...
//initialize spinner with default values
mySpinner.setSelection(a);
mySpinner.setTag(true);
mySpinner.setOnItemSelectedListener(this);
...
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(!(Boolean)parent.getTag()){
//my code
...
}
parent.setTag(false);
}
}
This worked fine.
Later i decided that i should use a List View instead of a Table Layout. For that i have a custom ArrayAdapter that uses a row Layout, in which of course i have a Spinner, to populate the List View. As those of you who are familiar with List Views, understand its complex dynamic behavior, would know that the somehow static turn around of the boolean flag method wont work.
After three days of tremendous headache trying to find a way to resolve this issue, i just came across the spinner's isDirty() method that in contrast with isPressed(), isSelected(), etc. actually changes its boolean values when spinner item is selected by user.
the code is something like this:
public class spinnerHandling implements OnItemSelectedListener{
public void dynamicallyPopulateListView(int a){
...
//initialize spinner with default values
mySpinner.setSelection(a);
mySpinner.setOnItemSelectedListener(this);
...
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(!parent.isDirty()){
//my code
...
}
}
}
This seems to work fine!
But i would like to make sure that isDirty() handles properly spinner initialization and user selection.
Can you please confirm or contradict this behavior?
Thank you.
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);
Up front: This is my first attempt at an Android app. I'm in that strange place of not knowing what to search for to find the answer to my question.
What I have accomplished is:
Created a custom class myCustomClass with properties of 'title' and 'youTubeUrl'
Created an ArrayList<myCustomClass>
Added multiple elements to ArrayList<myCustomClass>
Created a custom ArrayAdapter and attached it to the the arraylist.
Added an onItemClickListener to the custom ArrayAdapter.
All of that works good. I would like to show the title in the ListView and then when the user clicks the list view item, I'd like to get a reference to the youtubeUrl property.
Here's what I have for the adapter code:
MyListAdapter myListAdapter = new MyListAdapter(this, R.layout.my_list, elements);
myList.setAdapter(myListAdapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
myListAdapter.notifyDataSetChanged();
Thanks for your help.
You can use the position property in onItemClick to go back to your data source and find the relevant item. From there you should be able to retrieve the Url.
As another poster implied, it depends on what you are using in your adapter. Assuming it's MyCustomClass. You can do something like this in your onItemClick method:
MyCustomClass selection = (MyCustomClass) getListView().getItemAtPosition(position);