Data That is Fetched From The Web Server Are Appending

Search This thread

clonedaccnt

Member
Jun 4, 2014
44
0
I have this search function for my app that fetches data from web server using json, everything works completely except that everytime I search something, the data keeps on appending on my listview. For example if I search for a data with an id number 7 then press search button, the data is fetched and placed on the listview which what I want, but then if I search again the id number 7, there are now 2 instances of data with an id number of 7 in the listview. What I want is to refresh the listview for every search so that the only data that will appear on the listview is the current searched data.

MainActivity.java
Code:
package learn2crack.listview;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;

import learn2crack.listview.library.JSONParser;

public class MainActivity extends Activity {
	ListView list;
	TextView title;
	Button Btngetdata;
	ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
	
	//JSON Node Names 
	private static final String TAG_NEWS = "news";
	private static final String TAG_TITLE = "title";
	
	JSONArray android = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        setContentView(R.layout.activity_main);
        oslist = new ArrayList<HashMap<String, String>>();
        
        Btngetdata = (Button)findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
		         new JSONParse().execute();
			}
		});
    }
    
    private class JSONParse extends AsyncTask<String, String, JSONObject> {
    	 private ProgressDialog pDialog;
    	@Override
        protected void onPreExecute() {
            super.onPreExecute();
             title = (TextView)findViewById(R.id.title);
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
    	}
    	
    	@Override
        protected JSONObject doInBackground(String... args) {
    		
    		JSONParser jParser = new JSONParser();

    		String url = "http://localhost/abc-news/news.php?json-request-news=";
    	
    		EditText id = (EditText)findViewById(R.id.search_text);
    		
    		url = url + id.getText().toString();
    		
    		// Getting JSON from URL
    		JSONObject json = jParser.getJSONFromUrl(url);
    		return json;
    	}
    	
    	 @Override
         protected void onPostExecute(JSONObject json) {
    		 pDialog.dismiss();
    		 try {
    				// Getting JSON Array from URL
    				android = json.getJSONArray(TAG_NEWS);
    				for(int i = 0; i < android.length(); i++){
    				JSONObject c = android.getJSONObject(i);
    				
    				// Storing  JSON item in a Variable
    				String title = c.getString(TAG_TITLE);	
    				
    				// Adding value HashMap key => value

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

    				map.put(TAG_TITLE, title);
    				
    				oslist.add(map);
    				list=(ListView)findViewById(R.id.list);

    				ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
    						R.layout.list_v,
    						new String[] { TAG_TITLE }, new int[] {
    								R.id.title });

    				list.setAdapter(adapter);
    				list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    		            @Override
    		            public void onItemClick(AdapterView<?> parent, View view,
    		                                    int position, long id) {
    		                Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();

    		            }
    		        });

    				}
    		} catch (JSONException e) {
    			e.printStackTrace();
    		} 
    	 }
    }
}

I attached an image below to support this problem I'm encountering.
 

Attachments

  • Capture.PNG
    Capture.PNG
    56.6 KB · Views: 16
Last edited:

deanwray

Senior Member
Apr 2, 2006
1,145
427
www.deanwray.co.uk
Code:
oslist.add(map);

you're adding to the list that drives the adapter, add does what it implies ... maybe you should go though some basics, copy and pasting code sometimes wastes more time that you think it would save in the long run.

Hope that helps
 
  • Like
Reactions: clonedaccnt

clonedaccnt

Member
Jun 4, 2014
44
0
What do I need to do to my code so that the newly searched data will not append on the previous data that is fetched?
 
Last edited:

deanwray

Senior Member
Apr 2, 2006
1,145
427
www.deanwray.co.uk
What do I need to do to my code so that the newly searched data will not append on the previous data that is fetched?

I thought that was clear, don't "add" to what you have? Are you aware of what an array is ? or a list? and an adapter? cause I think I would start there, you just keep adding to the list that powers the adapter. If you dont want to add to it just don't, either clear it or replace it.

so just to be clear, a list or map has the method .clear() <--- that clears it of all data
 
  • Like
Reactions: clonedaccnt

clonedaccnt

Member
Jun 4, 2014
44
0
I've already solve the problem earlier, I was going to post that I've already solve it but found out that you've already replied on the thread, sorry. About the problem, yes I too used the .clear() of the ArrayList to clear the array before adding a new one, it's my first time to create an activity that pass the data on the same activity, I'm used to passing the data from one activity to another so I don't have a chance to encounter this kind of problem.

Anyways thanks for helping I will not have accomplished this without your help.
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    Code:
    oslist.add(map);

    you're adding to the list that drives the adapter, add does what it implies ... maybe you should go though some basics, copy and pasting code sometimes wastes more time that you think it would save in the long run.

    Hope that helps
    1
    What do I need to do to my code so that the newly searched data will not append on the previous data that is fetched?

    I thought that was clear, don't "add" to what you have? Are you aware of what an array is ? or a list? and an adapter? cause I think I would start there, you just keep adding to the list that powers the adapter. If you dont want to add to it just don't, either clear it or replace it.

    so just to be clear, a list or map has the method .clear() <--- that clears it of all data