public class DietSlideAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public DietSlideAdapter(Context context){
this.context = context;
}
//Arrays
public int[] slide_images2 = {
R.drawable.breakfast,
R.drawable.cabbage,
R.drawable.protein,
R.drawable.fish,
R.drawable.tea
};
public String [] slide_headings2 = {
"heading1","heading2","heading3","heading4","heading5"
};
public String [] slide_descs2 = {
"Try fortified ready-to-eat or cooked breakfast cereals with fruit. Fortified cereals have added nutrients, like calcium.",
"Choose a variety of vegetables and fruits, like carrots, cooked greens, bananas, and melon.\n" +
"Eat plenty of beans and whole grains. Try brown rice or oatmeal.",
"Iron keeps your blood healthy. Folic acid helps prevent birth defects.\n" +
"Even before you find out you're pregnant, it's smart to start eating plenty of folate-rich foods like fortified cereals, asparagus, lentils, wheat germ, oranges, and orange juice.",
"Avoid fish and shellfish with high levels of mercury. Common fish that are low in mercury include shrimp, and catfish. ",
"Drink decaffeinated coffee or tea.\n" +
"Drink water or seltzer instead of soda.\n" +
"Don't drink alcohol."
};
#Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide2, container, false);
ImageView slideImageView2 = (ImageView) view.findViewById(R.id.slideImage2);
TextView slideHeading2 = (TextView) view.findViewById(R.id.slide_heading2);
TextView slideDescription2 = (TextView) view.findViewById(R.id.slide_desc2);
slideImageView2.setImageResource(slide_images2[position]);
slideHeading2.setText(slide_headings2[position]);
slideDescription2.setText(slide_descs2[position]);
slideDescription2.setMovementMethod(new ScrollingMovementMethod());
container.addView(view);
return view;
}
}
I want to make my app support multiple language.
I want to access string.xml instead of the Hard-coded strings in the Arrays slide_headings2 and slide_desc2.
I looked everywhere and I tried context.getResources().getString(R.string.string_name).
How do i replace the hard coded strings in the array with a R.string from string.xml?
String[] slide_images2 = {
context.getString(R.string.heading1),
context.getString(R.string.heading2),
context.getString(R.string.heading3),
context.getString(R.string.heading4),
context.getString(R.string.heading5),
};
Simple like this ^
Related
I've got an SQLite database with a units table. The units table is set up with only two columns:
create table units (_id INTEGER PRIMARY KEY, desc TEXT)
Example data for a row in this table is:
_id: 4
desc: "Helix #5 [2231]"
The "[2231]" substring is important, and I'd like to change its color to a medium gray color. Id also prefer to do this to the data in the desc column, as opposed to manipulating it with java.
So, I query for the data:
/**
* Get all unit records for display in spinner
*/
public Cursor getAllUnitRecords(){
String sql = "select * from units order by `desc`";
return db.rawQuery(sql, null);
}
My spinner looks like this:
<Spinner
android:id="#+id/UnitSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
And I get the data to the spinner like this:
// Prepare unit dropdown
Cursor units = db.getAllUnitRecords();
MatrixCursor unitsMatrixCursor = new MatrixCursor(new String[] { "_id", "desc" });
unitsMatrixCursor.addRow(new Object[] { 0, "" });
MergeCursor unitsMergeCursor = new MergeCursor(new Cursor[] { unitsMatrixCursor, units });
String[] unitsFrom = new String[]{"desc"};
int[] unitsTo = new int[]{android.R.id.text1};
Spinner unitSpinner = (Spinner) findViewById(R.id.UnitSpinner);
SimpleCursorAdapter unitAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, unitsMergeCursor, unitsFrom, unitsTo, 0);
unitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
unitSpinner.setAdapter(unitAdapter);
Since I'd like to color the "[2231]" substring a medium gray color, I thought I might be able to change the value of desc in the database, so that it looks like this:
"Helix #5 <font color='#6e737e'>[2231]</font>"
I did that only because I was searching the internet, and it seemed like it might work. Well, that doesn't work, as the tags are just output, instead of changing the color. What is wrong, and how can I fix it? I guess I'm open to a different solution if necessary, but this Android stuff is hard for me, as I don't work on it very often, so I was trying to go for the easiest solution.
UPDATE #1 ----------------------
So #MartinMarconcini was kind enough to point me in the right direction, and I copy and pasted his colorSpan method into my activity class to test it out. I then looked all around Stack Overflow for any clues as to how to modify the text of my spinner, and then how to modify the text that's in a SimpleCursorAdapter.
I found these questions with answers:
Android, using SimpleCursorAdapter to set colour not just strings
Changing values from Cursor using SimpleCursorAdapter
That gave me some ideas, so I tried to work with that:
// Prepare unit dropdown
Cursor units = db.getAllUnitRecords();
MatrixCursor unitsMatrixCursor = new MatrixCursor(new String[] { "_id", "desc" });
unitsMatrixCursor.addRow(new Object[] { 0, "" });
MergeCursor unitsMergeCursor = new MergeCursor(new Cursor[] { unitsMatrixCursor, units });
String[] unitsFrom = new String[]{"desc"};
int[] unitsTo = new int[]{android.R.id.text1};
Spinner unitSpinner = (Spinner) findViewById(R.id.UnitSpinner);
SimpleCursorAdapter unitAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, unitsMergeCursor, unitsFrom, unitsTo, 0);
unitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
/* NEW CODE STARTS HERE */
unitAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
if (aColumnIndex == 1) {
String desc = aCursor.getString(aColumnIndex);
TextView textView = (TextView) aView;
final Spannable colorized = colorSpan(desc);
textView.setText(TextUtils.isEmpty(colorized) ? desc + "a" : colorized + "b");
return true;
}
return false;
}
});
/* NEW CODE ENDS HERE */
unitSpinner.setAdapter(unitAdapter);
Notice I added the letter "a" if there was no text, and "b" if there was text. Sure enough, the "a" and "b" were added to my spinner items, but there was no color change! So, I am trying ... but could still use some help. Here is an image of what I'm seeing:
As mentioned in the comments, the presentation shouldn’t be tied to the logic. This is a presentation problem. You want to display a text and you want part of that text to be colored.
So, anywhere in your app where you need to display/present this text to the user, say…
someTextViewOrOtherWidget.setText(yourString);
…you’ll then have to call a method that does the coloring for you.
Example…
I’d move this code into a separate method/place for reuse and make it more re-usable by not hardcoding the [] and such,but this is how a simple example would look:
private Spannable colorSpan(final String text) {
if (TextUtils.isEmpty(text)) {
// can't colorize an empty text
return null;
}
// Determine where the [] are.
int start = text.indexOf("[");
int end = text.indexOf("]");
if (start < 0 || end < 0 || end < start) {
// can't find the brackets, can't determine where to colorize.
return null;
}
Spannable spannable = new SpannableString(text);
spannable.setSpan(
new ForegroundColorSpan(Color.BLUE)
, start
, end
, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
return spannable;
}
And you’d use it like…
String text = "Hello [123123] how are you?";
final Spannable colorized = colorSpan(text);
textView.setText(TextUtils.isEmpty(colorized) ? text : colorized);
I hope this gives you a better idea how to get started.
So, with a lot of help from #MartinMarconcini, I finally achieved what needed to be done, and so I wanted to leave "the answer" here, in case anyone else wants to see what needed to be done. I ended up making a custom cursor adapter, and although I'm still dumbfounded by the complexity of Android Studio, it works!
First, in the activity, the way SimpleCursorAdapter was being used ended up getting changed to the custom cursor adapter (which extends SimpleCursorAdapter).
These lines:
Spinner unitSpinner = (Spinner) findViewById(R.id.UnitSpinner);
SimpleCursorAdapter unitAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, unitsMergeCursor, unitsFrom, unitsTo, 0);
unitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
unitSpinner.setAdapter(unitAdapter);
Were replaced with these lines:
Spinner customUnitSpinner = (Spinner) findViewById(R.id.UnitSpinner);
UnitSpinnerCursorAdapter customUnitAdapter = new UnitSpinnerCursorAdapter(this, R.layout.unit_spinner_entry, unitsMergeCursor, unitsFrom, unitsTo, 0);
customUnitSpinner.setAdapter(customUnitAdapter);
I put the custom cursor adapter in its own file, and I put Martin's colorSpan method in there too (for now):
package android.skunkbad.xxx;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class UnitSpinnerCursorAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
public UnitSpinnerCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.context = context;
this.layout = layout;
}
/**
* newView knows how to return a new spinner option that doesn't contain data yet
*/
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
super.newView(context, cursor, parent);
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int descCol = c.getColumnIndex("desc");
String desc = c.getString(descCol);
final Spannable colorized = colorSpan(desc);
TextView unit_spinner_entry = (TextView) v.findViewById(R.id.custom_spinner_entry_desc);
if (unit_spinner_entry != null) {
unit_spinner_entry.setText(TextUtils.isEmpty(colorized) ? desc : colorized);
}
return v;
}
/**
* bindView knows how to take an existing layout and update it with the data pointed to by the cursor
*/
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
int descCol = cursor.getColumnIndex("desc");
String desc = cursor.getString(descCol);
final Spannable colorized = colorSpan(desc);
TextView unit_spinner_entry = (TextView) view.findViewById(R.id.custom_spinner_entry_desc);
if (unit_spinner_entry != null) {
unit_spinner_entry.setText(TextUtils.isEmpty(colorized) ? desc : colorized);
}
}
private Spannable colorSpan(final String text) {
if (TextUtils.isEmpty(text)) {
// can't colorize an empty text
return null;
}
// Determine where the [] are.
int start = text.indexOf("[");
int end = text.indexOf("]");
if (start < 0 || end < 0 || end < start) {
// can't find the brackets, can't determine where to colorize.
return null;
}
end++; /* Why do we even need this ? */
Spannable spannable = new SpannableString(text);
spannable.setSpan(
new ForegroundColorSpan(Color.rgb(100,100,100))
, start
, end
, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
return spannable;
}
}
Finally, I had to make a layout file for each entry in the spinner:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="#+id/custom_spinner_entry_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#FFFFFF"
android:layout_marginLeft="10dp" />
</LinearLayout>
Thanks Martin! It might seem like nothing to you, but it was hard for me, and I couldn't have done it without your help.
One note: I had to put end++; in your colorSpan method, because for some reason it wasn't coloring the closing bracket.
So I have been looking around for about 4 hours now and maybe I just don't know how to word my search, but I am not finding out what I need.
I am using a custom adapter to setup my listview from items I have stored into an sqlite database. My cursor sends id, name, product_id, data, datetime to my adapter. But my adapter is set to only display the name, data & datetime.
I need to be able to click on the name, but have it go to my next activity and display the information from the product_id that was sent to the adapter. Example I click on Apples, but it would actually be product_id 505 in my database. Unless I set the product_id to a textview, when I click on my item, it does not bring up the correct listing and searches for Apples.
If this is not clear, please let me know and I will to explain it better.
** EDIT **
So my database is like this
_ID | PRODUCT_NAME | PRODUCT_ID | PRODUCT_DATA | DATE
1 Apples 505 Fresh Apples 123456
2 Oranges 506 Fresh Orange 123456
3 Kiwi 507 Fresh kiwi 123456
4 Nuts 508 Fresh Nuts 123456
My ListView shows this:
Apples- FRESH APPLES - DATE
Oranges - FRESH ORANGE - DATE
Kiwi- FRESH KIWI - DATE
Nuts - FRESH NUTS - DATE
When I click on Apples, I want it to return PRODUCT_ID to me, not _ID, so I can pass that product_id onto my next intent.
My Display listing
public void displayListView()
{
DatabaseOperations db = new DatabaseOperations(this);
Cursor cursor = db.getProducts();
dataAdapter = new MainActivityAdapter(
this,
cursor,
0);
ListView listView = (ListView) findViewById(R.id.product_list);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
// listening to single list item on click
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long ID)
{
TextView listText = (TextView) view.findViewById(R.id.name);
String product_name = listText.getText().toString();
Intent i = new Intent(MainActivity.this, product_detail.class);
i.putExtra("productName", product_name);
MainActivity.this.startActivity(i);
}
});
db.close();
}
My Adapter
public class MainActivityAdapter extends CursorAdapter
{
private LayoutInflater cursorInflater;
private Context c;
// Default constructor
public MainActivityAdapter(Context context, Cursor cursor, int flags)
{
super(context, cursor, flags);
cursorInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
c = context;
// Log.i("Custom-Adapter", "Starting up");
}
public void bindView(View view, Context context, Cursor list)
{
TextView PN = (TextView) view.findViewById(R.id.product_name);
TextView PI = (TextView) view.findViewById(R.id.product_data);
TextView date = (TextView) view.findViewById(R.id.date);
// SET OUR DATA FROM OUR CURSOR
String pn = list.getString( list.getColumnIndex("pn"));
String pi = list.getString( list.getColumnIndex("pi"));;
// NEED TO RECONVERT DATE THEN SHOW IT
Long millidate = list.getLong(list.getColumnIndex("dateTime"));
Date myDateNew = new Date(millidate);
String Sdate = getDate(millidate);
PN.setText(pn);
PI.setText(pi);
date.setText(Sdate);
}
public static String getDate(long milliSeconds)
{
Calendar cl = Calendar.getInstance();
cl.setTimeInMillis(milliSeconds); //here your time in miliseconds
String date = "" + cl.get(Calendar.DAY_OF_MONTH) + ":" + cl.get(Calendar.MONTH) + ":" + cl.get(Calendar.YEAR);
String time = "" + cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND);
return date;
}
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
//Log.i("Custom-Adapter", "newView Started");
// R.layout.list_row is your xml layout for each row
return cursorInflater.inflate(R.layout.activity_list, parent, false);
}
I want to get the product_id when I click on the name Apples.
We can achieve it in multiple ways but most common ways are :
Option 1:
Use setTag method for saving id of product with name in TextView :
PN.setText(pn);
PN.setTag((list.getInt(list.getColumnIndex("product_id"))).toString());
Now use getTag method to get clicked product product_id in onItemClick :
String product_name = listText.getText().toString();
int product_id = Integer.parseInt(listText.getTag().toString());
Option 2:
Instead of getting product name from TextView, get clicked row cursor using Adapter in onItemClick :
Cursor cursor = (Cursor) dataAdapter.getItem(position);
String product_name = cursor.getString(cursor.getColumnIndex("pn"));
int product_id = cursor.getInt(cursor.getColumnIndex("product_id"));
....
A good option is to create an array and store product_ids for all the names.
Goto Res->Strings create an array of strings .
then fetch from the array the relevant product_id for the name.
Basically I am inserting text along with new line character in mysql db like this:
$message = 'Hello World' . "\n";
$message .= 'This is a test' . "\n";
$message .= 'Thanks;
When saving message in db, I have tried setting value of new line as \n, \\n, \\\n, \\\\n and even <br> instead of \n then used fromHtml on android app but nothing working out.
In db, I can see there is new line.
On Android app I want to have new line character if any showed in TextView, I have tried various things but nothing is working, I've tried things like:
String message = m.getMessage().toString();
message = message.replaceAll("\\n",
System.getProperty("line.separator"));
Here instead of "\\n", I have also tried "\\\n", "\n" and even "\\\\n"
And:
Spanned html = Html.fromHtml(m.getMessage());
And:
message.replaceAll("\n","<br />");
message.replaceAll("\r\n","<br />");
message.replaceAll("\r","<br />");
And:
android:singleLine="false"
With various things tried, in TextView I get text like these permutations:
Hello WorldThis is a testThanks
Hello World\This is a test\Thanks
Hello World\nThis is a test\nThanks
Here is my complete code:
public class MessageListAdapter extends BaseAdapter {
private Context context;
private List<ListMessage> messagesItems;
public MessageListAdapter(Context context, List<ListMessage> navDrawerItems) {
this.context = context;
this.messagesItems = navDrawerItems;
}
#Override
public int getCount() {
return messagesItems.size();
}
#Override
public Object getItem(int position) {
return messagesItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
/**
* The following list not implemented reusable list items as list items
* are showing incorrect data Add the solution if you have one
* */
ListMessage m = messagesItems.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// Identifying the message owner
if (messagesItems.get(position).isSelf()) {
// message belongs to you, so load the right aligned layout
convertView = mInflater.inflate(R.layout.list_item_message_right,
null);
} else {
// message belongs to other person, load the left aligned layout
convertView = mInflater.inflate(R.layout.list_item_message_left,
null);
}
TextView lblFrom = (TextView) convertView
.findViewById(R.id.lblMsgFromListMessage);
TextView lblTo = (TextView) convertView
.findViewById(R.id.lblMsgToListMessage);
lblFrom.setText(m.getFromName());
// Spanned html = Html.fromHtml(m.getMessage());
// String message = html.toString();
String message = m.getMessage().toString();
message = message.replaceAll("\\n",
System.getProperty("line.separator"));
try {
lblTo.setText(message);
} catch (Exception e) {
lblTo.setText(message);
}
return convertView;
}
}
TextView in layout:
<TextView
android:id="#+id/lblMsgToListMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:background="#drawable/chat_bg_msg_from"
android:paddingBottom="#dimen/five_dp"
android:paddingLeft="#dimen/ten_dp"
android:paddingRight="#dimen/ten_dp"
android:paddingTop="#dimen/five_dp"
android:textColor="#color/chat_title_gray"
android:textSize="#dimen/eighteen_sp" />
Is something wrong with baseAdaptor? Somewhere I saw this link, it suggested SimpleAdapter has problem setting html but in my case this doesn't help either since I am using BaseAdapter.
I will be really realy thankful for your help as I have wasted five hours on this new line issue :(
Try:
message = message.replaceAll("\\n", "
");
or (only if there really are double backslashes in the input)
message = message.replaceAll("\\n", "
");
Update: Replacements were not working as the search string (\n) had been stripped out (escaped) by the sql database. This answer supplied a workaround which involves running unescape on the string prior to setting it on the TextView.
I know that Parse.com is a NoSql database and SQL concepts should not be applied to it, anyway, besides theory, i bet there is a way to do in Parse something like classic SQL joins.
For example, assume we have 3 tables/classes in our DB: People, Cities and Countries, with example values in brackets.
People:
-Name (Mario Rossi)
-Age (23)
-City (Bologna)
Cities:
-Name (Bologna)
-PostalCode (40100)
-Country (Italy)
Countries:
-Name (Italy)
-Continent (Europe)
Given this structure, let's say I want to know in which continent the person Mario Rossi, aged 23, lives. In a classical SQL approach this would be accomplished easily like:
select People.Name,People.Age,Countries.Continent from People
JOIN Cities on People.City = Cities.name
JOIN Countries on City.Country = Countries.Name
Where People.Name = 'Mario Rossi'
The resulting recordset would be "Mario Rossi, 23, Europe". Now, if i want to make the same thing with Parse, or better, if i want to get the same result with Parse, what should my structure look like? Should i use pointers? References? I read about these structures but i don't know if they apply to this case and how to use them.
I'm developing in Android, but since this is a basic question i think i may accept/understand answers based on other platforms too.
Thank you!
Edit after Fosco suggestions:
public class CustomAdapter extends ParseQueryAdapter <ParseObject> implements OnItemClickListener{
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("People");
query.include("City");
query.include("City.Country");
query.whereEqualTo("Name","Mario Rossi");
return query;
}
});
}
#Override
public View getItemView(ParseObject parseobject, View v, ViewGroup parent) {
if (v==null){
v = View.inflate(getContext(), R.layout.row_ppl, null);
}
super.getItemView(parseobject, v, parent);
ParseObject city = parseobject.getParseObject("City");
ParseObject country = City.getParseObject("Country");
String continent = country.getString("Continent");
TextView nome = (TextView) v.findViewById(R.id.textView1);
nome.setText(parseobject.getString("Name"));
TextView cont = (TextView) v.findViewById(R.id.textView2);
cont.setText(continent);
return v;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.d("rilevato ", "click");
}
}
If you create Country and City as classes on Parse, and use pointers, you can do this very easily.
Cities contains a pointer to Countries, column name 'country'
People contains a pointer to Cities, column name 'city'
var query = new Parse.Query("People");
query.include('city');
query.include('city.country');
query.equalTo('Name', 'Mario Rossi');
query.first().then(function(person) {
console.log(person);
}, function(err) {
});
You'll have the full city & country records when you pull the people record.
edit to add Android example:
ParseQuery<ParseObject> query = ParseQuery.getQuery("People");
query.include("city");
query.include("city.country");
query.whereEqualTo("Name", "Mario Rossi");
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject person, ParseException e) {
if (e == null) {
ParseObject city = object.get("city");
ParseObject country = city.get("country");
String continent = country.get("continent");
} else {
Log.d("person", "Error: " + e.getMessage());
}
}
});
Been playing around with Android and using various resources have come up with this list:
public class TestingList extends ListActivity {
private static final String ICON_KEY = "icon";
private static final String TITLE_KEY = "title";
private static final String DETAIL_KEY = "detail";
private static final int[] ICONS = new int[] { R.drawable.lista,
R.drawable.listb, R.drawable.listc, R.drawable.listd };
private static final String[] TITLES = new String[] { "List 1", "List 2",
"List 3", "List 4" };
private static final String[] DETAILS = new String[] {
"List 1 description, a little more text here please, just testing how it reacts to more.",
"List 2 description, a little more text here please, just testing how it reacts to more.",
"List 3 description, a little more text here please, just testing how it reacts to more.",
"List 4 description, a little more text here please, just testing how it reacts to more." };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
for (int i = 0; i < ICONS.length; i++) {
rows.add(createListItemMap(ICONS[i], TITLES[i],
DETAILS[i]));
}
// set up SimpleAdapter for icon_detail_list_item
String[] fromKeys = new String[] { ICON_KEY, TITLE_KEY, DETAIL_KEY };
int[] toIds = new int[] { R.id.icon, R.id.text1, R.id.text2 };
setListAdapter(new SimpleAdapter(this, rows,
R.layout.icon_detail_list_item, fromKeys, toIds));
}
I'd like to some how add list separators to this list, what would the best way to get that done. The final list will have a lot more items, but I'd like named separators at certain points.
if you want to add a single divider that will be the same for each row than that is simple. you just call setDivider(Drawable) on your list and add the drawable that will represent your divider.
If you want to divide the list by categories like divider then things get more complicated.
#CommonsWare has a nice demo and a library of how to achieve this in his cwac-Merge project.
here is a link: Cwac
have a look at SeperatedListAdapter or android-section-list