I'm using a MergedAdapter to group two custom adapters (ArrayAdapter derived) along with a section header for each one into a single ListView. It is working fine, but now I need to display a TextView saying "No Data" for the section that has no items, e.g. ArrayAdapter is empty.
What's the best approach for this? The code that sets the ListView binding is like this:
ArrayList<ItemOne> firstItems = getFirstGroupItems();
ArrayList<ItemTwo> secondItems = getSecondGroupItems();
ItemOneAdapter firstAdapter = new ItemOneAdapter(this, this.firstItems);
ItemTwoAdapter secondAdapter = new ItemTwoAdapter(this, this.secondItems);
MergeAdapter adapter = new MergeAdapter();
adapter.addView(createGroupSeparator(R.string.first_section_header)); //Just creates a TextView
adapter.addAdapter(firstAdapter);
adapter.addView(createGroupSeparator(R.string.second_section_header)); //Just creates a TextView
adapter.addAdapter(secondAdapter);
listView.setAdapter(adapter);
In case of an empty list, you could make the first item read "No Data", but this usually makes the code unmanageable. I would suggest you add an invisible TextView to your screen with the message; this is only displayed when the list is empty.
Related
I am looking to create a list with a format looking something like this:
Sensor 1
var1=4, var2=4
Sensor 2
var1=2, var2=-12
So I would like to have a bold header, and then some other info on a separate line in each item in the list. This is how I am currently adding things to my list.
View sensorView = inflater.inflate(R.layout.fragment_sensor, container, false);
ListView sensorList = (ListView)sensorView.findViewById(R.id.sensorList);
String [] sensors = {"Sensor2\nAlpha=2", "Sensor3\nAlpha=2"};
ListAdapter sensorListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, sensors);
sensorList.setAdapter(sensorListAdapter);
I've tried using Html.fromhtml() but it didn't work. Any ideas?
You could create a custom adapter view to replace android.R.layout.simple_list_item_1 that has two TextView views, one for the header and one for the content. Then just override the ArrayAdapter class to set the text to each view.
Example: Custom Adapter for List View
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/
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.
I'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.
Here is my code, please help.
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add(0,"");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setSelection(0);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("");
I can see two ways to do this.
1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).
2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.
I want to draw some section headers in android listview just like the Contacts app did.
When the listview was dragged the section headers will move flatly,thanks.
Just look at this Android – Sectioned Headers in ListViews example, Its nicely describe how to implement Sectioned Headers in ListViews.
And
android-amazing-listview
Jeff Sharkey's SeparatedListAdapter
MergeAdapter by CommonsWare
Thanks.
Create a HEADER LAYOUT in your List Item Layout. We use the VISIBILITY option to show and hide the HEADER LAYOUT. This will act like a section header.
In the adapters "getView" method, check the first letter of the "name field (in the case you are showing in accords to Name)" with the first letter of the previous LIST ITEMS "name field". If it macthes hide the HEADER LAYOUT (with a text view) else show the HEADER LAYOUT with the Header Text showing the first letter of the Name Field.
Here is the code
String nameFirstLetter = "A"; // Declare this globally, not inside the getView.
// Inside the getView
String nameF = Name.slice(0,1);
if(!nameFirstLetter.equals(nameF )){
nameFirstLetter = nameF ;
holder.headerText.setText(nameFirstLetter );
holder.headerLayout.setVisibility(View.VISIBLE);
}else{
holder.headerLayout.setVisibility(View.GONE);
}
This is the easiest method to show section header in Android List view, but it wont work like Iphone section header, ie. The section header hide along with other list items when we scroll up/down.
I found some examples in Android can resolve this issuse:
Please find the example about PinnedHeaderListView
PinnedHeaderListView Example
If anyone needs a different solution, especially those more used to iOS development, prefer it, or want to emulate the iOS look and feel; I recommend the following:
http://applidium.com/en/news/headerlistview_for_android/
The logic is the same as for iOS, and does most of the leg-work for you
You can have a look into SectionedMergeAdapter. If you have multiple sub-lists of data, you can stitch them together and have headers for them.
Example code -
ListView listView = (ListView) findViewById(android.R.id.list);
ArrayAdapter<Integer> adapter1 =
new ArrayAdapter<>(this, R.layout.item_list, android.R.id.text1, arrayList1);
ArrayAdapter<Integer> adapter2 =
new ArrayAdapter<>(this, R.layout.item_list, android.R.id.text1, arrayList2);
ArrayAdapter<Integer> adapter3 =
new ArrayAdapter<>(this, R.layout.item_list, android.R.id.text1, arrayList3);
TextView tv1 = new TextView(this);
tv1.setText("Header 1");
TextView tv2 = new TextView(this);
tv2.setText("Header 2");
TextView tv3 = new TextView(this);
tv3.setText("Header 3");
SectionedMergeAdapter adapter = new SectionedMergeAdapter();
adapter.addSection(new SectionedMergeAdapter.Section(tv1, adapter1));
adapter.addSection(new SectionedMergeAdapter.Section(tv2, adapter2));
adapter.addSection(new SectionedMergeAdapter.Section(tv3, adapter3));
listView.setAdapter(adapter);