Remove one row from gridlayout - android

I'm using a GridLayout with 5 coloums and multiple (not a fix value) rows. It is possible to add more rows with values to the grid by a add button (the values are hardcoded in hashmap for now).
In each row there is a delete button in the first cell.
Now I want to implement, that if you hit the delete button, the complete row should be deleted but also other rows should stay.
Any proposals how to do this?

You could remove the specific dataset from your HashMap (or use another map in the adapter for the dymanic content) and then call:
grid.setRowCount(grid.getRowCount() - 1);
grid.notifyDataSetChanged();
That should remove the specific row.

It's difficult to explain without having your implementation, but I'll try. So suppose we have our Activity/Fragment with a RecyclerView. We have our Adapter who holds a List/Map/Anything with your elements in it. What we need is the delete button and it's click listener i guess. When you click the button you call a method in your adapter from the Activity/Fragment, which iterates your List/Map/Anything removing the items. The method then calls notifyDataSetChanged() on the Adapter again, and the grid should update correctly.

Related

Android ExpandableListView save input in each item's edit text on button click

I'm new to Android and can't quite figure out the right approach for the problem I'm trying to solve. I have an ExpandableListView with several items. Each item has an EditText, except the last item has a button. The contents of the EditTexts are to be loaded from the database. When the button is clicked or when the activity is navigated away from, I want to save the contents of each EditText to my database.
I'm not sure what to call from the activity's class, what to call from my adapter, and how exactly to access each item appropriately. Code is welcome but not necessary, I'm just looking for guidance on the general approach. Thanks.
I'd recommend putting a method in your adapter called saveAllValues. It iterates through the list of objects in the adapter and saves them to the database. Call this when your button is clicked and in your activity's onStop() method (which is called when the activity is no longer visible).
You should have your activity fetch the values for the item IDs in an AsyncTask in its onCreate method. Then pass the list of id/value pairs to the adapter in its constructor. It should maintain this list so it can go back through it and save the IDs and values to the DB.
Hope my answers in these links help:
Values of counter changes after scrolling ExpendableListView shows how to maintain the list inside the adapter and how to get list from the activity.
Remove the divider view in the expandablelistview last item shows how to make the last child different from the others.

does ListView re-draws itself every time the adapter changed?

I want to make a list of SMSs in my own application..
my question is about the steps makes by the adapter (automatic)
-when new message arrived I adds it to list object (not the ListView).
-then I passes the list to the adapter of the listView.
the adapter GetView() method run for every Item in the list
-I notify the listview about the change.
- the listview re-draws all its existing rows and then draw the new row.
My question: this behavior (re-draw and redraw, it mean every row will be drown times equal to the total rows) affect the performance?
*if the question is not clear I say: does the ListView Draw all the Raws just to add new row? *
The ListView doesn't redraw every single item in order to add one. It will only be drawn when you scroll to it. And yes you have to notify ListView that the change had happened.
I guess you want to add the item at the top position. So all other item's position and index will change . Adapter will redraw the whole list(only elements which are visible on the screen) .
For performance you can use viewHolder pattern.
see this link
[Making ListView Scrolling Smooth][1]http://developer.android.com/training/improving-layouts/smooth-scrolling.html

How to get correct position of a checkbox in a scrollable listView?

I have a scrollable listview that has 2 textViews, 3 imageButtons and 1 checkBox in each row.
In the Header of the listView, I have a delete button just above the column containing all the checkboxes of the listView.
I have to perform the delete operation of objects in the listView when 2 or more checkBoxes are checked randomly by scrolling the listView and thereafter the delete button at the top is clicked.
But the problem is that i am not getting the correct poition of the checkbox that was selected. Moreover, sometimes i get the correct position but still the object to be deleted passed is wrong. Hence the entire functionality is affected maybe due to the scrolling nature of the list.
Should i take the position in the holder of the adapter class and also bind the state of the checked or unchecked checkbox with my object.
And should I use checkBox.setOnCheckedChangeListener() or deleteButton.setOnClickListener().
If i use the latter one, then how to get all the corresponding objects of the list whose checkboxes were checked before pressing the delete button?
And where should all the related code be placed..in the listAdapter class or in the activity?
Please help me find a solution to this problem..
first of all in your getView() method you should set a specific Tag to each checkBox. For example: check1.setTag(position) then you should implement both OnCheckBoxChangeListener for your checkBoxes and OnClickListener for your delete button. As you know setting of onCheckBoxChangeListner have to be in getView() method. Then you add positions of list that their checkBox is checked to the a ArrayList with the help of getTag() method of chechBoxes in onCheckedChanged() method.
Try getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {}

Retrieve view inside a row of a ListView Android

I have a listview with 4 identical rows. Inside those rows, I have a RelativeLayout which contains a TextView (id : R.id.notif). In my Activity, I use my own ArrayAdapter.
I would like to be able to modify the text of the third row. I tried this but it isn't working.
((TextView)listview.getAdapter().getView(2, null, listview).findViewById(R.id.notif)).setText("50");
Thank you.
Do not use adapter.getView() for that! This method is used internally for the adapter to create the view that gets displayed in the list! The correct way to do this is to modify the underlying data and to refresh the list with adapter.notifyDataSetChanged(). Do not try to access views in the list directly, you don't know if they are visible at the moment or scrolled outside the view.

Android first row not selected using setSelection(0) method

after multiple try's, i'm using "mListView.setSelection(0);" to select the first row in my ListView when the list appears. But dunno why its not working. So how can I let the first row always clicked? Because in my application i have a listview on left side and a webview on right side. So i need that when my app launchs, the first row always selected so the webview loads the details of the first element per default. The data of the listView are loaded using a cursor Adapter that communicates with an SQLite Database.
This is a part of my code :
code
Try setting up an OnGlobalLayoutListener to your ViewTreeObserver, which sets the selection as soon as the layout is set. (Remember to remove the listener as soon as it's called the first time to avoid repeated calls.)
Check the android sources whether setSelection(int) handles unpopulated lists right.
You can try using smoothScrollToPosition(int position) it should work for you.
mListView.smoothScrollToPosition(0);

Categories

Resources