i'm using a custom expandable listview in my app and i`m stopped in one issue. I'm trying to insert in each group all the results from the query and for that i'm using 2 queries..one for the parent and another for the child.
So...the result from parent query returns a String[] with the values that i put normally on parent group list.
When i try to query the itens for the child..i use the parent result as parameter for get the values. The problem i`m having is that only the last item was list on the child row is showed.
I`m trying to put each result from String[parent number] on a Arraylist that i respective put on his parent Array...but only the last query was showed in all the parent group.
What i think is the queries are overwrite the previous one and adding this one instead of adding all. Searching on some topic i found something like Multidimensional array..is that what i have to use?
this is the part of the code that i stopped
//String[] with the result from the parent query
String[] teste = consulta.NomeArtista(busca);
//custom array for parent
ArrayList<ItemExpandable> ArrayItem = new ArrayList<ItemExpandable>();
//custom array for child
ArrayList<DataAdapter.ItemLista> ArraySubItem = null;
//using the parent size for create all the parent group
for (int i = 0; i < teste.length; i++) {
//Create a array with all the field belong to parent
ArraySubItem = consulta.consultaArtista(teste[i]);
ItemExpandable itens = new ItemExpandable();
itens.setTitle(teste[i]);
itens.setArrayFilhos(ArraySubItem);
Log.v(String.valueOf(ArraySubItem),"2");
ArrayItem.add(itens);
}
//My custom ExpListview with the 2 arraylist
listagem.setAdapter(new CustomBaseAdapter(artista.this.getActivity(), ArrayItem, ArraySubItem));
Related
I'm trying to create an Array of TextViews that each get populated from an Array of objects, but each TextView ends up displaying the last object that I enter.
I've initiated the array of TextViews with
TextView[] loanViewDisplay = new TextView[5];
and inside of my onCreate, I have
loanViewDisplay[0] = findViewById(R.id.loanView1);
loanViewDisplay[1] = findViewById(R.id.loanView2);
loanViewDisplay[2] = findViewById(R.id.loanView3);
loanViewDisplay[3] = findViewById(R.id.loanView4);
loanViewDisplay[4] = findViewById(R.id.loanView5);
I have an addNewLoan button that initiates an Intent and startActivityForResult. The new loan gets added to a array called arrayOfLoans and the program returns to this activity where I want to list each loan from arrayOfLoans in its own TextView.
for(int i=0; i<listOfLoans.length; i++){
if(arrayOfLoans[i] != null){
loanViewDisplay[i].setText(arrayOfLoans[i].loanInformation());
}
}
If I add one loan with a principle of $5000, the output is perfect with
Loan 1: Principle $5000
but if I then click my addNewLoan button and add a second loan with a value of $2000, my output turns into
Loan 2: Principle $2000
Loan 2: Principle $2000
I know my data is being added to arrayOfLoans properly. I've narrowed the problem down to the array of TextViews. Thanks for the help
The problem is in your loop you don't need loop for display name in text view . What you need is just number at which your last position array was populated .
For that, set i=0 initially then use below code line .
if(arrayOfLoans[i] != null){
loanViewDisplay[i].setText(arrayOfLoans[i].loanInformation());
i++;
}
My question is basic but I couldn't find any answer for my question. I have a layout which contains 2 textviews and a gridview. I added this layout into a listview.I want to generate multiple layouts. For example;
ListView------------------1stElement
----------textview1-----------------
----------gridview1-----------------
----------textview1-----------------
ListView------------------2ndElement
----------textview2-----------------
----------gridview2-----------------
----------textview2-----------------
ListView------------------3rdElement
----------textview3-----------------
----------gridview3-----------------
----------textview3-----------------
I tried it like that;
for(int i = 0; i<category.getChildren().size(); i++){
listView.setAdapter(new CategoryListAdapter(this, category.getChildren().get(i)));
}
But of course, it showed my last view only. setAdapter() doesn't suit to my code. I need something like adding views over and over.
Thanks for your helps.
You don't have to do this in for loop, Adapter Takes whole list Once Only, If u do it in a loop it will set the last list visible, So make list in For loop first and then add i to adapter only once.
for(int i=0;i<size;i++)
{
ArrayList<Object> myChldrenList = new ArrayList<Object>();
Object mychild = mycategory.getChildren().get(i)
myChildrenList.add(myChild);
}
listView.setAdapter(new CategoryListAdapter(this, myChildrenList));
u can use Ur Class type Instead of object class or simply Type Cast Object Class.
I am using a collection of ArrayList to fill my Listview. My ListView contains two separate rows types.
Header and Footer.
I am trying to achieve the ExpandableListView Functionality on my Listview from which I am trying to remove some items on click of header till next header.
I am using this function to loop through items and removing items
private void removeItems(int value)
{ Log.e(Constant.LOG, items.size()+"");
for (int i = value;i < items.size(); i++) {
if(!items.get(i).isSection())
items.remove(i);
}
Log.e(Constant.LOG, items.size()+"");
adapter = new EntryAdapter(this, items, this);
mListView.setAdapter(adapter);
}
QUESTION IS : I am not able to remove all items from the list in one shot, some stays there !
I have tried looping through adapter.count(); but no luck
My List :
SECTION 1
ITEM 1
ITEM 2
Item N
Section 2
But when I click on Section 1 not all ITEMS get deleted in one shot WHY!
I am not able to use Expandable Listview at this stage because activity contains many more complex functionality on List. Please help me where I am going wrong!
Create a new ArrayList<Collection> , Then add your item in it and then use removeAll(collection).
TRY THIS:
private void removeItems(int value)
{ Log.e(Constant.LOG, items.size()+"");
ArrayList<Collection> deleteItems= new ArrayList<Collection>();
for (int i = value;i < items.size(); i++) {
if(!items.get(i).isSection())
deleteItems.add(items.get(i));
}
items.removeAll(deleteItems);
Log.e(Constant.LOG, items.size()+"");
adapter = new EntryAdapter(this, items, this);
mListView.setAdapter(adapter);
}
EDIT
Every time you are deleting an item, you are changing the index of the elements inside .
e.g : let suppose you are deleting list1 , then list[2] becomes list1 and hence your code will skip list1 next time because now your counter would be moved to 2.
Here are other ways by which you can achieve this also,
Removing item while iterating it
So what exactly I did now. Instead of looping through items again I did like this :
I created another list and parallely populate it with the main array.
items.add(user);
// after populating items did this
newItems.addAll(items); // same collection ArrayList
and finally I can play with the main array by using removeAll and addAll methods.
items.removeAll(newItems); // remove items
items.addAll(afterPosition,newItems); // add items after position
I am creating one view from XML, I have defined one row in xml and in my main layout I am adding it through layout inflator and setting the id's of component( TextView, EditText, Button) run time. I have three requirements
User can add new row( It is done)
User can delete row ( It is done
I need to fetch the data from the created row. ( It is done too)
I am following this tutorial
https://github.com/laoyang/android-dynamic-views#readme and it is great tutorial as well.
I am creating the ID of each component at run time and adding it to arraylist so that I can fetch the data from it through loop. i.e
for (EditText editText : Quanitity) { }
Problem is that when user presses the delete button on each row, It deletes the row from the layout and its components as well through this code:
Main.removeView((View) v.getParent());
but its corresponding components ID's are already added to the arraylist. I want when user presses the delete button of the row I should get the position of it so that I can remove it through the arraylist as well.
Each row has a textview which is spinner style. I want to open the spinner on click of textview and value should be set for that Textview not all rows.
Please help me in this case. I am really stucked and deadline is today.
Thanks
aray
First thing to achieve this is to get your selected view's ID and after that search the arraylist for that id and if found delete it.That should look something similar to this :
int myEditTextID = myEditText.getId(); // ids of your selected editext
ids.remove(ids.indexOf(myEditTextID)); // ArrayList<Integer> where you are storing your ids.
The code above first get the id of your selected edittext and than search for the it's index in your arraylist and remove it.
That's all! : )
You can get the index of the child in the ViewGroup with indexOfChild(View). You may need to subtract an offset from that index depending on how many child views come before the rows you are adding (if any).
http://developer.android.com/reference/android/view/ViewGroup.html#indexOfChild(android.view.View)
public void onDeleteClicked(View v) {
// get index
int index = mContainerView.indexOfChild((View) v.getParent()) - offset;
// remove from ArrayList
myArrayList.remove(index);
// remove the row by calling the getParent on button
mContainerView.removeView((View) v.getParent());
}
You can get the offset by storing the initial index of the ViewGroup that you will be adding the views (rows) at before you add any. Which, in the case of the link provided (although unnecessary since it equals 0), would be something like this:
private int offset = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
...
// this should be equal to the index where the first row will be inserted
// offset = 0 with the code in your link
offset = mContainerView.getChildCount() - 1;
// Add some examples
inflateEditRow("Xiaochao");
inflateEditRow("Yang");
}
is it possible to programatically access specific rows in a list of CheckedTextViews to change the state of their textboxes?
my program has a listview which has several CheckedTextViews which the user can press to toggle state.
I want to save the state of the checkboxes when the user leaves the activity, so I have in my onPause method:
public void onPause(){
super.onPause();
SparseBooleanArray positions;
positions = listView.getCheckedItemPositions();
ListAdapter items = listView.getAdapter();
int j = items.getCount();
ArrayList<Long> ids = new ArrayList<Long>();
for (int k =0; k < j;k++){
if(positions.get(k)==true){
ids.add(items.getItemId(k));
}
}
this.application.getServicesHelper().open();
this.application.getServicesHelper().storeServices(ids,visit_id);
this.application.getServicesHelper().close();
}
which very simply iterates the list view, adds the checked items to an ArrayList and then saves that list of ids to the database.
My problem lise in trying to reset the list once a user goes back to that activity.
so far in my onStart method, I recall the checked items from the database, but I do not know how to march the ids returned to the listview elements. can I do something like:
listView.getElementById(id_from_database).setChecked?
I know I cant use getElementById but I have it here to show what I mean
Thanks in advance
Kevin
You can call
listView.setItemChecked(int position, boolean value)
This is what Ive ended up doing.. but it seems like a complete hack.
Basically I have to set up a double for loop.. one to iterate through my list elements, and one to iterate through the cursor that I have retreived my check list state (a simply array of ids of elements that were checked when state was last saved)
my outer for iterates through the list elements checking each id against a loop through the list of ids to be set as checked. if they equal each other then set that item as checked.
// mAdapter is contains the list of elements I want to display in my list.
ServiceList.this.setListAdapter(mAdapter);
// Getting a list of element Ids that had been previously checked by the user. getState is a function I have defined in my ServicesAdapter file.
Cursor state = ServiceList.this.application.getServicesHelper().getState(visit_id);
int checks = state.getCount();
int check_service;
int c = mAdapter.getCount();
if(checks>0){
state.moveToFirst();
for (int i=0; i<checks; i++) {
// set check_service = the next id to be checked
check_service = state.getInt(0);
for(int p=0;p<c;p++){
if(mAdapter.getItemId(p)==check_service){
// we have found an id that needs to be checked. 'p' corresponds to its position in my listView
listView.setItemChecked(p,true);
break;
}
}
state.moveToNext();
}
}
ServiceList.this.application.getServicesHelper().close();
Please tell me there is a more efficient way of achieving this!!
Thanks
Kevin