Detect click on checkbox in custom ListView layout? - java

I know there's tons of other threads around about this same topic, but none of them seem to work with my scenario and I couldn't get my listview to work with their code. Basically, I'm using a SimpleCursorAdapter to populate a listview with items from the database. Each listview row uses a custom layout which consists of a checkbox and a line of simple text. How do I detect a click on the checkbox? I know I need to use OnItemClickListener, but I don't know how to incorporate that into my code. Here's my code:
remindersCursorAdapter = new SimpleCursorAdapter(this,
R.xml.view_reminders_item_layout,
remindersCursor, new String [] { RemindersDAO.NAME },
new int[] { R.id.view_reminders_item_text } );
viewRemindersListView.setAdapter(remindersCursorAdapter);
R.xml.view_reminders_item_layout is the custom listview layout file. How do I capture the checkbox from this file and set a click listener to it? Thanks for all your help!

If you want to check the checkbox when a item is clicked, you can do it by setting checked status of Checkbox in onItemClick method.
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// now get the checkbox view. Then set the checked status.
CheckBox checkbox = (CheckBox) view.findViewById(R.id.check_box);
checkBox.setChecked(!checkbox.isChecked());
}
If you want to detect the click on only checkbox then set the focusable true in xml.
// in your custom list view item. It takes the current view focus.
<Checkbox>
android:focusable="true"
</Checkbox>

Related

Reload recyclerview

I am new to android and I am having a problem reloading (rebuilding) as recyclerview after the activity is active. The activity with the recyclerview is a second activity started as an intent on the main activity. I can load it up, and show a list of items. When one of the items is clicked a third activity is launched. The problem is sometimes there are too many items on the list. I want to add a way of selecting an item more easily. I added a spinner on the same activity as the recyclerview. When an item is selected on the spinner, I want to reload a shorter version of the list, based on the spinner selection. However, I can't find a way to achieve that. Any help or suggestions pointing me in the right direction will be greatly appreciated.
as #bink said without code it's hard to help, but the idea in general is that on your spinner.setOnItemSelectedListener() you populate your recyclerView, so everytime you change the selection you get a new list depending on the choosed value.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayList<> arrayList = new ArrayList<>();
// add your items based on item spinner.itemSelected
// for(---)
adapter = new MyAdapter(context, arrayList );
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
});
You can also add a filter to your recycler view (https://medium.com/android-news/filterable-recyclerview-in-android-the-how-to-a9ade9cd26)
If you dont want to recreate all the recycler view and the list itselft, use adapter.notifyDataSetChanged();

Data in the text view toggling while scrolling list view

I have a list view in which there are a text and a button in every row.When I click on the button the text in the text view changes.It is fine till now.But when I scroll the list view the text changes to its default text.Please help how can I show the changed text on button click in the list view when I scroll.
I want like the above pic.But I am getting like the below pic when I am scrolling the list view
Try with following steps:
You should use a array/ArrayList of your default texts like plus, plus... and then pass this array/ArrayList to your CustomListAdapter.
In you CustomListAdapter inside getView(): set the text on textview from the array/ArrayList.
On button click: You should update the string in your array/ArrayList, corresponding to clicked index and call notifyDataSetChanged().
Edited ans -
private void updateText(int position, String text) {
arrayList.set(position, text);
notifyDataSetChanged();
}
Now call above method on both button clicks like
on minus button click- updateText(position,"minus");
on plus button click- updateText(position,"plus");

How to find the class of a checkbox that was clicked

I have a 'ChecklistItem' class that has the following properties:
private CheckBox checkBox;
private ImageButton noteButton;
private TextView vitalField;
I have an onClick Listener for my checkbox. Now the problem is, when I click on that checkbox and the OnClick() method gets called, how can I figure out what ChecklistItem that checkbox is a part of?
Whenever I click on a checkbox, I want to add the ChecklistItem that the checkbox is a part of to an array, but the OnClick() only knows about the checkbox that called it.
How can I get around this?
Ok so this answer is according to the "long discussion" we had
let's assume you want to make a - re usable - view of your list and you wrote a separate xml layout file called list_item as the following:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view"/>
so now let's assume you are in the activity or fragment or wherever you want to host your view , NOW I have to point out this is just an example , usually a list view is what you would need in this case but again I have very little details about your app so I'm going to keep it simple
Assuming you have a vertical linear layout and you want to add these "rows" to it, each row represents one of your custom view
LinearLayout layout = findViewById(R.id.layout);
LayoutInflater inflater = LayoutInflater.from(this); // This inflater is responsible of creating instances of your view
View myView = inflater.inflate(R.layout.list_item, layout, false); // This view objects is the view you made in your xml file
CheckBox checkBox = (CheckBox) myView.findViewById(R.id.checkbox);
TextView textView = (TextView) myView.findViewById(R.id.text_view);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if checkbox is checked enable textview for example
// here you have a reference to all the views you just created
// Weather you want to save them in a class together that's up to you and your app's logic
}
});
layout.addView((myView));
if the list is might exceed the screen height you may want to wrap your linear layout in a scroll view.
BTW: ListView is just a neat way to do this automatically by defining how you want each row to appear, and of course it manages your views for you and recycle them when they get of screen, but I just wanted to point out the concept.
Hope this helps you

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();
}
});

Update ListView item on click/select

I'm new in android and I've got problem with ListView. My ListView loads data from REST service, then using this data ListView is filled. I want to add something lke OnItemClickListener/OnSelectedItemListener on each item and when user cliks on item want to add button. Button should disappear when user clicks on another item. Any ideas?
[EDIT]
What have I tried?
I've create LinearView which holds ListView and Button
ListView is filled in Activity using REST service data (TextView is used for view)
I think that my OnClickListener class may look like this but I don't know how I can add Button (the best will be create one Button object amd show it only on selected item - I think it will bebetter performance)
public class RoomClickListener implements OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Button b = new Button(arg0.getContext());
b.setText("Join");
}
}
Did you tried to play with the android:visibility of your Button?
Set it to visible/invisible can be a way to do this i guess.
Add button and call invalidate() on the view which changed.
This should work.

Categories