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.
Related
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();
In my main activity page, i have a recycler view that has buttons for each item. As i click an item button, i want it to update the text on the Shopping cart button to show the count of items in the cart.The shopping cart button is outside of the recycler view.
I already setup a global variable that records the count of items in the cart, but i can't figure out how to display that count on the button. Mostly, i can't figure out how that button text can update when i click another item in the recycler view.
Thanks in advance!
In my Main Activity (where the recycler view is located and the button i want the text to change on is located), I've created a method init2.
public void init2() {
shopbutton = (Button) findViewById(R.id.button2);
String countshopS = "hello";
shopbutton.setText(countshopS);
}
then i try to call the method init2 from the Onclick Listener of the Adapter where the button rows are.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private MainActivity main = new MainActivity();
holder.priceset.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
main.init2();
I get the response "Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference" on the row where the Id of the button is identified in init2();
Use callback in your Adapter and increase for every click on row's button .
Then implement callback in your activity and do all things you need for example show finial price or count or...
Do not use statics and handle every rows count with data model (transiant maybe useful) and callbacks and interfaces.
I have a custom listView with 3 spinners. I want to execute a code in onItemSelcted but I cannot do this in adapter because some part of the code that I need to execute won't work in a adapter.
I had this code pinned to a button before but sometimes there are over 20-30 elements in the list and I need do this "automaticly" right after user picks a value from spinner to save some time.
For a button I had this code:
((ListView) parent).performItemClick(v, position, 0);
in setOnClickListener that I created inside adapter. After using this code in adapter I could exetue any code outside adapter by writing something like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
if(viewId == R.id.button){
myCode(); }
I found no way to do something similar because there was no "link" between inside adapter onItemSelcted and outside adapter onItemSelcted. Of course writing same line of code inside adapter's onItemSelcted won't work because spinner "doesn't click".
Any idea how to solve this?
In a fewer words: I need to execute onItemSelected for a spinner that's inside custom listView element but need to do this outside the adapter in onCreate method.
You can create an CallbackInterface and create an instance of it "outside" and pass it down to the "inside", where you call the callback function with the needed arguments.
public interface Callback {
void onAction (int a, int b, String c);
}
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();
}
});
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>