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?
Related
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.
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 am using Arraylist < HashMap< String, String >> in ListView to archive multi-column(I just need two column, so I use HashMap). But when I am using remove method in context menu. It would always remove the last item in the list.
The code:
#Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case R.id.bc_contextmenu_delete:
list.remove(info.position);
adapter.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}
What should I do to solve this problem to remove the selected one from the list?
Besides, I would also like to get those two values from the HashMap which in the ArrayList. Which code should I use here.
Here is an ArrayList:
PS4 4000<br>
PS5 5000<br>
XBOX 6000<br>
I would like to get PS4 and 4000.
Thanks all.
As per your requirement, you can create a bean for same. i.e. DummyBean. it has two field like
class DummyBean{
String name;
String val;
--getter setter method
}
Use it into List<DummyBean>.
In future if new column added than it is easy to add and expandable.
No need to wrap the HashMap into an ArrayList. HashMap itself is enough. If you want to remain the order, you should use LinkedHashMap.
A side effect is that you cannot access elements by index, so you have to iterate over it to get the last item or the one by index.
So if you don't care about duplicates I would use ArrayList with as template a Pair or a custom Object. (Where I prefer a custom object to be more readable)
ArrayList<Pair<String,String>> consoles = new ArrayList<Pair<String,int>>();
consoles.Add(Pair.create("PS4", 4000));
consoles.Add(Pair.create("PS5 ", 5000));
consoles.Add(Pair.create("XBOX ", 6000));
And remove using index:
consoles.Remove(index);
To store and retrieve your values from the Hashmap in ArrayList, You need to store the the HashMap values with keys to identify them
As with your example ,
PS4 4000
PS5 5000
XBOX 6000
ArrayList<HashMap<String ,String>> list = new ArrayList<>();
// to add item
HashMap<String ,String> val = new HashMap<>();
val.put("GAME", "PS4");
val.put("NUMBER", "4000");
list.add(val); //added to 0th index position
val = new HashMap<>();
val.put("GAME", "PS5");
val.put("NUMBER", "5000");
list.add(val); //added to 1st
// to retrieve ps4 and 400
String forPS4 = list.get(0).get("GAME");
String for4000 = list.get(0).get("4000");
Hi I am just looking for some advice on how to approach adding 2 seperate edittext values to a hash map.
Basically, I have the name of an item in one edittext, and the price of the item in the next one.
I want to click add and for both values to be stored together.
My aim is to add these details to a spinner and when its selected I can show the value that is associated with it.
I am just looking for some direction if that is possible, I am able to add one hash map entry to a spinner but I am having trouble adding two connected values at once
Why not creating a new class Item in which you put the two attributes the name of the item and it price, and then create a hash map of this class.
Your class should look like this:
public class Item{
private String itemName;
private String itemPrice;//or put an integer instead.
public Item (String n, String p){
this.itemName = n;
this.itemPrice =p;
}
EditText itemNameInput, itemPriceInput;
HashMap<Integer, String> inputMap = new HashMap<>();
public void onAddClicked() {
String itemName = itemNameInput.getText().toString();
String itemPrice = itemPriceInput.getText().toString();
inputMap.put(itemNameInput.getId(), itemName);
inputMap.put(itemPriceInput.getId(), itemPrice);
// iterate over hashmap -> arrayadapter backing spinner
}
But why do you need a hashmap? You want to be able to lookup the EditTexts after the spinner is clicked? You're probably better off just concatenating the name and price (String spinnerItemOne = itemName + " " + itemPrice;) then feeding that to your spinner's array adapter. You can use the hashmap if you need, for instance, to send focus back to the itemNameInput. I just used the id since I don't know more about what you're trying to do: you can use the actual edit texts as keys as well (just don't leak them). On a final note, that it matters to much if you are only doing this pair, but you might use a SparseArray instead of the hashmap for more efficiency.
I have a Spinner object that contains an array of strings populated with the 50 states in the United States.
I also have some more arrays that contain lists of cities for the different states. What I am wanting to do is to populate the dependent spinner with the array of cities from the state that was selected by the other spinner.
For exampe:
First spinner selected - Alaska
Second spinner - Anchorage, Fairbanks, Ketchikan, Kodiak.
Another example:
First spinner selected - Florida
Second spinner - Key West, Tallahassee.
The problem is that I'm not sure how to populate the dependent spinner based on the state spinner selected.
The Array Adapter that I am using accepts an integer value that references the array that is used to populate the spinner.
I don't know the exact API calls you're using, but here's the general approach I'd use:
class CitySpinnerFactory {
Map<String,Spinner> stateToCitySpinner = new HashMap<String,Spinner>();
public void map(String state, Spinner citiesForState) {
stateToCitySpinner.put(state,citiesForState);
}
public Spinner spinnerForState(String state) {
return stateToCitySpinner.get(state);
}
}
And later you might do this to populate the cities and states
Spinner states = new Spinner();
Spinner cities = null;
CitySpinnerFactory cityFactory = new CitySpinnerFactory();
states.add("Alabama");
cities = new Spinner();
cities.add("Birmingham"); // city in AL
cities.add("Mobile"); // city in AL
cityFactory.map("Alabama",cities);
states.add("Alaska");
cities = new Spinner();
cities.add("Juneau"); // city in AK
cities.add("Anchorage");
cityFactory.map("Alaska",cities);
// ... add other states
And later you might do this to get the cities out
String state = states.spin(); // making up method to get a random state...
Spinner cities = cityFactory.spinnerForState(state);
String city = cities.spin();