Reputation:
I am new to android. I am developing an application in which I get data from server in JSONArray
format. I am saving the data in shared preferences and then retrieving it and displaying it in a ListView
. I have to add the new entered data from the server and replace the existing data. I have tried but I get the data added twice i.e the one retrieved from shared preferences and then the data from server.
public class TipsActivity extends AppCompatActivity {
private Toolbar toolbar;
Pojo pojo;
ListView listTips;
String strServerResponse = null;
ArrayList<Pojo> tips;
ConnectionDetector cd;
TipsAdapter tipsAdapter;
SharedPreferences MyPrefs;
String tipsss;
Context context;
ArrayList<String> tii;
ProgressBar nPregress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips);
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitle("Tip of the Day");
setSupportActionBar(toolbar);
listTips = (ListView) findViewById(R.id.tipsList);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
nPregress = (ProgressBar) findViewById(R.id.toolbar_progress_bar);
nPregress.setVisibility(View.GONE);
tips = new ArrayList<Pojo>();
tii = new ArrayList<String>();
SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
Set<String> set = prefs.getStringSet("tipoftheday", null);
if(set!= null){
for(String p : set)
{
pojo = new Pojo();
pojo.setTip(p);
tips.add(pojo);
}
tipsAdapter = new TipsAdapter(TipsActivity.this, tips);
listTips.setAdapter(tipsAdapter);
}
else{
new NetCheck().execute();
}
new NetCheck().execute();
}
private class NetCheck extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
nPregress.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
nPregress.setVisibility(View.GONE);
SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
Set<String> set = prefs.getStringSet("tipoftheday", null);
for(String p : set)
{
pojo = new Pojo();
pojo.setTip(p);
tips.add(pojo);
}
tipsAdapter = new TipsAdapter(TipsActivity.this, tips);
listTips.setAdapter(tipsAdapter);
return;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(
"http://tipseducation.com/system/eadmin/gettipofday/");
httpRequest.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpRequest.setEntity(se);
HttpResponse httpRes = httpClient.execute(httpRequest);
java.io.InputStream inputStream = httpRes.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
strServerResponse = sb.toString();
Log.e("Server Response", "" + strServerResponse.toString());
if (strServerResponse != null) {
try {
JSONArray arr = new JSONArray(strServerResponse);
JSONObject jsonObj = arr.getJSONObject(0);
ArrayList<String> tii = new ArrayList<String>();
for (int i = 0; i < arr.length(); i++) {
pojo = new Pojo();
JSONObject jobj2 = arr.getJSONObject(i);
String tipoftheday = jobj2.optString("tipsoftheday");
tii.add(tipoftheday);
}
List<String> listTemp = tii;
Set<String> temp = new HashSet<String>(listTemp);
SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
temp.addAll(listTemp);
editor.putStringSet("tipoftheday",temp);
editor.commit();
}
How can i do this. can anyone please help me.
Upvotes: 0
Views: 222
Reputation: 989
This is a basic thing. You are using ArrayList for showing data in listview. Now if you are getting new data from server there you need to clear your arraylist object like :
tips.clear();
Create setter method in your adapter class like:
public void setTipsList(ArrayList tips){
this.tips=tips;
}
And from Activity class just create arraylist by data getting from server.
for(String p : set)
{
pojo = new Pojo();
pojo.setTip(p);
tips.add(pojo);
}
adapter.setTips(tips);
adapter.notifyDataSetChanged();
After calling notifyDataSetChanged() listview change will show.
Upvotes: 0
Reputation: 132972
i have tried but i get the data added twice i.e the one retrived from shared prefernces and then the data from server.
Because need to clear tips
ArrayList before adding new data in it:
tips.clear(); // clear tips ArrayList
for(String p : set)
{
pojo = new Pojo();
pojo.setTip(p);
tips.add(pojo);
}
Upvotes: 1