Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,806,408 Members 52,932 Now Online
XDA Developers Android and Mobile Development Forum

Data That is Fetched From The Web Server Are Appending

Tip us?
 
clonedaccnt
Old
(Last edited by clonedaccnt; 4th July 2014 at 01:13 AM.)
#1  
Member - OP
Thanks Meter 0
Posts: 33
Join Date: Jun 2014
Default Data That is Fetched From The Web Server Are Appending

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.
Attached Thumbnails
Click image for larger version

Name:	Capture.PNG
Views:	15
Size:	56.6 KB
ID:	2831615  
 
deanwray
Old
#2  
deanwray's Avatar
Senior Member
Thanks Meter 418
Posts: 1,121
Join Date: Apr 2006

 
DONATE TO ME
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
The Following User Says Thank You to deanwray For This Useful Post: [ Click to Expand ]
 
clonedaccnt
Old
#3  
Member - OP
Thanks Meter 0
Posts: 33
Join Date: Jun 2014
Can you explain it more clearly here?
 
deanwray
Old
#4  
deanwray's Avatar
Senior Member
Thanks Meter 418
Posts: 1,121
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by clonedaccnt View Post
Can you explain it more clearly here?
erm, add = add ? sorry to come across like this but not sure what you are expecting... a,b,c .add(d) == a,b,c,d
 
clonedaccnt
Old
(Last edited by clonedaccnt; 4th July 2014 at 02:36 PM.)
#5  
Member - OP
Thanks Meter 0
Posts: 33
Join Date: Jun 2014
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?
 
deanwray
Old
#6  
deanwray's Avatar
Senior Member
Thanks Meter 418
Posts: 1,121
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by clonedaccnt View Post
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
The Following User Says Thank You to deanwray For This Useful Post: [ Click to Expand ]
 
clonedaccnt
Old
(Last edited by clonedaccnt; 4th July 2014 at 04:02 PM.)
#7  
Member - OP
Thanks Meter 0
Posts: 33
Join Date: Jun 2014
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.

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


MediaTek Releases Full Kernel Source for First Android One Devices

Those who might have thought that MediaTek wouldnever release working … more

Submitting a Patch to Gerrit – Featuring XplodWild – XDA Developer TV

Not every developer specializes in every area of … more

Learn How to Make a Live Wallpaper all by Yourself

Stillimages can bebeautiful, but some of you prefer to see animations on your screens. … more

Google Play Services Updated to Version 6.1

In addition to Android operating system proper, Google is focusing hard on giving the developers … more