Please see the image to understand the question better.
I have a fragment with an ExpandableListView and few buttons.
The ExpandableListView -> has ListViews as each child elements -> each row in ListView is a combination of ImageView, TextView and a checkbox.
The ExpandableListView is populated by a CustomAdapter ( which is a separate java file from the Fragment )
Question is - how do i find out which Child of the ExpandableListView is selected, from the button.
I could store the information in SharedPreferences, but I might have to use a complex java Collections object. I want to avoid this.
Fragment and Adapter are two separate files and to my knowledge have no systematic callback.
What is the standard way of - a Fragment being able to access each of its child view's information ??
Any help or pointers welcome !! Thanks in advance.
Create a listener call back interface, something like:
public interface ListenerChildSelected
{
public void onChildSelected(int childNum);
}
Then, make your Fragment implement this listener:
public class FragmentTest implements ListenerChildSelected
Then, when you instantiate your custom Adapter, pass in the listener:
//kv 3rd parameter would be listener
CustomBaseAdapter customBaseAdapter = new CustomBaseAdapter(this, items, this);
Then, in the constructor of your CustomBaseAdapter, set a member field to the listener:
public CustomBaseAdapter(Context context, ArrayList<Item> items, ListenerChildSelected listenerChildSelected)
{
mListenerChildSelected = listenerChildSelected;
...
}
Then, every time an item is checked, call:
mListenerChildSelected.onChildSelected(rowNum);
Get the ExpandableListView in onCreateView() and store a reference to it.
In your button's anonymous onClick() handler, call getSelectedPosition() on the reference stored in step 1.
Related
I have created a custom list view whose parent class is Base Adapter. Now i need to delete its menu item .i searched for it and most of the tutorial says to use "remove" method and will be done.But actually in all tutorials they extend their class from Array-adapter.Now problem in my case is i didn't find the remove method in Base Adapter class to use it . so how i can remove my list view item in this case ?.
You won't find a remove in a BaseAdapter, because ArrayAdapter is already an Array so..
What you can do is remove the item from the ArrayList<> and then set it again for the adapter, and after that call notifyDataSetChanged(),
have a setData(ArrayList) in your adapter so you don't have to instantiate a new one. i.e.
setData(ArrayList list) {
this.list = list;
notifyDataSetChanged();
}
hope this helps
I use a CheckBox in my Activity, which is defined in "main.xml" (cbSetAll).
I also have a BaseAdapter, using "item.xml", for setting customized ListItems in a ListView in "main.xml".
Now i want to check all CheckBoxes, depending on cbSetAll. When I fetch the value of cbSetAll, the app crashes. I do this by
boolean bCheckAll = ((CheckBox) view.findViewById(R.id.cbSetAll)).isChecked();
to set the CheckBoxes in BaseAdapter by
((CheckBox)view.findViewById(R.id.cbSetItem)).setChecked(bCheckAll);
If I define
boolean bCheckAll = true;
everything works. I think, the error is, that the CB is in "main.xml" instead of "item.xml" and so the "view" is scoping in the Nirvana. Can someone give a hint?
One way to solve it is to create an additional variable in your custom adapter and set this variable from the parent activity. This way you can control the behaviour in the adapter.
You can add this variable to the constructor of the adapter.
You spotted the problem, you can't access views outside a row from inside. A quick way to do this, would be defining bCheckAll as an instance static variable of your Activity.-
static boolean bCheckAll;
In onCreate method, add a listener to cbSetAll CheckBox so that you can update bCheckAll value, and access it from you adapter's getView method.
I have a list of items which is fetched from the local database. Every item has property isNew. I want to make visible TextView with text "new" only for items which match isNew = true. I solve this problem with two ways, and now I want to know which is best method.
Method 1:
I write a class MyViewBinder which implements SimpleCursorAdapter.ViewBinder and overrides public boolean setViewValue(view, cursor, columnIndex) method with my logic next to that.
Method 2:
Create MySimpleCursorAdapter which extends SimpleCursorAdapter, overwrite getView method and wrote logic there.
Now I'm working with the second method. Can anyone suggest me which is the best method or if there any other best methods.
If, depending on the value of the column is necessary to make many changes over the item in the list, the second method is more convenient.
I have seen related answers and I do not know ehich one is the appropriate for me.
I have a listView and each row has a textview. I want given some conditions, each row to get different color.(Imagine that I am getting data from a DB, and given the value I get, i want text set to different color) My code is shown below:
public class TrailsConditionScreen extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
seeTrails();
}
private void seeTrails() {
String[] help=new String[] {"foo","bar"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.row,R.id.text1,help);
setListAdapter(adapter);
}
}
This code so far just prints them on the list.
Now I want when retrieving values from the db, given the value i get set a different color on each row. imagine that my codes retrieves data from a table an I want when reading the first line , given the number set the appropriate color on the first line of the listview. Then go to second lone of the db,set color for the second line of the list and so on.. i have successfully implemented the reading from the db but i do not know how to set color on specific row. In pseudo-code it looks like this.
for i=0 to number of lines in db{
read_data();
if key_value=0 then color_of_line(i)=red;
else color_of_line_i)=green;
}
Any help for this?
You must create a custom adapter to handle this instead of using the ArrayAdapter. Extend the ArrayAdapter and override the getView method. And inside the getView method based on the condition you can change the color of the text on your textView.setTextColor.
To write a custom adapter check the tutorial here (6. Tutorial: Implementing your own adapter). This example doesn't use the holder pattern but you should.
Your best bet is to extend ArrayAdapter and create your own class.
In the getView() method, execute your condition and set the color to be drawn. There are a lot of good tutorials on creating your own adapters. Check, for instance, http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter.
Hope it helps!
Inorder to achieve this, you will need to use a custom adapter. Since you mentioned, you are getting the data from a database, I am assuming here that you will have information in a cursor adapter. Hence, a good place to start would be to extend cursor adapter.
Within your new custom adapter, you should override getView() method. The view to be used for each item in list view is specified there. Additionally, you can also set the properties of the view there.
Depending on your business logic, you should set the text color in getView.
A tutorial here:
http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter
What I would do is create a CustomAdapter for the list view
public class MyCustomAdapter extends BaseAdapter
and in the funcion
public View getView(int position, View convertView, ViewGroup parent)
you have the position and you can change whatever you want.
If you search google by custom adapter listview you will get some examples
Different Color For different list item ,yes this can be achieved only if items are static only , but for dynamic content it's hard to do ...
I have a gridview using an image adapter for the elements,
it's implemented like in this example:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
Additionally I added a click listener for each item in the getView method, which sends the clicked position to the main class (outside of ImageAdapter) using a handler.
Now I want to update only the concerned imageView, but:
I don't know how to get the imageView outside of the ImageAdapter class (send with the handler? It's not serializable - create a buffer in ImageAdapter and getter?)
I'm not sure which method to use to change the image.
Currently I'm updating the whole grid each time:
((ImageAdapter)gridview.getAdapter()).setImages(imageIds);
gridview.invalidate();
ImageAdapter:
public void setImages(int[] images) {
mImages = images;
notifyDataSetChanged();
}
Thanks in advance
What you are currently doing is somewhat correct.
As an optimization, you could use an ArrayList<int> as the images and create a method in your adapter class to modify the value of a given index using ArrayList.set(int index, E element). Then calling notifyDataSetChanged() method should "theoretically" update the changed image view only :)