Multiple Strings in a Single TextView - android

I have two List<String> LIST A and LIST B.Each List<String> have 10 lines. I need to display these Strings in TextView. But it should be like line by line.
Example:
LIST A - line 1 ( display first line of LIST A)
LIST B - line 1 ( display first line of LIST B)
LIST A - line 2 ( display 2nd line of LIST A)
LIST B - line 2 ( display 2nd line of LIST B)
LIST A - line 3 ( display 3rd line of LIST A)
LIST B - line 3 ( display 3rd line of LIST B)
etc....etc....
Please suggest me a solution.

Just iterate your lists and use textview.append("yourline"); as per your convenient.
int listSize=10;
List<String> A = new ArrayList<String>(listSize),B = new ArrayList<String>(listSize);
for(int i=0;i<listSize;i++){
textview.append(A.get(i)+"\n"+B.get(i)+"\n");
}

I merged the both list and displayed the items in textview by using HTML and it worked

Just try with this example below. This is the answer with the output you want.
public class test extends AppCompatActivity {
public ArrayList<String> alldata;
TextView test;
String str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
test = (TextView) findViewById(R.id.test);
List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");
List<String> stockList2 = new ArrayList<String>();
stockList.add("stock3");
stockList.add("stock4");
stockList.addAll(stockList2);
String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);
alldata = new ArrayList<String>();
for (int i = 0; i < stockArr.length; i++) {
str = stockArr[i].substring(0, stockArr[i].length()) + "<br>";
alldata.add(str);
str = "";
}
StringBuilder PostItems = new StringBuilder();
for (int i = 0; i < alldata.size(); i++) {
PostItems.append(alldata.get(i));
}
String StrigItem = String.valueOf(PostItems);
test.setText(Html.fromHtml(StrigItem));
}
}

Related

get value from Dynamically add multiple spinner in android?

How to get value to dynamically add multiple spinner where spinner id is same.i used 'String spin = parent.getSelectedItem().toString();' but i get all time last spinner value.Plz help me?
you can create array list of spinner
ArrayList<Spinner> listSpinner = new ArrayList<>();
listSpinner.add(spinner1);
listSpinner.add(spinner2);
listSpinner.add(spinner3);
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem();
}
get different value for all spinner out side for loop and store it in arraylist.
ArrayList<Spinner> listSpinner = new ArrayList<>();
ArrayList<ArrayList<String>> s‌​tatus_list = new ArrayList<>();
status_data.add("" + snapshot.getValue());// getting data
s‌​tatus_list.add(status_data);
for (int i = 0; i < 10; i++) {
View v = LayoutInflater.from(AssetCodingDetails.this).inflate(R.layou‌​t.custom_asset_codin‌​g_label, null);
Spinner spinner_status = (Spinner) v.findViewById(R.id.spinner_status);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.spinner_item, s‌​tatus_list.get(i));
spinner_status.setAdapter(adapter);
listSpinner.add(spinner_status);
}
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem().toString();
}

how to shuffle a letters of array in android?

String arrAnimal[] = {"cat","dog","parrot","fish"};
I need to shuffle each word and set it to TextView, when click button wanna go through each element(see Shuffeled elements).
Complete working code,
String[] animals = {"cat","dog","parrot","fish"};
for (int i = 0; i < animals.length; ++i) {
List<Character> letters = new ArrayList<>(animals[i].length());
for (char c : animals[i].toCharArray()) {
letters.add(c);
}
Collections.shuffle(letters);
StringBuilder builder = new StringBuilder();
for (char c : letters) {
builder.append(c);
}
animals[i] = builder.toString();
}
System.out.println(Arrays.toString(animals));
Outputs: [tca, ogd, roptar, sifh]
EDIT: To print line by line, change the last line to,
for (String s : animals) {
System.out.println(s);
}
Use Collections.shuffle( )
ArrayList<String> list = new ArrayList<String>();
list.add("cat");
list.add("dog");
list.add("parrot");
list.add("fish");
Collections.shuffle(list);

Using a variable to switch to a certain spinner item

I have:
a String array with an unknown length that's populated with unknown items (let's say fish, bird, cat)
an ArrayAdapter and a Spinner that displays the items
a variable that contains one unknown item from the string array (let's say cat)
I want to set the Spinner to the value from the variable (cat). What's the most elegant solution? I thought about running the string through a loop and comparing the items with the variable (until I hit cat in this example), then use that iteration's # to set the selection of the Spinner, but that seems very convoluted.
Or should I just ditch the Spinner? I looked around and found a solution that uses a button and dialog field: https://stackoverflow.com/a/5790662/1928813
//EDIT: My current code. I want to use "cow" without having to go through the loop, if possible!
final Spinner bSpinner = (Spinner) findViewById(R.id.spinner1);
String[] animals = new String[] { "cat", "bird", "cow", "dog" };
String animal = "cow";
int spinnerpos;
final ArrayAdapter<String> animaladapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, animals);
animaladapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bSpinner.setAdapter(animaladapter);
for (Integer j = 0; j < animals.length; j++) {
if (animals[j].equals(animal)) {
spinnerpos = j;
bSpinner.setSelection(spinnerpos);
} else {
};
}
(Temporarily) convert your String array to a List so you can use indexOf.
int position = Arrays.asList(array).indexOf(randomVariable);
spinner.setSelection(position);
EDIT:
I understand your problem now. If your String array contains all unique values, you can put them in a HashMap for O(1) retrieval:
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < animals.length; i++) {
map.put(animals[i], i);
}
String randomAnimal = "cow";
Integer position = map.get(randomAnimal);
if (position != null) bSpinner.setSelection(position);

ArrayList in ArrayList<String>

ArrayList<ArrayList<String>> mainlist = new ArrayList<ArrayList<String>>();
ArrayList<String> childlist = new ArrayList<String>();
for (int i = 0; i < 3 i++){
String chlidtext = chlidlist + String.valueof(i);
String maintext = mainlist +String.Valueof(i);
childlist.add(chlidtext);
mainlist.add(maintext);
}
return mainlist;
i dont have an idea how to get 3rd element in every chlidlist ?
You can access 3rd element of childList by:
mainList.get(index).get(2); // Here 1st get() will get the 1st element of mainList which conatins 1st child element and 2nd get() will get the 3rd element of childlist.

sort array list from second item android

i have a array list for using it in spinner ,i have first value i spinner as title and i want to sort array list from second item in the spinner but i dont know how to do this i am using below trick but it sort whole array list including first item which is title so how to statr sorting from second item...
my code is below...
// this is my title ie. "provincia"
String select2= "Provincia";
if(!estado1.contains(select2)){
estado1.add(select2);
}
for (int i = 0; i < sitesList1.getEstado().size(); i++)
{
if(!estado1.contains(sitesList1.getEstado().get(i)))
{
estado1.add(sitesList1.getEstado().get(i));
Collections.sort(estado1);
}
use below code for show it in spinner...
final ArrayList<String> estado1 = MainMenu.barrio1;
final Spinner estado11 = (Spinner) findViewById(R.id.Spinner04);
ArrayAdapter<String> adapterbarrio = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, estado1)
estado11.setAdapter(adapterbarrio);
Why not remove the title / only add the title after the list has been sorted?
How about this
List<String> list = new ArrayList<String>();
// Fill list
String title = list.get(0);
list.remove(0);
Collections.sort(list);
list.add(0, title);
One way would be to split the arraylist like
estado1.subList(1,estado1.size()-1);
This would return a sublist excluding your title.
Use bubble sort! and start at index = 1!
final ArrayList<String> estado1;
for(int i=1; i<estado1.size() ; i++) {
for(int c=i; c<estado.size() ; c++) {
if(estado1.get(i).compareTo(estado1.get(c)))
{
String temp = estado1.get(i);
estado1.remove(i);
estado1.add(i, estado1.get(c));
estado1.remove(c);
estado1.add(c, temp);
}
}
}
PS: very bad performance

Categories

Resources