Converting ArrayList<T> to CharSequence[] - android

I want to convert my ArrayList containing elements with the toString method shadowed in "T":
public String toString(){
return name + " " + realname;
}
to a CharSequence array containing all the "T"s toString. Checked some stuff out but nothing works for me since CharSequence[] can't be concatenated (correct me if i'm wrong).
Saw a solution for the ArrayList at -> ArrayList<String> to CharSequence[] which didn't work out for me
I'm doing this because I've searched for devices ("T") and added them to a Arraylist, the user then has to make a choice which one to accept by clicking on a mutliple choice dialogwindow as shown under DIALOG_TEXT_ENTRY in this link
Please help me out cause it's driving me mad

So through the responses via the comments you guys suggested a solution with subsequence. However this is a similar solution I guess and it's semi-implemented in my code already so I'm gonna go with this one with a few modifications ofcourse.
Feel free to still give feedback if it's a bad way to implement or if there actually exists other ways of doing it!
::EDIT::
The solution was the following: I tried ot the code given by the link and take note that my ArrayList.toString() returns a String object with the following look for each element in the List
"[foo, bar, super, duper]"
Taken to account that the toString() is overriden in the my object class. The following is done
private String[] stringToArray(String str){
str = str.substring(1, str.length()-1);
String[] str2array = str.split(", ");
return str2array;
}
And voila I have the String[] array and can now represent them as choices in my single choice list.

Related

How to make a generalized use from multiple array

I'm creating an android app which has selection of company and based on that I show cars and based on selection of particular car, I want to show some details of car e.g. capacity, power, engine oil etc. Now I'm using array to store all this car related information. e.g.
private static final String[] car1={"624CC","33bhp","10W40"}
private static final String[] car2={"1600CC","120bhp","10W40"}
To check the section, I've used multiple if-else statements for each car. And depending on the selection, I show value form array.
if(bn.equals("car 1"))
{
cap.setText(""+car1[0]);
powr.setText(""+car1[1]);
oil.setText(" "+car1[2]);
}
else if(bn.equals("car 2"))
{
cap.setText(""+car2[0]);
powr.setText(""+car2[1]);
oil.setText(" "+car2[2]);
}
else if(bn.equals("car 3"))
{
cap.setText(""+car3[0]);
powr.setText(""+car3[1]);
oil.setText(" "+car3[2]);
}
Now the problem is as the number of if-else have increased,I'm getting error of "Code too large" in android studio.
I was wondering if is there any way to replace these multiple if else statements with single, generalised statement. As you can see, in all if else, the code is the same, its just that array name is different.
I'm aware that I can use SQLite db, but I'll have to add all the values to it for all the cars again. So was wondering if I can use the same array that I've created. Any solution/suggestion will be really helpful
Probably in place of string comparison of names you can store the index of the value selected and use switch case to find out which car is selected. This should reduce the code also make it much more readable.

output text line by line

I write app for Android such gets data from server in JSON format. Now I get this value in string, but in my application it must look like:
Route:
1)first point
2)secon point
3).....
n) n point
I read that in Android in textView I can do it if string will be with html tags but I think it is not the best variant. After Android I must do it in iPhone now I don't know how to do that there. Send Routes as Array is not good variant too. Can you say what is the best way to decide this problem?
Have a look here you will have to find the good pattern .
Hence you have separated strings just use a list View with an ArrayAdapter.
I am not so good with regex but i think it should like : [1-9][0-9]) [[a-f][0-9]]+
I couldn't comment b/c of rep, sorry. Could you provide an example of returned JSON string. I think JSON format can be parsed with ease.
If this the case you can parse it in a loop (or another way. I'm not that good at it)
String[] parseIt (String JSON){
String[] list=JSON.split("\\d\\)");
String[] rlist=new String[list.length-1];
for(int i=0;i<list.length-1;i++){
rlist[i]=list[i+1].trim();
}
return rlist;
}
This might do trick. But you should edit result. I didn't test yet
Edit: I edited code. It simply return the address now with leading whitespace. You can get rid off them using. String trim() method like;
list[1].trim();
Do it in loop and don't care about first element (index 0).
Edit 2: Now it should work

Display a List in TextView

I'm making a fake command-line system for a fun app, and I want to show the input and output in the same TextView, like this:
>something
>something else
>even more stuff
>etcetera.
I already figured out how to store the text from the EditText into a string and add \n and >, but I can't use strings for the whole thing: to avoid clogging up RAM, I'd like to delete lines after, say 50? I figured that would be much easier to do using Lists.
However, this doesn't work:
log.setText((CharSequence) logText);
But what will?
This method :
http://developer.android.com/reference/android/text/TextUtils.html#join(java.lang.CharSequence, java.lang.Iterable)
return a string composed of each element (either cast as a string or the toString value is used) separated by the delimiter in between each element. You can therefore easily concat all your items in one String.
You can also use http://developer.android.com/reference/java/util/AbstractList.html#subList(int, int)
to limit the count of items in said list.
From your question I assume logText is a List of some sort, therefore you can call
log.setText(TextUtils.join("\n>", logText.subList(0, 50));
Maybe you can put all your strings in a list, an each time you add one, recreate a single string from the list which contains all your items, and affect it to your textview.
You could use a ListView without a separator and populate it using an ArrayAdapter.
That way you wouldn't have to worry about memory, and the user could easily scroll through previous commands.

Search Suggestion results displayed as blank/no text

I have included a Search Dialog in my Activity which works fine. However adding Search Suggestions gives me a little problem: The search suggestion entries are "empty".
I can see my content provider gets called (query(..)) and I return a MatrixCursor with several rows. The suggestions list also shows with (clickable) entries -- but are all blank. Blank as if the string I returned for SUGGEST_COLUMN_TEXT_1 and SUGGEST_COLUMN_TEXT_2 where an empty string.
The columns I use in the MatrixCursor are:
String[] columnNames = {"_ID", "SUGGEST_COLUMN_TEXT_1", "SUGGEST_COLUMN_TEXT_2", "SUGGEST_COLUMN_INTENT_EXTRA_DATA"};
I did try with just the _ID and SUGGEST_COLUMN_TEXT_1 column but same result.
EDIT: And I tried returning a simple "test" string as SUGGEST_COLUMN_TEXT_1 instead of something from my data.
I'm note quite sure what code is relevant here, so please ask for whatever may be needed to figure this out.
I have no idea for where to look for this bug, and my Google-Fu has failed me.
Thanks
(I would like to have added an 'android-search-suggestion' tag, but I'm newguy so it seems I cant)
(Thank you, Jcwenger for teaching the new guy :-)
The solution, from my comment above:
Found it. Use SearchManager.SUGGEST_COLUMN_TEXT_1 instead of "SUGGEST_COLUMN_TEXT_1".. (Same for the rest).The String SearchManager.SUGGEST_COLUMN_TEXT_1 maps to "suggest_text_1": http://developer.android.com/reference/android/app/SearchManager.html#SUGGEST_COLUMN_TEXT_1

Android: Removing "The" from listview items

I have received several requests to adjust the way list items are displayed on my media player application.
Currently any entry beginning with "The" is sorted with the T's as you would expect. Many users would like to sort the item using the second word in the title.
Can someone shed some light on where this would be done? Would it be adjusted while getting the cursor or when applying to listview?
Any help would be greatly appreciated.
Thank You,
Josh
UPDATE:
Thank you all for the answers, but I am still struggling to find apply the solutions suggested in my instance.
I am using a simple cursor to acquire the dataset. Snippet of the code it below:
audioCursor = getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, cols,
null, null,AudioColumns.ARTIST + " COLLATE LOCALIZED ASC");
startManagingCursor(audioCursor);
setListAdapter(new MyABCAdapter(this, R.layout.list_item,
audioCursor, new String[]{AudioColumns.ARTIST,MediaStore.Audio.Artists.NUMBER_OF_TRACKS},
new int[]{android.R.id.text1, android.R.id.text2}));
I also have a utility that successfully adjusts the entry text, changing "The Artist" to "Artist, The", but it is not applied until bindView and the list has already been sorted:
private String fixFileName(String fileName) {
...
if (fileName.startsWith("The")){
fileName = fileName.replace("The ", "");
fileName = fileName.concat(",The");
}
return fileName;
}
My questions are:
1) Is there any way to apply fixFileName to the Cursor before the list is created?
2) If that is not possible, is there a way to resort the listview after bindview?
Thanks!
Have your list items extend Comparable. In the compareTo(..) method, write the custom compare code, likely using a "the".equalsIgnoreCase(fileName.subString(0, 3)), to make "The..." a special case.
Are you using an ArrayList<String> for the titles? One way I can think of is to implement your own Comparable interface. That would include, inside the method compareTo(...), remove any word like "The, "A", etc. and return the comparison of the rest of the String.
When you add items into the list do a Collections.sort() with a custom Comparator.
class CustomComp implements Comparator
{
public int compare(Object obj1, Object obj2)
{
...
if(obj1.toString().toLowerCase().startsWith("the"))
sort by second word instead
...
}
}

Categories

Resources