listview item click event not working in android - java

I have a ListViewin my ListView show ImageButton, ImageView, Button, TextView
I set ListView.OnItemClickListner. When I run my project, it shows my ListView.
But when i click on listview it's not working
what is wrong? and I want button, ImageButon,ImageView click events should call Activity or dialog ? Help me?
This is the code:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Toast.makeText(getActivity(), "sdadad",1).show();
}
});

Add one tag in parent layout of your custom layout
android:descendantFocusability="blocksDescendants"

Related

Get click event from gridview's child item in main activity from adapter

I have a GridView in my activity. I am having 2 elements in the GridView. One is an ImageView and the other is a TextView.
I want to perform an action when clicking the ImageView only, but I want this to happen in the activity and not in the GridView adapter.
I handle the ImageView click in the getView() of my adapter, but I do not want it that way. I want to handle it in the activity when calling:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, items));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//THIS WORKS FINE
String string = ((TextView) v.findViewById(R.id.text)).getText().toString();
Log.d("string",string);
//THE PROBLEM OCCURS HERE
ImageView capture = (ImageView) v.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
});
//THE PROBLEM OCCURS HERE
The action is supposed to happen the first time I click the ImageView, but it only happens on the second click and further.
This is where I am stuck with the issue. I want this action to occur on the first click and not on the second click.
You can see the code block-
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String string = ((TextView) v.findViewById(R.id.text)).getText().toString();
Log.d("string",string);
//THE PROBLEM OCCURS HERE
ImageView capture = (ImageView) v.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
Your imageview capture is adding any action after gridview is being clicked. So, after clicking first time it is executing and then setting click listener to imageview and then next click its executing its onClick block. So better, handle imageview click event inside adapter.
You can call method inside your activity from adapter class but you should implement setOnClickListener inside your adapter class.
with the help of interface, you can do that do one thing create an interface and implement it on your activity and get imageview click on the adapter and here initiate the interface and you will get the click inside the activity
I think the problem is that on the first click you just set OnClickListener on your imageView, and so its onClick() method is not getting called. On the second click though, as the listener is already set, onClick() is invoked.
Instead of setting new Listener to your imageView each time an item in the grid clicked (which does not make any sense by the way), you should do either of these:
If imageView in each item has to be handled the same way, set OnClickListener to the imageView in the adapter, when creating a view.
If not, pass the interface for handling imageView clicks to the constructor of the adapter, and then, in the activity, implement this interface when creating an adapter.
Create a method in you activity.
Now, in adapter, onClick call that method using
((Activityname)context).methodname(parameters);

OnClick listerner seems to stop OnLongClick?

I have a listview in which previously every item was clickable, and it worked fine. But as soon as I added a feature to have all items long clicked, the onClick feature stopped working. I stripped down all my code for debugging purposes which I will submit here:
listview = (ListView) findViewById(R.id.workout_listview);
listview.setAdapter(new WorkoutListViewAdapter(this, new String[] {...}));
listview.setClickable(true);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.print("CLICK");
}
});
listview.setLongClickable(true);
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("LONG CLICK");
return true;
}
});
This code causes nothing to happen when a list item is clicked and displays "LONG CLICK" when one is long clicked.
I found this question, which seems to have the same problem but the code is structured very differently, and my code is already following the answer's guidelines of implementing the listener on the ListView rather than the row.

How to get the value of a TextView that is part of a ListView row in all checked items

As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});

Android Dragging ListView and ContextMenu

I'm still working with dragging listview (example from google https://www.youtube.com/watch?v=_BZIvjMgH-Q) - long click by listview item - goes dragging and I want to implement context menu on short click, but my changes do not seem towork. After short click on listview item goes dragging effect..
here is my code for activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.changeTheme(this,getApplicationContext());
Utils.setLang(this,getApplicationContext());
setContentView(R.layout.activity_list);
listView = (DynamicListView) findViewById(R.id.listview);
adapter = new StableArrayAdapter(this, R.layout.text_view, productsArray);
listView.setList(productsArray);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
registerForContextMenu(listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
view.showContextMenu();
}
});
}
With simple listview, short click working without any problems...
Could anybody explain what I'm doing wrong?
Thx in advance!
The best solution in this case is use popup menu instead of context menu.

Clear focus after click in item autocomplete

I have a problem. If I set clearFocus() in setOnItemClickListener it's not work.
Focus set nearby input.
How I can clear focus in autocomplete, after I clicked item?
textView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
arrAuto = (String) ((TextView) view
.findViewById(R.id.item_auto)).getText();
findViewById(R.id.editText1).clearFocus(); //it's not work
}
});
You need to set android:windowSoftInputMode="stateHidden" to your activity in AndroidManifest in order to keep the keyboard hidden when the activity is started.

Categories