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.
Related
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);
}
});
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.
I'm new for android.
Now I'm using hashmap to show my listview
Below is my SimpleAdapter,
private SimpleAdapter createSimpleAdapter() {
List<Map<String, String>> data = this.createData();
return new SimpleAdapter(this, data, R.layout.menuui,
new String[] {
"name", "food"
}, new int[] {
R.id.membername, R.id.food
});
}
I want to get only one row but if I use listview.getItemAtPosition(position).toString() just like this {food=Onigiri, name=Hashimoto Nanami}
How can I get only one row ?
If there's not enough information to let you know what I ask, please tell me, thanks.
I come from an action script back ground and i am baffled by how to use arrays in java.
In my main activity i created an empty array called mIcons like so
private Array mIcons;
Now i want to set the value of that array by using a method from my DataBaseHelper class which looks like this:
public Array getHomeScreenIcons() {
Array iconList;
// Select All Query
String selectQuery = "SELECT * FROM " + homeIcons;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
iconList.push(Integer.parseInt(cursor.getString(0)));
} while (cursor.moveToNext());
}
Log.v(TAG, "List Created");
// return contact list
}
that bold line jumping out of the code is the problem so far.. how do i PUSH to my array
Then later i will want to run a for loop for the array from my main activity using.length
Use an ArrayList paramaterized to any type you want
ArrayList<Integer> iconList = new ArrayList<Integer>();
add with
iconList.add(Integer.parseInt(cursor.getString(0)));
Iterate with
for (int i: iconList)
{
// i is your entire array starting at index 0
}
or
for (int i=0; i< iconList.size(), i++)
{
}
You're probably thinking about ArrayList
private ArrayList<String> iconList = ArrayList<String>();
iconList.add("Stuff");
and then later to loop through
for (int i=0; i<iconList.size(); i++) {
String newStuff = iconList.get(i);
}
You should probably hit up some basic java tutorials to get used to the array syntax and functionality. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html could be a good starting point.
Looking at your specific problem -
You generally don't want to use the Array class in the manner that you do. It's more of a helper class. Also, it seems that you are going for stack semantics, and you'd likely want to use a Stack instead.
First, arrays:
you declare an array like so:
Type[] myArray = new Type[arraySize];
and then you access it with index like so:
Type myThingFromArray = myArray[myArrayIndex];
and you put things in it like so:
myArray[myTargetIndex] = myObjectOfTypeType;
Raw arrays in java have static size and are not easily growable. For most applications it would be a better idea to use a member of the Collections framework instead. If you're actively looking for a stack (as you mention pushing) then you could use Stack<Integer> and have all the regular stack operations.
Another benefit of using a modern collection class is that you can iterate through your collection using the for-each construct, which eliminates some regular for boilerplate. An example:
public ArrayList<Integer> iconList;
public Array getHomeScreenIcons() {
Array iconList = new ArrayList<Integer>();
// Select All Query
String selectQuery = "SELECT * FROM " + homeIcons;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
iconList.add(Integer.parseInt(cursor.getString(0)));
} while (cursor.moveToNext());
}
Log.v(TAG, "List Created");
// return contact list
//Iterate like so:
for (Integer i : iconList){
System.out.println("Here's all integers in the icon-list: " + i);
}
}
You can define arrays in Java like this:
int[] intArray = new int[3]; // array for three ints
String[] stringArray = new String[10]; // array for 10 Strings
So for your code, you can do something like this:
if (cursor.moveToFirst()) {
int[] iconList = new int[cursor.getCount()];
int index = 0;
do {
iconList[index] = Integer.parseInt(cursor.getString(0));
index++;
} while (cursor.moveToNext());
}
After that you can loop over the array like this:
for (int icon : iconList) {
// Do something with icon
}
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 !