How to divide gridview in sections in android? - android

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.

Related

Android: create checkbox via javacode in class fragment

I'm adding list items containing a checkbox to a list in my fragment class like this:
public class CheckboxList extends Fragment {
...
listViewToDo = (ListView)myView.findViewById(R.id.listViewToDo);
//creates list of names of the default list items
arrayListToDo = new ArrayList<String>();
if(!arrayListToDo.contains("Menu1"))arrayListToDo.add("Menu1");
if(!arrayListToDo.contains("Menu2"))arrayListToDo.add("Menu2");
if(!arrayListToDo.contains("Menu3"))arrayListToDo.add("Menu3");
//sets the checkboxes
arrayListCheck = new ArrayList<>();
for(int i = 0; i < arrayListToDo.size(); i++){
CheckBox cb = new CheckBox(myView.getContext());
cb.setText(arrayListToDo.get(i));
arrayListCheck.add(cb);
}
arrayAdapterCheck = new ArrayAdapter<CheckBox>(listViewToDo.getContext(),
android.R.layout.simple_list_item_checked, arrayListCheck);
listViewToDo.setAdapter(arrayAdapterCheck);
...
I want it to do it this way because the user shall be able to add and delete specific list items.
Though that works fine, my checkboxes look like this:
Can someone tell me what I'm doing wrong?
Ok my fault, I did'nt know that I need a custom ArrayAdapter to use other objects then strings to display in my list.
This helped me a lot to get a better understanding of the Adapter and to write my own one for the checkboxes:
http://techlovejump.com/android-listview-with-checkbox/

Getting a spinner's item list

I can retrieve a list of all items of a spinner by:
List<String> list = new ArrayList<String>();
for (int i =0; i<spinner.getCount(); ++i)
{
String item = String.valueOf(spinner.getItemAtPosition(i));
list.add(item);
}
Or storing the item list globally...
Is there any more elegant way, something like .getItemList()?
My concern is the iteration (linear complexity), I would prefer to directly get the list from the adapter (possibly constant complexity?)
See http://developer.android.com/reference/android/widget/Adapter.html and http://developer.android.com/reference/android/widget/SpinnerAdapter.html (which inherits from Adapter)
You will find that the method you're looking for doesn't exist
Your solution is about as elegant as it gets

Remove items from listview on its ItemClickListener

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

Splitting a string and putting it inside a listview

I asked a question before about splitting string but maybe it wasn't clear enough.
I made a simple activity which has an example to what my problem is.
I have a message and it's a long one coming from a server.
I need to split this message and put it inside a listview, I'll show you my code.
public class Page1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity6);
String message = "0---12,,,2013-02-12 08:04,,,this is a test,,,0---11,,,2013-02-12 08:05,,,and this is why it is damaged,,,0---10,,,2013-02-12 08:06,,,what comes from select data randomly";
String[] variables = message.split(",");
ListView listView1 = (ListView) findViewById(R.id.listView12);
String[] items = { variables.toString() };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
}
}
Now let's say that the split is commas ", " so it will be
0---12 ------->ID1
2013-02-12 08:04 ------------>date1
this is a test ----------->subject1
0---11 ------->ID2
2013-02-12 -8:05 ------------>date2
and this is why it is damaged ----------->subject2
And so on, now what I can't do is that I want to put these strings in a loop and write them to a listview such that the subject1 should be in item1 and date1 should be in subitem1 like this
Subject1
Date1
------
Subject2
Date2
------
This is how the listview should look like
Can anyone help me with this please?
You would need to create a custom ArrayAdapter to populate a ListView from your objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs, in it you will override the getView method and use the position parameter you receive for the corrent row View to indicate the row index.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
Please note that ArrayAdaper is designed for items containing only one single TextView. From the docs:
A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView
Consider subclassing ArrayAdapter (docs) and override its getView method.

ListView getChildAt(i) gives null

I have a listview with with custom Layout. ListView items come from a separate xml document. The listview renders properly, now I want to access the textview and the editview which is in that listview, and I want to change the value of that dynamically. I tried it using list.getChildAt(i) but it gives me a null view. So how can I access the textviews?
ListView list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this,5,R.layout.layout_id);
list.setAdapter(adapter);
for (int j = 0; j < list.getCount(); j++) {
View v = list.getChildAt(j);
EditText text = (EditText) v.findViewById(R.id.textview1);
text.setText("Hello");
}
Ok I tried a lot and found that list.getChildAt(i) gives me value on onClickListener(). I want to get that value after the list render in onCreate() method. So how can i get the list value immediately of list render?
As you said you used a custom Layout, did you write your own Adapter? If so, you properly have to override the getChildAt-method. Hence you have to manage the model on your own.

Categories

Resources