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
Related
Is it possible to show a thumbnail with a Array adapter in a list? The thumbnail is always the same.
final ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 );
ListView myList = (ListView) findViewById(R.id.list);
myList.setAdapter(myAdapter);
I populate my list with volley.
ArrayAdapter is usually used for populating a ListView widget from array or list of primitive values like String. When you are creating an ArrayAdapter with ArrayAdapter myAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1 ); as the second parameter you have R.layout.simple_list_item_1 which is a layout with only one TextView, so if you need a ImageView alongside a TextView, you will need to create new view that will contain TextView and ImageView widgets and create custom class that will extend Adapter where you will use the newly created view and populate it's views with your content.
I have this layout in which I have a listview. The content of list is defined in another layout.I have text views inside this layout and I want to dynamically change the color of the text view depending on some condition(like simple if else).Please let me know how to do this.
Here is my code-:
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.recentlist,
new String[] { KEY_CAT, KEY_DATE, KEY_TID, KEY_AMO, KEY_DEB,KEY_CUR,KEY_BAL,KEY_FEES}, new int[] {
R.id.textView1,R.id.textView2, R.id.textView3, R.id.textView4,R.id.textView5,R.id.textView7,R.id.textView6,R.id.textView8});
setListAdapter(adapter);
This particular layout I have added from another xml file to put the contents of listview in this.So I cant setTextColor by taking textView because it takes the id of this class's layout and not this layout.Please help me regarding this.
AFAIK you cant use a SimpleAdapter to perform this operation as SimpleAdapter only copies data from data set to TextView in your ListView. In order to handle color changes at run-time you should define a CustomArrayAdapter.
Override getView method of SimpleAdapter by extending it to your own class. Inside getView method, you can use View.findViewById and then do whatever you want to do with your TextView
TextView tv1 = (TextView) vi.findViewById(R.id.your_textview_id);
// where vi is convertview
tv1.setTextColor(Color.BLUE);
I'm trying to bind data do a listview on android, but I'm not able to.
I saw some code on the internet and it worked, but I just don't know why, and I don't want to create a new ListView on the fly, I want to use the one that is listed on the main.xml
Why I can do this:
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,PEOPLE));
setContentView(lv);
But I can't do this:
ListView listPessoas = (ListView) findViewById(R.id.listPessoas);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listPessoas, PESSOAS);
listPessoas.setAdapter(adapter);
What is R.id.pessoas??
This works fine for me:
String[] x = new String[]{"AAA","BBB","CCC"};
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> test = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,x);
lv.setAdapter(test);
Your error here is when you are creating the new ArrayAdapter. You are passing R.id.listPessoas as the row view to use for each row. This is the id of the ListView. The adapter is looking for a layout id containing a text view to be used for each row of the list. Change the R.id.listPessoas to android.R.layout.simple_list_item_1 and your code should work. The simple_list_item_1 layout is just a TextView that the data will be bound to.
What you have written is not correct, the second parameter in the array adapter constructor should be the simple layout for each list item not again your list view.
If your have a custom complex layout for your list item you need to write a custom adapter as well.
If you are getting error.
getApplicationContext(); will work instead of 'this'.
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);
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.