i have this customized list. each row contains an image and two lines of text one below the other. i want to open a new activity when any list item is clicked. but i am not able to do so, even after implementing the setOnItemClickListener(). please correct me if i am wrong. the below is the code for the list.
PS: This is an normal activity and not list activity.
l1.setAdapter(new EfficientAdapter(this,eventTitleArray,eventDateArray,eventImageLinkArray));
//l1 = getListView();
l1.setClickable(true);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
Bundle b = new Bundle();
b.putString("event", eventTitleArray[position]);
intent.putExtras(bundle);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Opening detailed view for:"+eventTitleArray[position], Toast.LENGTH_SHORT).show();
}
});
Please have a look whether the row layout has any items which are focusable. If an ListView Item contains focusable children, the Listview Handler will not be fired.
I think there is a bug in the SDK that prevents the onItemClickListeners from firing when there are focusable views in the View of your items.
So you should try to do a setFocusable(false) on all the Views of your items.
The problem is described here
Related
I am kind of new to android studio, I have an array of strings for my list and when i click on an item from that list it takes me to the correct page. However, there are 100 items on the list and it's not logical to create 100 java files for all of them to set the text on that page.
My question is: is there a way to set the text to the correct item of list just after or before clicking that item?
Assuming you have in your FirstActivity a ListView please use the following code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String item = (String) adapterView.getItemAtPosition(position);
Intent intent = new Intent(YourActivity.this, SecondActivity.class);
intent.putExtra("item", item);
startActivity(intent);
}
});
To get the item in your SecondActivity please use this code:
String item = (String) getIntent().getExtras().get("item");
yourTextView.setText(item);
Hope it helps.
I'm using notifyDataSetChanged with my custom GridViewAdapter which updates the listView however when I click on something with my updated listView, the old links are still there. I have the following ClickListener in my onCreate method.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getBaseContext(), SpiceList.class);
intent.putExtra("INGREDIENTS_CALL", true);
intent.putExtra("INGREDIENTS_SELECTED", position + 1);
if (recipeCall) {
intent.putExtra("RECIPE_CALL", true);
}
startActivity(intent);
}
});
I've tried duplicating this inside the buttons that update the listView but this does not work. Any ideas for how I can best update my listener to reflect my changed listView?
Thanks! :)
Not sure if i understand your question but let me see if i can try. You are changing the listviews views when you click on one?
Are you deleting any element in the array or List you are using for your adapter? So you remove an item and then call notifyDataSetChange()? or you add an item to the array and call the notify method?
Turns out the problem was in the way I was retrieving my SQL queries. I was making the position of my listener equal to the ID of the SQL entry I was retrieving. Therefore, even though my list changed, item 1 was still pulling SQL items with an ID of 1.
When I designed my queries in Android, I originally did not expect that I would run into this problem. However, now I've linked my SQL queries to the actual item ID and not the listener position. My new listener is now:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getBaseContext(), SpiceList.class);
intent.putExtra("INGREDIENTS_CALL", true);
intent.putExtra("INGREDIENTS_SELECTED", Integer.parseInt(resultsID.get(position)));
if (recipeCall) {
intent.putExtra("RECIPE_CALL", true);
}
startActivity(intent);
}
});
Thanks for your help!
I have an Android GridView inside an ExpandableListView. All data is appearing fine but I can not find a way to handle GridView Item Click event. I just want to open up another Intent upon clicking on a grid's cell and pass a value to the intent.
Is it possible?
Thanks.
YourGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(this,
MyNewActivity.class);
startActivity(intent);
}
});
Make sure in your GridLayout there is no clickable content like buttons otherwise they'll fired instead of this
grid.setOnChildClickListener();
may solve your problem!
I have created listview in Android to show my records. And records are shown as well. Now I want to click on particular record from that listview to show/view particular record. And that record must show another layout for that profile. How to achieve it?
Create a onItemClick() event for the listview. Then, when you click on an item, a new intent will be created that will start a child activity that will display the particular record.
listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent launchActivity = new Intent(MyActivity.this, OtherActivity.class);
startActivity(launchActivity);
}
});
I have a bunch of items on my ListView, but I don't know how to implement a listener on a specific item. I also tried to use the if statement and still didn't work.
I tried this code:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id)
{
if(list.getItemAtPosition(position).equals(mStrings[0]))
{
Intent i = new Intent(MainActivity.this, Activity2.class);
startActivity(i);
}
}
});
}
But this code is applied to all items.
just put
setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,String[] to display as List));
then you can came to know which item is clicked.....
arg1 is the View that was clicked inside of your ListView. position is the index of that item in your ListView. If you passed an ArrayList of Strings into your ListAdapter, you would call stringArray.get(position) to get the item that was clicked.
I was facing a similar situation after adding this android:descendantFocusability="blocksDescendants"
to my list view xml i was able to get my on click listener work properly