Add data from textview in an array list - android

I am trying to add the data of textview into array. myLogClass is a array list and The codes is as follows:
String txt_datetime = txt_date.getText().toString();;
String txt_messageData = txt_message.getText().toString();
String txt_day = this.dayName;
List< myLogClass > results = null;
results = new ArrayList< myLogClass >();
results.add( new myLogClass( txt_datetime, txt_messageData, txt_day ) );
I have setter and getter method in myLogClass and has constructor with 3 variable which is as follows:
public diaryLogs(int dateTime, String messagetxt, String dayN){
setDayCode(dateTime);
setDateTime(messagetxt);
setDairyText(dayN);
}//end constructor.
While i tried to add method it says the constructor is undefined.
Thanks for your help in advance

To be able to add a diaryLogs instance to the result array, the ArrayList should be of diaryLogs. Here I made the modification before going to the other issue.
The definition of the diaryLogs constructor is (int dateTime, String messagetxt, String dayN), that being said, that function is expecting an int, and then two strings as parameters.
To get this working you need to convert the string txt_datetime to int before passing it to the constructor.
String txt_datetime = txt_date.getText().toString();;
String txt_messageData = txt_message.getText().toString();
String txt_day = this.dayName;
// Convert to Integer
int txt_datetime_int = Integer.parseInt(txt_datetime);
// Define the ArrayList as diaryLogs
List< diaryLogs > results = null;
results = new ArrayList< diaryLogs >();
// Pass as Integer
results.add( new diaryLogs( txt_datetime_int , txt_messageData, txt_day )

In this line:
results.add( new diaryLogs( txt_datetime, txt_messageData, txt_day ) );
txt_datetime is a String while Integer is expected. This is why you get this message.

Related

How can i put a list to array

How can i put a list of strings, type string
in
String countryList[] = {a};
a is the variable that have the list
so, it will be
String countryList[] = {"heyyesGabon","heyyesGibraltar","heyyesGuinea"}; //and the others
this list comes from console.log in javascript
it always save the last item
From List to String array
String [] countryArray = countryList.toArray(new String[0]);
From String array to List
List<String> list = Arrays.asList(array);
You can use in-build function list.toArray(stringArray); it will return String[].
String countryList[] = new String[list.size()];
countryList = list.toArray(countryList);
Hope this will help you.

How to convert a string variable to a string array

I am making a request from my app to a server which is returning an array. I want to put that array into a listView but the the whole array is treated as a single listItem.
As the response is recieved in a string variable i want to convert it to a string array?
any help is appretiated.. Thanks
Just replace arr with your String Array variable name:
StringBuilder builder = new StringBuilder();
for(String s : arr) {
builder.append(s);
}
String str = builder.toString();
Firstly string response data split by space[Available special character] or assign string array.
String yourdata = "A B C D E F G H";
String[] yourtDataArr = yourdata.split(" ");
ArrayList<String> list = new ArrayList<String>();
for(i =0;i<yourtDataArr.length;i++){
list.add(yourtDataArr[i]);
}
Then use your list on ListView. You can see following example.
http://www.vogella.com/tutorials/AndroidListView/article.html
https://www.tutorialspoint.com/android/android_list_view.htm
http://windrealm.org/tutorials/android/android-listview.php

How to get the Index of an element from an array using Arrays.asList

I got an arrray like this in android the usage of Arrays.asList is different because i made the Array with different class "Person":
people = new Person[]{
new Person("Python"),
new Person("PHP"),
new Person("Pascol"),
new Person("PyCrust"),
new Person("C"),
new Person("C#"),
new Person("C++")
};
and i used Arrays.asList in this way
int index= Arrays.asList(people).indexOf("Pascol");
String tags = Integer.toString(index);
Toast.makeText(getApplication(),tags,Toast.LENGTH_SHORT).show();
But i get the value "-1"in the toast.
i could not find the fault.
The problem is that you have a list of Person objects. When you call .indexOf("Pascol"); you pass in a String. Comparing a Person with a String will return always false.Do this instead
int index = -1;
for (int i = 0; i < people.length; i++) {
if (people[i].getName().equals("Pascol")) {
index = i;
}
}
String tags = Integer.toString(index);
Toast.makeText(getApplication(),tags,Toast.LENGTH_SHORT).show();
int index = Arrays.asList(people).indexOf("Pascol");
Pascol is a String here but objects in your array are Person type objects. So indexOf method can not match a String with a Person. You need to override equals() and hashcode() and pass a Person type parameter to the indexOf which name is Pascol. Of course I assume that equality of your object depends on only name attribute.
You should implement Comparable<> in Person class

how to get first value of arraylist and pas as parameter

how to pass ArrayList index 0 value as parameter to another screen help me please
int counter=Category_name.size();
if(counter==1)
{
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Menu_ID = new ArrayList<String>();
Intent iMenuList = new Intent(MenuGroup.this, thirdstep.class);
String s = Menu_ID[index0];
String t = Category_name[index0];
iMenuList.putExtra("Menu_ID",s);
iMenuList.putExtra("menu_group", t);
startActivity(iMenuList);
}
String s = Menu_ID.get(0);
String t = Category_name.get(0);
EDIT
You cant add a single value on any index but 0, here is a simple example
ArrayList<String> Category_name = new ArrayList<String>();
Category_name.add(5, "string");
console
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0
at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
I believe that some null values are getting added in the list as you are reading from the JSON and you want to take the first non null values.
Solutions:
1. When you parse the JSON add only the non null values to the list and then use list.get(0).
Or
2. Once the list is formed, iterate through the list. Check for null condition, pick the first non null value and put in your string and break the loop.

Android: Storing int and String values in one multidimensional array

is it possible to store different values into a multidimensional array such as int's and String's?
String[][] mainArray= new String[2][2];
mainArray[0][0] = 1;
mainArray[0][1] = "Name1";
mainArray[1][0] = 2;
mainArray[1][1] = "Name2";
this obviously doesn't work because 1 and 2 are not String values
yes you can store
try this
String[][] mainArray= new String[2][2];
mainArray[0][0] = String.valueOf(1);
mainArray[0][1] = "Name1";
mainArray[1][0] = String.valueOf(2);
mainArray[1][1] = "Name2";
You can create an Object array, and save Integers, which is the boxing of the primitive int.
Object[][] arr = new Object[2][2];
arr[0][0] = "hello";
arr[0][1] = Integer.valueOf(1);
arr[1][0] = Integer.valueOf(2);
arr[1][1] = "world";
Here is the solution
Object[][] arr=new Object[anysize][]; and you can do like this
arr[0][0]=1;
arr[1][0]="hello";
But while accessing this array you should also do this using Object only.Else there may be ClassCastException.

Categories

Resources