I have an arraylist and I want to update specific item.I am adding data to list with this line:
randomsList.add(new Random(result.get(i).getAsJsonObject(),0));
This is adding datas to 0,1,2,3,4... locations so when I try to update an item I don't know which object is where.
I am updating data with this line:
randomsList.set(position,new Random(user,1));
I think if I use the custom numbers for location I can update specific item.My prototype:
randomsList.add({USER_ID},new Random(result.get(i).getAsJsonObject(),0));
And if I want to update it then I use this line:
randomsList.set({USER_ID},new Random(user,1));
Is this a good approach ? If your answer is no,how should be ?
P.S. : I am using this arraylist with an adapter
As #itachiuchiha mentions, you should use a Map. The "custom numbers" you mention are your key (integers), and the value is the Random object.
As an aside, in response to your comment, below is an example of an Android Adapter that uses a Map as the underlying datasource.
public class RandomsAdapter extends BaseAdapter {
private Map<Integer, Random> randoms;
public RandomsAdapter(Map<Integer, Random> randoms) {
this.randoms = randoms;
}
public void updateRandoms(Map<Integer, Random> randoms) {
this.randoms = randoms;
notifyDataSetChanged();
}
#Override
public int getCount() {
return randoms.size();
}
#Override
public Random getItem(int position) {
return randoms.get(position);
}
#Override
public long getItemId(int position) {
// we won't support stable IDs for this example
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = createNewView();
}
update(view, songs.get(position));
return view;
}
private View createNewView() {
// TODO: create a new instance of your view and return it
return null;
}
private void update(View view, Random random) {
// TODO: update the rest of the view
}
}
Note the updateRandoms(Map<Integer, Random> randoms) method.
While you could expose a method in the adapter to update a single entry in the Map, it shouldn't be the responsibility of the Adapter to handle modifications to the map. I prefer passing the entire map again - it could still be a reference to the same object, but the Adapter doesn't know or care; it just knows: "my underlying datasource has been changed/modified, I need to tell my observers that they should refresh their views by calling notifyDataSetChanged()".
Alternatively, you could call notifyDataSetChanged() on the adapter externally when you modify the underlying Map (this tells the ListView that its data is out of date and to request its views from the adapter again).
Related
Apologies if this is poorly explained, I am having difficulty in understanding it myself. If you point out anything you don't understand I will do my best to correct any issues. Okay so here we go.
Several classes. (D&D sheet, sheet has weapons user can equip, this is about equipping said weapons which is stored in a list)
A fragment activity - CombatFragment
The arrayadapter list which is declared in CombatFragment -
AttackListViewContentAdapter
The realm object - Weapon
The realm object where a list of Weapon is held - Sheet
A number of XML files (The code of which I won't paste here as SO has a limit on code. content_combat, attack_list_item
What I've gathered so far is that when I create a new attackListViewContentAdapter it loops at a rapid and continued pace. So much so that the screen does not respond to me touching any of the widgets. I've done things like log a number each time it passes so it shows when it's doing it again and again. If you need information on that I can show you where I put the logs and what shows in my Logcat when I add an additional view (row).
I believe that it's something to do with the onChangedListener which keeps being triggered, even if I found the reason why how do I then get to a stage where I can create a new view and have the listener so it can record changes.
Please note in the interests of space I will be using abbreviated code. I've ignored things like dialog boxes and widgets which aren't relevant. So if it seems like something missing or you need to view the classes, it's possibly in the file which I've linked above each one.
CombatFragment
public class CombatFragment extends Fragment {
#BindView(R.id.lv_attack_spellcasting_content)
ListView lv_attack_spellcasting_title;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.content_combat, container, false);
RealmList<Weapon> weaponList = sheet.getWeaponList();
final AttackListViewContentAdapter attackListViewContentAdapter = new AttackListViewContentAdapter(getActivity(), sheet, realm, weaponList);
weaponList.addChangeListener(new RealmChangeListener<RealmList<Weapon>>() {
#Override
public void onChange(RealmList<Weapon> weapons) {
/* Gives the adaptor a kick to know that the weapon realm list has changed */
attackListViewContentAdapter.notifyDataSetChanged();
loopOnChanged++;
}
});
lv_attack_spellcasting_title.setAdapter(attackListViewContentAdapter);
playerInit();
return rootView;
}
// This is a fake method, this is just to show that the .add is in it's own method which is triggered by a button press and not in onCreate
public void buttonPress() {
sheet.getWeaponList().add(realm.createObject(Weapon.class));
}
} `
AttackListViewContentAdapter
public class AttackListViewContentAdapter extends ArrayAdapter<Weapon> {
public AttackListViewContentAdapter(Context context, Sheet sheet, Realm realm, List<Weapon> weaponList) {
super(context, 0, weaponList);
this.sheet = sheet;
this.realm = realm;
}
#Override
#NonNull
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
//Because you're returning the view (AttachToRoot is false) the ArrayAdaptor (This class) will handle adding the view to the list.
convertView =
LayoutInflater.from(getContext()).inflate(R.layout.attack_list_item, parent, false);
return convertView;
}
}
Weapon
public class Weapon extends RealmObject {
#PrimaryKey
int weaponID;
//properties, set get methods etc.
}
Sheet
public class Sheet extends RealmObject {
#PrimaryKey
private int sheetID;
private RealmList<Weapon> weaponList;
public RealmList<Weapon> getWeaponList() {
return weaponList;
}
public void setWeaponList(RealmList<Weapon> weaponList) {
this.weaponList = weaponList;
}
}
content_combat
<ListView
android:id="#+id/lv_attack_spellcasting_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="7"
android:rowCount="1" />
attack_list_item
Nothing really in there to include
Your problem is happening because of bad initialization of your spinner widgets within AttackListViewContentAdapter class.
You must set your spinners selection before setOnItemSelectedListener is set.
You must check whether your spinner selection is not equal to current selection, to avoid an infinite loop between onChange and onItemSelected methods. I mean, your spinners onItemSelected callbacks, execute a realm transanctions, then, those transanctions fire your onChange callback and finally, your onChange callback invokes notifyDataSetChanged() which make cycle start again going into an infinite loop.
To solve your problem, you should follow the next steps inside AttackListViewContentAdapter.java:
A) Remove the following lines from addWeaponToUI() method:
private void addWeaponToUI() {
et_name_value.setText(weapon.getWeaponName());
np_damage_number_of_die_value.setValue(weapon.getWeaponDamageNumberOfDie());
SheetEnum.Ability ability = SheetEnum.Ability.getEnumValue(weapon.getWeaponAbilityBonusInt());
tv_attack_bonus_value.setText(String.valueOf(sheet.getAbilityBonus(ability)));
// REMOVE below lines!
//s_damage_die_type_value.setSelection(weapon.getWeaponDamageDieTypeInt());
//s_damage_type_value.setSelection(weapon.getWeaponDamageTypeInt());
//s_ability_bonus_value.setSelection(weapon.getWeaponAbilityBonusInt());
}
B) Invoke spinner setSelection() before setOnItemSelectedListener(), then check selected item is not equal to selected position to avoid an infinite loop:
ArrayAdapter<CharSequence> damageDieTypeAdapter = ArrayAdapter.createFromResource(getContext(), R.array.die_type, android.R.layout.simple_spinner_dropdown_item);
s_damage_die_type_value.setAdapter(damageDieTypeAdapter);
//Set selection before listener
s_damage_die_type_value.setSelection(weapon.getWeaponDamageDieTypeInt());
s_damage_die_type_value.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, final int position, long id) {
//Check selected position is not equal to current position to avoid an infinite loop
if (position != weapon.getWeaponDamageDieTypeInt()) {
String[] value = getContext().getResources().getStringArray(R.array.die_type);
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
weapon.setWeaponDamageDieType(position);
}
});
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
C) Repeat Step B for s_damage_type_value and s_ability_bonus_value spinners
This question already exists:
categorise listView items into different sections - Android
Closed 6 years ago.
how can we create a Adapter (for ListView) which can take categorized data
and show them in a listView with headers and rows,
by categorized data i meant grouped data e.g.
record = ["US",("California","Alabama","Alaska","Wisconsin")]
the above is a record which holds a country and its states.
i have list of states with their countries and i wanna show it on a listView (sorted by countries) and i want to use country's name as section header of my list
this is what its going to look like :
Country1
state1ofC1
state2ofC1
state3ofC1
Country2
state1ofC2
state2ofC2
state3ofC3
................
a basic Adapter for listView looks like this :
public class NewAdapter extends BaseAdapter {
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
which takes one array of records. but how can i create an Adapter which is compatible with Categorized data so that i can use listView with section headers please guide me
You need to use an ExpandableListView.
I understand how a ViewHolder's onBindViewHolder works, however I'm unclear about how notifyItemRangeChanged(0, this.data.size()); works in this example and what it does exactly.
The data that is supplied to this adapter is in Json format.
The adapter is below:
public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{
private LayoutInflater mLayoutInflater;
//this is an arrayList of questionData objects
private ArrayList<QuestionData> data =new ArrayList<>();
//Created the layoutInflator
public AdapterQuestion(Context context){
//get from context
mLayoutInflater=LayoutInflater.from(context);
}
public void setBloglist(ArrayList<QuestionData> data){
this.data =data;
notifyItemRangeChanged(0, this.data.size());
}
#Override
public ViewQuestion onCreateViewHolder(ViewGroup parent, int viewType) {
//inflates the customQuestion view or converts it to java code
View view= mLayoutInflater.inflate(R.layout.customquestion, null);
//We now want to convert the View into a ViewQuestion, view Question takes
//a view so we pass the view into view question and then return it.
ViewQuestion holder=new ViewQuestion(view);
return holder;
}
//ViewGroup parent and ViewType are not being assigned.
#Override
public void onBindViewHolder(ViewQuestion holder, int position) {
//here we need to bind the data to our view, there is currently no Data!
//We need to get the data from our JSON
//Parameters is a ViewHolder and a Position
//This gives us the current information object from the whole arraylist
//data.get(position) data is the arraylist and we are getting the current position or index;
//That current obj is of Type QuestionData
QuestionData currentObj= data.get(position);
//we are accessing the Inflated view, or saved view with holder
//holder.answerText is the textView in holder. We are then taking that current object
//We are getting the text of the current object and setting it to the AnswerText view
holder.answerText.setText(currentObj.getMtext());
holder.answerId.setText(currentObj.getId());
holder.mVotes.setText(currentObj.getVotes());
holder.mLikeButton.setTag(currentObj);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewQuestion extends RecyclerView.ViewHolder{
//once we create it once the reclycer view will automatically recycle it
private TextView answerText;
private TextView answerId;
private TextView mVotes;
private LikeButton mLikeButton;
public ViewQuestion (View itemView){
super(itemView);
//here we are finding the views by their ID
answerText=(TextView)itemView.findViewById(R.id.answerText);
answerId=(TextView)itemView.findViewById(R.id.answerId);
mVotes=(TextView)itemView.findViewById(R.id.VoteTextView);
mLikeButton= (LikeButton)itemView.findViewById(R.id.heart_buttons);
mLikeButton.setOnLikeListener(new OnLikeListener() {
#Override
public void liked(LikeButton likeButton) {
Voting vote = new Voting();
vote.onUpVote(convertToString(),
getAdapterPosition(),ViewQuestion.this);
System.out.print("Adapter Position"+getAdapterPosition());
}
#Override
public void unLiked(LikeButton likeButton) {
Voting onDown=new Voting();
onDown.onDownVote(convertToString(),
getAdapterPosition(), ViewQuestion.this);
}
});
}
public String getVoteView(){
String voteView=mVotes.getText().toString();
return voteView;
}
public String convertToString(){
String converted=answerId.getText().toString();
return converted;
}
public int convertToInt(){
String converted=answerId.getText().toString();
int ConvertedInt=Integer.parseInt(converted);
return ConvertedInt;
}
}
}
When the data that is to be set in RecyclerView is changed, the Adapter needs to get notified of the data change so that it can change the data in recyclerview.
The method
notifyItemRangedChanged(fromIndex,toIndex);
is used to notify the adapter that some set of data is changed among the whole data and it tells the adapter that adapter should refresh the data and reload it into the recyclerView starting from fromIndex to toIndex as passed into the method .
use this method if you have multiple data changed but not all , those changed data also are in cluster so that you can say from 5th to 10th index data are changed .
If all data are changed call :
notifyDataSetChanged();
if only one dataItem is changed then call :
notifyItemChanged(dataPosition);
Using notifyItemRangeChanged(0, this.data.size()) it’s bad practice.
Best way is using notifyItemChanged or notifyItemRangeChanged with payload.
Payload - optional parameter (key). That give you opportunity to check what kind of update do you need.
public void onBindViewHolder(/*...*/, List payloads) {
if (payloads.isEmpty()) {
setText(holder, position);
downloadBitmap(holder, position);
} else if (payloads.contains(SET_ONLY_TEXT)){
setText(holder, position);
}
}
In this example payloads used for checking when adapter should update only the text.
in your case you are not doing it(notifyItemRangeChanged) right as you might as well can call notifyDataSetChanged(); because you are telling the adapter that the entire list has changed and not specific position.
I have a POJO:
public class RoomData implements Parcelable {
private String objectId, name, building, floor, imageUrl;
private boolean hasPc, hasProjector, hasTelepresence, hasWhiteboard;
private boolean dataReady = false;
private ArrayList<MeetingData> meetingData = new ArrayList<MeetingData>();
private int capacity;
private ArrayList<BeaconData> beacons = new ArrayList<BeaconData>();
private DateTime nextAvailableStart, nextAvailableEnd;
private boolean availableNow = false;
private ArrayList<Interval> meetingDuringIntervals = new ArrayList<Interval>();
}
that is populated into a custom ListView/ListAdapter.
I want to know how I can filter that list by the fields of my POJO. For instance if I construct a RoomFilter object, that states that my capacity integer should be higher than X. How can I implement this? I can only find links to filtering by primitive data types...
Here is my custom Adapter class:
public class BeaconAdapter extends BaseAdapter {
private Context context;
private ArrayList<BLEDeviceAdvertisement> beaconArrayList = new ArrayList<BLEDeviceAdvertisement>();
private static LayoutInflater inflater = null;
//BeaconAdapter for the custom ListView
public BeaconAdapter(Context context, ArrayList<BLEDeviceAdvertisement> beaconArrayList) {
this.beaconArrayList = beaconArrayList;
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return beaconArrayList.size();
}
#Override
public BLEDeviceAdvertisement getItem(int position) {
return beaconArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
//Custom getview for the custom layout beacon_row_item.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View myCustomView = convertView;
if (myCustomView == null)
myCustomView = inflater.inflate(R.layout.beacon_row_item, null);
return myCustomView;
}
}
After OP's request I post my comment as an answer for future reference, giving more details. Here are some possible solutions:
Destructive update: Loop through the list, filtering out what you don't need. Implement adapter based on this list.
Virtual view: Implement logic for a "virtual view" over the original list. Here you keep only the original list, but in all adapter's methods you skip items that don't match the filter. For instance, in getItem(int pos) you loop through the list keeping a counter, but you increment it only if the item matches the filter: when the counter equals pos you return the item. Similar for the other methods.
Concrete View: Use an additional list to keep the "view" over the original list, so to cache the computation done in 2. When the filter is set/updated, a loop through the original list builds the "view" and then you implement the adapter based on the "view". More time efficient, but also more memory consumed.
In the app I've been working on, I have a custom class DeviceListAdapter extending BaseAdapter which gets passed to my ListView. In my DeviceListAdapter class, I keep my own ArrayList<Device> which I use to generate the list's views with View getView(... ). Whenever the app causes a change in the data, I use custom methods in DeviceListAdapter to update the ArrayList<Device> to reflect the changes. I've used the debugger and many print statements to check that the data does get changed how I expect it to, adding and removing Device objects as specified. However, after each change to the data I also call notifyDataSetChanged(), but on the UI none of the elements get updated. In the debugger, I found that after calling notifyDataSetChanged(), the getView(... ) method was not being called, which explains why the ListView wasn't being redrawn. To figure out why, I used the debugger's "step into" function to trace where the program execution went into the android framework since I have the SDK sources downloaded. What I found was very interesting. The path of execution went like this:
DeviceListAdapter.notifyDataSetChanged()
BaseAdapter.notifyDataSetChanged()
DataSetObservable.notifyChanged()
AbsListView.onInvalidated()
Rather calling the onChanged() method, it jumped tracks and executed the onInvalidated() method once it reached AbsListView. Initially I thought this was an error with the debugger perhaps reading the wrong line number, but I restarted my Android Studio as well as totally uninstalled and reinstalled the app, but the result is the same. Can anybody tell me if this is legitimately a problem with Android's framework or if the debugger is unreliable for tracing execution outside of my own project files?
More on my implementation of notifyDataSetChanged()... I created the local method to override BaseAdapter's notifyDataSetChanged() so that I could set a boolean flag mForceRedraw inside of my DeviceListAdapter as to whether I should force redraw my list entries. In the getView(... ) method, I typically check if the second parameter, View convertView is null, if it is then I redraw the view and if not then I pass convertView through and return it. However, when 'mForceRedraw' is true, I never return convertView, I explicitly redraw the view. The problem that arises is caused by my earlier concern, which is that getView() is not called after I execute notifyDataSetChanged().
EDIT: Here's a code snippet of my DeviceListAdapter:
/**
* Serves data about current Device data to the mDeviceListView. Manages the dynamic and
* persistent storage of the configured Devices and constructs views of each individual
* list item for placement in the list.
*/
private class DeviceListAdapter extends BaseAdapter {
private boolean mForceRedraw = false;
/**
* Dynamic array that keeps track of all devices currently being managed.
* This is held in memory and is readily accessible so that system calls
* requesting View updates can be satisfied quickly.
*/
private List<Device> mDeviceEntries;
private Context mContext;
public DeviceListAdapter(Context context) {
this.mContext = context;
this.mDeviceEntries = new ArrayList<>();
populateFromStorage();
}
/**
* Inserts the given device into storage and notifies the mDeviceListView of a data update.
* #param newDevice The device to add to memory.
*/
public void put(Device newDevice) {
Preconditions.checkNotNull(newDevice);
boolean flagUpdatedExisting = false;
for (Device device : mDeviceEntries) {
if (newDevice.isVersionOf(device)) {
int index = mDeviceEntries.indexOf(device);
if(index != -1) {
mDeviceEntries.set(index, newDevice);
flagUpdatedExisting = true;
break;
} else {
throw new IllegalStateException();
}
}
//If an existing device was not updated, then this is a new device, add it to the list
if (!flagUpdatedExisting) {
mDeviceEntries.add(newDevice);
}
TECDataAdapter.setDevices(mDeviceEntries);
notifyDataSetChanged();
}
/**
* If the given device exists in storage, delete it and remove it from the mDeviceListView.
* #param device
*/
public void delete(Device device) {
Preconditions.checkNotNull(device);
//Remove device from mDeviceEntries
Iterator iterator = mDeviceEntries.iterator();
while(iterator.hasNext()) {
Device d = (Device) iterator.next();
if(device.isVersionOf(d)) {
iterator.remove();
}
}
TECDataAdapter.setDevices(mDeviceEntries);
notifyDataSetChanged();
}
/**
* Retrieves Device entries from persistent storage and loads them into the dynamic
* array responsible for displaying the entries in the listView.
*/
public void populateFromStorage() {
List<Device> temp = Preconditions.checkNotNull(TECDataAdapter.getDevices());
mDeviceEntries = temp;
notifyDataSetChanged();
}
public int getCount() {
if (mDeviceEntries != null) {
return mDeviceEntries.size();
}
return 0;
}
public Object getItem(int position) {
return mDeviceEntries.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LinearLayout view;
if (convertView == null || mForceRedraw) //Regenerate the view
{
/* Draws my views */
} else //Reuse the view
{
view = (LinearLayout) convertView;
}
return view;
}
#Override
public void notifyDataSetChanged() {
mForceRedraw = true;
super.notifyDataSetChanged();
mForceRedraw = false;
}
}
You are in the adapter and calling notify dataset changed.This would ideally not even be needed.Because you are modifying the dataset which is used internally by your adapter.The getView method of your adapter will always be called whenever a view needs to be rendered.
The convertView approach is to solely recycle a view(not the data).It merely provides you an alternative to the expensive process of view inflation.
So what your code should be :
public View getView(final int position, View convertView, ViewGroup parent) {
LinearLayout view;
if (convertView == null) //Regenerate the view
{
/* inflate Draws my views */
} else
{
view = (LinearLayout) convertView;
}
//repopulate this view with the data that needs to appear at this position using getItem(position)
return view;
}
There are many bugs with notifyDataSetChanged() and usually they appear if you try doing some complex work with your list data.
Mostly, it is because the method is lazy and can't distinguish changes, so to avoid this problem, test your code with this scenario:
delete changing rows
call notifyDataSetChanged()
add changed rows at their indexes
again call notifyDataSetChanged()
and, tell me if it did'nt solve your problem.
Edit: After adapter code is put, I saw the flaw in your code.
Sorry for late the response:
convertView is the view which you had populated before after initializing it.
When in method getView() you get an instance of convertView, you must populate it before returning it.
so to be clear, do something like this:
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) //Regenerate the view
{
/* Instantiate your view */
} else {
view = convertView;
}
// populate the elements in view like EditText, TextView, ImageView and etc
return view;
}