android


Add item in listview only if not already present


I would like to add in my Android widget a new item only if this item is not already present in the list.
I tried to use contains() method but it still doesn't work.
In fact, i get a JSON object, and for each JSON object i would like to test if there is already an item corresponding to this JSON objecti in the list.
If yes, do nothing, if no, add the item on the top of the list.
Here is my code :
//parsing data
String titre, url, site, ancienprix, nouveauprix, temperature, pseudo, categorie, dealtermine, date, id;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
SharedPreferences setNbDeal = PreferenceManager.getDefaultSharedPreferences(mContext);
String nbDeal = setNbDeal.getString("nbDeal", "30");
int nbDeals = Integer.parseInt(nbDeal);
for(int i=0;i<nbDeals;i++){
json_data = jArray.getJSONObject(i);
titre=json_data.getString("titre");
url=json_data.getString("url");
site=json_data.getString("site");
ancienprix=json_data.getString("ancienprix");
nouveauprix=json_data.getString("nouveauprix");
dealtermine=json_data.getString("dealtermine");
temperature=json_data.getString("temperature");
pseudo=json_data.getString("pseudo");
categorie=json_data.getString("categorie");
date=json_data.getString("date");
id=json_data.getString("id");
WidgetItem currentItem = new WidgetItem(titre, url, site, ancienprix, nouveauprix, dealtermine, temperature, pseudo, categorie, date);
if (mWidgetItems.contains(currentItem)){
break;
} else {
mWidgetItems.add(currentItem);
}
}
}
Here is the code to declare mWidgetItem :
private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();
Here is the code of my Widget Item class :
public class WidgetItem {
public String titre, url, ancienprix, nouveauPrix, temperature, site, submitter, categorie, dealTermine, date;
public WidgetItem(String titre, String url, String site, String ancienprix, String nouveauPrix, String dealTermine, String temperature, String submitter, String categorie, String date) {
this.titre = titre;
this.url = url;
this.site = site;
this.ancienprix = ancienprix;
this.nouveauPrix = nouveauPrix;
this.dealTermine = dealTermine;
this.temperature = temperature;
this.submitter = submitter;
this.categorie = categorie;
this.date = date;
}
}
You can use this method after adding all element into your list (It removes duplicates):
private ArrayList<WidgetItem> getUniqueElements(Collection<WidgetItem> values)
{
return new ArrayList<WidgetItem>(new TreeSet<WidgetItem>(values));
}

Related Links

How to decrease brightness like screen filter
Android device as touchpad
ScoreloopUI -> Source is not found
Problems with httpget connection
how to set opacity of background image without affecting text
Weird behavior with buttons and visibility gone
Does Committing Editor - SharedPreferences Close the Transaction?
SVG Background Image on Android Chrome
PDFJet-Open-source: How to set multiline text inside Cell object
Wifi Receiver not triggered Android 4.x.x works on 2.2.1
How to get bitmap from sdcard
Setting click event on a dynamically created view in view pager
AutoCompleteView not wide enough
ScrollView not scrolling after setting View.OnTouchListener on it
Creating a Custom Seekbar
onCreate() not getting called in the Database helper

Categories

HOME
service-worker
developer-tools
linkedin
jmeter
mysql-workbench
jelastic
entity-framework-core
spring-session
jax-ws
nstableheaderview
rcloud
syntaxnet
mql5
modbus-tcp
scsi
wolframalpha
nodemcu
replication
blade
ksh
gulp-watch
hta
game-theory
intersystems-cache
ceylon
tizen-tv
dst
paragraph
linkedhashmap
sammy.js
gnat-gps
vue-chartjs
servicestack-text
magento-1.9.2.4
jpad
graphhopper
flatmap
gdata
railstutorial.org
visa
odoo-website
branching-and-merging
tcc
snapchat
phpdbg
xbmc
netbeans6.8
tf
mongotemplate
zenhub
jtree
materialized-path-pattern
rocks
visual-studio-2008-sp1
stripe-connect
uicolor
scaffolding
observablecollection
udpclient
omniauth-facebook
data-protection
boost-serialization
createprocess
kendo-upload
blitline
toran-proxy
iokit
computer-architecture
dulwich
linked-tables
dnssec
cwrsync
zend-currency
metatrader5
webfinger
insight.database
ss7
rolify
time.h
node-blade
diem
sqlclr
joomla3.1
nokia-s40
sentestingkit
database-create
role
w3c-geolocation
cuteeditor
diagrams
cruisecontrol.rb
addchild
firefox-3

Resources

Encrypt Message