Android:ArrayList storing weird values - java

I just don't understand what's going on here.
ArrayList<ListItem> list=new ArrayList<ListItem>();
ListItem curItem=new ListItem();
String[] contents=cwFile.list();
for(int i=0;i<contents.length;i++){
curItem.itemName = contents[i];
if(new File(cwd+contents[i]).isDirectory()){
curItem.isDir=true;
}
list.add(i,curItem);
}
Log.i("Main",list.get(0).itemName);
Log.i("Main",contents[0]);
So in this code snippet, I get the contents of a directory using the File.list() method
Then, I store the names in an ArrayList of ListItem objects, where ListItem is a self-created class.
But, ListItem is just a class that stores the string
class ListItem {
protected String itemName ="";
protected boolean isDir=false;
protected Double size=0.0;
}
However, after logging the first elements of both the array and the ArrayList (last 2 lines of the first code snippet), I get different results!
This is the log output:
03-15 20:29:46.427 465-465/com.harshal.filer I/filer: .userReturn
03-15 20:29:46.427 465-465/com.harshal.filer I/filer: Android
The second output,"Android" is an actual directory on the device.
But what's ".userReturn" and where did it come from???

Change your code to the following:
ArrayList<ListItem> list=new ArrayList<ListItem>();
String[] contents=cwFile.list();
for(int i=0;i<contents.length;i++){
ListItem curItem=new ListItem();
curItem.itemName = contents[i];
if(new File(cwd+contents[i]).isDirectory()){
curItem.isDir=true;
}
list.add(i,curItem);
}
Log.i("Main",list.get(0).itemName);
Log.i("Main",contents[0]);
You see I moved
ListItem curItem=new ListItem();
into the for-loop. If it's outside the reference of the item at position 0 in the list will always point to the latest entry in your array, thus the weird result.

Related

Append listview with the same variable but different value each time

I have variable a in a loop, it gives a different string each time
I want to add that string in ListView, without overwriting it, i searched for append ListView, but didn't find anything, i always got the last item which is the last a value in the ListView
I think I need to use HashMap or foreach, but i don't know how
in the image, each line is string a variable, so the variable will change every time
158 word
here's my code
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,list);
list.add(a);
simpleList.setAdapter(adapter);
UPDATE:
code
a variable comes from console.log in javascript
siteView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onConsoleMessage(ConsoleMessage consoleMessag) {
String imgDataUr= consoleMessag.message();
if (imgDataUr.contains("heyyes")) {
String a=imgDataUr.substring(6); //this changed with different value each time
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,list);
Log.d("countries", a);
list.add(a);
simpleList.setAdapter(adapter);
}
return super.onConsoleMessage(consoleMessag);
}
});

Extracting string array from object array

So I have arraylist modelData that populates a recyclerview using sqlite database in some activity.!
Now in my MainActivity I want an string arraylist of names from the modelData !
that's what did so far..
// inside the onCreate of MainActivity
//code ..
db = new DBHandler(this, null, null, 1);
modelData = new ArrayList<AzkarModel>();
modelData = db.getDataFromDB();
for (AzkarModel o : modelData) {
ArrayList<String> names = new ArrayList<>();
names.add(o.getName());
}
for (int i = 0; i< modelData.size();i++){
names.add(modelData.get(i).getName());
Log.i("The List Log", names);
}
Two problems
1) [FIXED] The names arraylist is showing the same element twice at first and end
I/ The List Log: [Mike, John, Sam, Nora, Mike]
2) The arraylist names doesn't get updated..! when I add/edit/delete from the recycler and go back to the MainActivity I don't see the new changes unless I close the app then open it again..! I can't use notifyDataSetChanged since there's no adapter here.!
Once you have an array list object, populate it inside a loop
ArrayList<String> modelNames = new ArrayList<>();
for (AzkarModel model : modelData) {
modelNames.add(model.getName());
}
Now you have an array list with model names inside. Concerning the second question, please use an recyclerview adapter here you can find a nice tutorial.

Arreylist<Object> show as per section in viewpager list

I have an ArrayList<Model> mList
and a Model class that looks like
public class Model{
String value;
String name;
String dob;
//getters & setters
}
I have view pager which has 5 fragments. mList holds the full list, ie value = All. which I am showing in the 1st fragment. I want to filter/sort for the other fragments. For instance, 2nd should have only value = k ,for 3rd value = B... what is the best way to get a sorted list and pass list.. for now I am doing
ArrayList<Model> newlist = new ArrayList<Model>();
for{int i =0 ;i> mlist.size();i++}{
if(mList.getvalue.equal("k")){
newList.add(mList(i))
}
}
and passing newlist to listview
but now how can I update the value in mList when the user clicks on one row because that row will have the value newList position...
Basically I have some condition for mList say user have to click one item for k,b etc that I want to check.
1) What is a good way to get sorted/filter list in different fragments?
2) How can I update the newlist value of the item clicked in mList?

Use of an ArrayList of HashMaps

Could someone tell me what is wrong with the code below? When I run the code below the first line after the try { produces the following error :
"java.lang.IndexOutOfBoundsException: Index: 0, Size: 0"
public static class ASIFFile {
private ArrayList<HashMap<String,String>> data;
private static int currRec = 0; //assign each record a numeric id based on this figure.
// Method for reading ADIFfile
public ArrayList<HashMap<String,String>> ReadASIFfile (File DataFile) {
data = new ArrayList<HashMap<String, String>>(500);
try {
HashMap<String, String> temp = new HashMap<String,String>(10);
data.set(currRec, temp);
(data.get(currRec)).put("recID", Integer.toString(currRec));//give the record a numeric ID
...
You never add anything to your ArrayList. You have an ArrayList that can hold HashMaps, but is currently empty.You have to add a new HashMap to the index you want to use before you use that index.
As Kevin mentioned, you never put anything in your ArrayList. Your code should look something like this...
data = new ArrayList<HashMap<String, String>>(500);
try {
//give the record a numeric ID
data.add(currRec, temp);
In your previous solution, you're calling data.set(currRec, temp); which assumes there is a HashMap element in position currRec of your ArrayList. If you haven't put anything in the ArrayList yet, there will be no element currRec.

Passing variable string to create arrays (Android)

I am a newbie to Android and Java and want to write a function that will display a list based on a variable that I pass to the function.
The function is below and the code below creates an array out of a string called type, but what I want to do is pass it a variable string and have it build a list based on that string.
So if I wanted the type list I would say list_it("type")
But if I try something like getResources().getStringArray(R.array.thelist); it doesn't work.
Can someone point me in the right direction?
public void list_it(String thelist){
String[] types = getResources().getStringArray(R.array.type);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this, R.layout.list_item1, types);
setListAdapter(mAdapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
Use the following code to get the identifier for the given name i.e thelist :
int resID = getResources().getIdentifier( thelist, "string", "<package name>" );
This will return you the identifier for the given resource name. Then use the
getResources().getStringArray( resID );
HTH !

Categories

Resources