I'm having a ListView
with my own custom adapter derived from a BaseAdapter
. Each item in the ListView
has sub items such as ImageView
and TextView
.
How can I know which one of these sub items the user clicked? Is it possible to attach a listener in the getView
function for example, or could that be a problem?
/ Henrik
Edit: Currently I have a onItemClick
in the Activity which contains the ListView
. Is there any good way to know which sub item in a specific item in the ListView which has been pressed by checking the params in the onItemClick
.
@Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
.
.
}
In an OnItemClickListener , if the variable is blank or not equal to the selected Item , set the variable to the value of the selected Item . Else , run the code you wanted to! And of course you'll want to clear selectedItem every time you bring up the ListView again!
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. ListView is a view group that displays a list of scrollable items.
ListView uses Adapter classes which add the content from data source (such as string array, array, database etc) to ListView.
To determine all the items that are checked in a ListView control, you can use the CheckedItems property. To take action when an item has been checked, you can create an event handler for the ItemCheck property of the ListView control. Gets or sets the starting position of the form at run time.
You can use DoubleTapped event of listview on XAML code. And then, in C# code you can get position by: private void display_DoubleTapped_1 (object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e) { int items = display.SelectedIndex; // use this index to do something }
To get the row id for the row which was clicked, call the getItemIdAtPosition (position) method of your ListView (which you have to get some other way, because it's not given as parameter to your callback, but that shouldn't be a big problem in most cases). The advantage of this solution is that it can be used with any ListAdapter.
true if the item is checked; otherwise, false. The default is false. The following code example creates a ListView control with three ListViewItem objects specified and three ListViewItem.ListViewSubItem objects specified for each item.
I used an idea from Miga's Hobby Programming.
The key is calling performItemClick() from the new onClick listener. This passes the click on through to the onItemClick() that's already being used for the listview. It's so quick and easy, I feel like I'm cheating.
Here's getView(), from the list adapter:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.one_line, parent, false);
}
// This chunk added to get textview click to register in Fragment's onItemClick()
// Had to make position and parent 'final' in method definition
convertView.findViewById(R.id.someName).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
// do stuff...
}
And the onItemClick():
@Override
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
long viewId = view.getId();
if (viewId == R.id.someName) {
Toast.makeText(getActivity(), "someName item clicked", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getActivity(), "ListView clicked: " + id, Toast.LENGTH_SHORT).show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With