Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to add swipe to refresh to my code,After adding data when i go back to the activity where the list view is present the list remains the same.how to refresh it.
please help..
thanks in advance

What I have tried:

public class TrainingActivity extends ActionBarActivity {

private String jsonResult;
public String url ="";
ListView listView;
List<Map<string,string>> activity = new ArrayList<Map<string,string>>();




@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_training);
listView = (ListView) findViewById(R.id.listView);

accessWebService();


}

private class JsonReadTask extends AsyncTask<string,>
{

@Override
protected String doInBackground(String... params)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try{
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();

}catch(ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return null;
}

private StringBuilder inputStreamToString(InputStream is){
String rLine ="";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try{
while((rLine = rd.readLine()) != null){
answer.append(rLine);
}
}catch(IOException e){
Toast.makeText(getApplicationContext(),"error ..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}

@Override
protected void onPostExecute(String result){
ListDrwaer(); // we will create it later
}
}

public void accessWebService(){
JsonReadTask task = new JsonReadTask();
task.execute(new String[] {url});
}

public void ListDrwaer (){
try{
JSONObject jsonResonse = new JSONObject(jsonResult.substring(jsonResult.indexOf("{"), jsonResult.lastIndexOf("}")+1));
JSONArray jsonMainNode = jsonResonse.optJSONArray("activity");

final ArrayList<hashmap><string,string>> MyArrList = new ArrayList<hashmap><string,>>();

HashMap<string,string> map;

for(int i=0; i<jsonmainnode.length();> JSONObject c = jsonMainNode.getJSONObject(i);

map = new HashMap<string,string>();

map.put("activity", c.getString("activity"));
map.put("time", c.getString("time"));
map.put("date", c.getString("date"));

MyArrList.add(map);

SimpleAdapter sAdap;
sAdap = new SimpleAdapter(TrainingActivity.this, MyArrList, R.layout.activity_column, new String[]{"activity","time","date"}, new int[]{R.id.ColMemberID, R.id.ColName, R.id.Colcity});

listView.setAdapter(sAdap);





}
}catch (JSONException e){
Toast.makeText(getApplicationContext(),"error ..." + e.toString(), Toast.LENGTH_LONG).show();
}
}




}
Posted
Updated 24-May-16 1:24am

1 solution

You need to add some code when you update the data, to tell the ListView (or the class that manages it) to update the display. You may also like to look at Managing the Activity Lifecycle | Android Developers[^].
 
Share this answer
 
Comments
Dheeraj Cidda 24-May-16 8:38am    
public class Activity_update extends ActionBarActivity {

private String jsonResult;
public String url ="";
ListView listView;
List<Map<string,string>> activity = new ArrayList<Map<string,string>>();

TextView ADD,delete,edit;


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);

listView = (ListView) findViewById(R.id.listView);

accessWebService();

ADD = (TextView)findViewById(R.id.add);
ADD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activity_update.this,Training_add.class));

}
});
delete = (TextView)findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activity_update.this,Training_del.class));

}
});
edit = (TextView)findViewById(R.id.edit);
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Activity_update.this,Training_edit.class));

}
});

}

private class JsonReadTask extends AsyncTask<string, void,="" string="">
{

@Override
protected String doInBackground(String... params)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try{
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();

}catch(ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return null;
}

private StringBuilder inputStreamToString(InputStream is){
String rLine ="";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try{
while((rLine = rd.readLine()) != null){
answer.append(rLine);
}
}catch(IOException e){
Toast.makeText(getApplicationContext(),"error ..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}

@Override
protected void onPostExecute(String result){
ListDrwaer(); // we will create it later
}
}

public void accessWebService(){
JsonReadTask task = new JsonReadTask();
task.execute(new String[] {url});
}

public void ListDrwaer (){
try{
JSONObject jsonResonse = new JSONObject(jsonResult.substring(jsonResult.indexOf("{"), jsonResult.lastIndexOf("}")+1));
JSONArray jsonMainNode = jsonResonse.optJSONArray("activity");

final ArrayList<hashmap<string,string>> MyArrList = new ArrayList<hashmap<string, string="">>();

HashMap<string,string> map;

for(int i=0; i<jsonmainnode.length(); i++){
="" jsonobject="" c="jsonMainNode.getJSONObject(i);

" map="new" hashmap<string,string="">();

map.put("activity", c.getString("activity"));
map.put("time", c.getString("time"));
map.put("date", c.getString("date"));

MyArrList.add(map);

SimpleAdapter sAdap;
sAdap = new SimpleAdapter(Activity_update.this, MyArrList, R.layout.activity_column, new String[]{"activity","time","date"}, new int[]{R.id.ColMemberID, R.id.ColName, R.id.Colcity});

listView.setAdapter(sAdap);





}
Dheeraj Cidda 24-May-16 8:40am    
this is my code for adding data,it is updating in the list only when i press back and again come to the activity.what code must be added,any help please
Richard MacCutchan 24-May-16 8:49am    
Sorry I do not know, and I do not plan to download all that code to try and work out the logic of your application.
Dheeraj Cidda 24-May-16 9:20am    
can you suggest any links for understanding
Richard MacCutchan 24-May-16 9:35am    
Google - but you need to be specific about what you do not understand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900