T-Mobile HTC One M8 Gets Selfie-Friendly Android 4.4.4 OTA

The T-Mobie variant of HTC One (M8) just now received an over-the-air update … more

Android L is for Lockdown

Root is, without a doubt, the (un)holy grail of the Android world. Those wonderful permissions that allow you as … more

XDA Xposed Tuesday: XHangouts Will Improve Your Hangouts

Ever since Google started supporting text messaging in Hangouts many people have … more

Shattered Screen? Turn Your Broken Device into a Complete Media Center!

A cracked screen usually means you’ll have to spend a hefty … more
Post Reply

Data That is Fetched From The Web Server Are Appending

OP clonedaccnt

4th July 2014, 01:09 AM   |  #1  
OP Member
Thanks Meter: 0
 
34 posts
Join Date:Joined: Jun 2014
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  
Last edited by clonedaccnt; 4th July 2014 at 01:13 AM.
4th July 2014, 08:42 AM   |  #2  
deanwray's Avatar
Senior Member
Thanks Meter: 424
 
1,132 posts
Join Date:Joined: 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: [ View ]
4th July 2014, 12:37 PM   |  #3  
OP Member
Thanks Meter: 0
 
34 posts
Join Date:Joined: Jun 2014
Can you explain it more clearly here?
4th July 2014, 01:06 PM   |  #4  
deanwray's Avatar
Senior Member
Thanks Meter: 424
 
1,132 posts
Join Date:Joined: Apr 2006
Donate to Me
Quote:
Originally Posted by clonedaccnt

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
4th July 2014, 02:31 PM   |  #5  
OP Member
Thanks Meter: 0
 
34 posts
Join Date:Joined: 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?
Last edited by clonedaccnt; 4th July 2014 at 02:36 PM.
4th July 2014, 03:23 PM   |  #6  
deanwray's Avatar
Senior Member
Thanks Meter: 424
 
1,132 posts
Join Date:Joined: Apr 2006
Donate to Me
Quote:
Originally Posted by clonedaccnt

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: [ View ]
4th July 2014, 03:54 PM   |  #7  
OP Member
Thanks Meter: 0
 
34 posts
Join Date:Joined: 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.
Last edited by clonedaccnt; 4th July 2014 at 04:02 PM.

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Top Threads in Java for Android App Development by ThreadRank