I have a RecyclerView that shows different lists depending on which option I pick in my Spinner. I can add items to a list dynamically, but when I add items to the top option on the Spinner, two items appear everytime I add, when it is only supposed to add just one and when I choose a different option in the Spinner and then back to the top option, all items that are supposed to be in it are gone. However, when I repeat the same actions in another option, one item is added at a time, and they are still there if i switch to another and back, so it is only in the first option where I experience these problems.
Here are pictures that shows what happens. First picture shows the entire Spinner, second is the top option selected with items added, third is the second Spinner option selected and fourth is back to the top option and now the items are gone.
This method is used to add a Item to one of the lists. Each option in the Spinner is used to get an element in listOfLists where each element is an ArrayList<Item>(). mItemList is the list which the RecyclerView prints out.
public void addItem(){
mItemList.add(0, new Item(idText.getText().toString(), addAmountField.getText().toString()));
listOfLists.get(dropdown.getSelectedItemPosition()).add(0, new Item(idText.getText().toString(), addAmountField.getText().toString()));
System.out.println("ListSize: " + listOfLists.get(dropdown.getSelectedItemPosition()).size());
addAmountField.setVisibility(View.GONE);
btnAdd.setVisibility(View.GONE);
mAdapter.notifyDataSetChanged();
saveData2();
}
This is the code for switching the lists when I choose another option from the Spinner. What I do here is that I clear the mItemList and then I fill it with one of the ArrayList<Item> inside listOfLists.
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
mItemList.clear();
for (int i = 0; i < listOfLists.get(position).size(); i++) {
mItemList.add(listOfLists.get(position).get(i));
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
How can I resolve this so that the top option of the Spinner will work just like the other options where it adds one item at a time and still has the items after I switch to another option?
I managed to solve it so for anyone who faces similar problems this was the problem:
Unlike the other lists in listOfLists where I set mItemList to the chosen list like this
mItemList.clear();
for (int i = 0; i < listOfLists.get(position).size(); i++) {
mItemList.add(listOfLists.get(position).get(i));
}
mItemList is always set to the first list when I run the app like this
mItemList = listOfLists.get(dropDown.getSelectedItemPosition());
So I just changed it so it sets mItemList the same way it is done with the other lists and now it is working.
Related
In the below code, where the item’s position and title is getting saved in list_items works fine if list_items and items are ArrayList. For ex;
ArrayList.add(ArrayList.get(position));
But I am trying to use listview:
ArrayList.add(Listview.get(position));
Below is my code
final Listadapter Adapter =new Listadapter(this,packageList1,packageManager);
items.setAdapter(Adapter);
items.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);
items.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
{
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
count = count +1;
mode.setTitle(count + "items selected"); //problem resides in the below line
list_items.add(items.get(position));
}
In the above code, on selecting an item from listview I am trying to save it in an Arraylist i.e list_items which could be used in onActionItemClicked method for further action on that respective item.
Any ideas on how to solve or find a workaround to use a listview in the above situation?
what I want to do is press a button to add previously declared TableRow objects (refers to TableRow objects already created in XML file) that I have hidden using table.removeView(row1) etc on program start.
Then I want to be able to click a button to add each TableRow back to the view one at a time until all the rows are visible again. I have tried a few different ways without any luck and the last method I tried was creating an array list in my onCreate method like so:
final ArrayList<TableRow> rowlist = new ArrayList<TableRow>();
rowlist.add(row4);
rowlist.add(row5);
rowlist.add(row6);
rowlist.add(row7);
rowlist.add(row8);
rowlist.add(row9);
rowlist.add(row10);
Then trying to iterate through like this:
public void onClick(View v) {
Iterator<TableRow> rowiterator = rowlist.iterator();
while (rowiterator.hasNext()) {
table.addView(rowiterator.next());
}
}
What I get now is when I press my button it just adds all the rows back in at once, when I want it to iterate through the list adding rows one at a time.
Can anyone help me resolve this problem, or tell me if I'm being a complete idiot and suggest an entirely new and better method of achieving what I want to achieve?
Note: I'm pretty new to Java programming and on this problem I am absolutely stumped!
You're iterating over the entire array when you click the button and adding them all back in. Something like this is probably more like what you want:
public void onClick(View v) {
if(rowlist.size() > 0)
{
table.addView(rowlist.get(0));
rowlist.remove(0);
}
}
Well it appears I have been stumped by one of the simplest ListView implementations out there. For a few days I have found it impossible to properly reallocate a JavaFX ListView. I am working on making an EntityView completely dynamic, being able to remove elements whenever needed through a ContextMenu. So I have a ListView that is populated by an ArrayList, which we will refer to as "renderable". When you select "Remove" in the context menu, it removes the Entity from the renderable list, which also happens to be the "value" of the List Cell on whom I right clicked. Afterwards, I wish to refresh the ListView and remove now the nonexistent cell. So by creating a new ObservableList with the new renderable list (which removes the correct entity and that works fine), I set the items in the ListView, which does jack shit. I can set the list to null in this case, which removes all the elements. But I cannot reset the list with the new array and it remove the now missing entity. Somebody point me in the right direction please!
When I use the method I stated above, it removes it from the list, but not visually. There becomes an unusable cell at the bottom of the list, which has its name.
public void createContextMenu(final Entity curr, MouseEvent me){
MenuItem[] items = {new MenuItem("EDIT TYPE"), new MenuItem("REMOVE")};
ContextMenu menu = new ContextMenu(items);
menu.show(list, me.getScreenX(), me.getScreenY());
items[1].setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent arg0) {
CanvasTab tab = (CanvasTab) core.canvasTabbedPane.getSelectionModel().getSelectedItem();
Kanvas k = tab.canvas;
k.renderable.remove(curr);
System.out.println(k.renderable);
k.redraw();
EntityView.this.list.getItems().remove(curr);
ObservableList<Entity> temp = FXCollections.observableList(k.renderable);
EntityView.this.list.setItems(temp);
}
});
}
This is the context menu:
How can I retrieve the View at the position X in a ListView? I dont want to inflate a new one, just retrieve the cell visible in the screen for change some parameters programmatically
Since views in ListView are re-used/re-cycled. There is no direct way of getting a view reference from the ListView.
If you want to access a view you need to extend ArrayAdapter and then override getView. There you should call the super.getView and write your own custom code.
If we you really need to control more than try extending BaseAdapter or CursorAdapter.
Found a dirty solution:
You should be able of identify each row generated. For example adding a TextView with visibility=gone and writing a unique value there when generating (or recycling the row)
In the listactivity call to getListView.setSelection(position) to the desired cell
Survey the listview list for the row (until displayed)
lv=getListView();
for (int i=0;i <lv.getChildCount();i++){
if (((TextView)lv.findViewById(R.id.my_hidden_textview)).getText.equals(mykey)){
// view found
} else {
// schedule another survey "soon"
}
}
For the schedule you can use something like:
final int RETRY_DELAY=100;
new Handler(){
public void handleMessage(Message msg){
if (msg.what<0) return; //something went wrong and retries expired
lv=getListView();
for (int i=0;i <lv.getChildCount();i++){
if (((TextView)lv.findViewById(R.id.my_hidden_textview)).getText.equals(mykey)){
//result = lv.findViewById(R.id.my_hidden_textview);
} else {
this.sendEmptyMessageDelayed(msg.what-1,RETRY_DELAY);
}
}
}
}.sendEmptyMessageDelayed(10,RETRY_DELAY);
As I said is a very ugly solution but it works
I didn't clearly understand your problem. But to what I've understood I would suggest you use a frame layout within a linear layout. You can use another frame layout to do your manipulations.
Checking on one item in custom list view and another get selected in Android. I'm using custom adapter list view with check box.
I'm using check boxes in list view.
and I'm using following code for check box count. i.e how many check box are selected.
final int listItemCount = screenList.getChildCount();
for (int i = 0; i < listItemCount; i++) {
CheckBox cbox = (CheckBox) ((View) screenList.getChildAt(i))
.findViewById(R.id.checkBox);
if (cbox.isChecked()) {
count++;
}
}
But while I'm getting count=listItemCount there are some numbers are not shown checked in the list view.
In order to access the one you expect, do something like this (assuming you are using ListActivity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// this will give you the widget that was selected.
Widget widget = widgetAdapter.getItem(position);
}
If this doesn't solve your problem, please show some code so we can take a look.
I'm having a similar problem with the list adapter. When the getview() is called for each customviewitem(has a checkbox) in the list the getview receives the position and the view object.
What i'm seeing while debugging is if the list shows 5 item at a time then say for
0th position the view obj id is 830173183032
then for the
6th position the view obj id is same ie 830173183032.
So when i check the checkbox of 0th item and then scroll down then I see that the 6th items checkbox also checked.
code was from http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/