2012-07-25 55 views
2

我使用LazyAdapter創建了一個listview。Android ListView OnScrollListener清除舊數據

當我滾動到列表的底部時,新數據顯示出來,但它清除了所有的舊數據。

我如何在舊數據下添加新數據?

這是我的主要活動代碼

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

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.w3c.dom.NodeList; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.sax.Element; 
import android.view.View; 
import android.widget.AbsListView; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.TextView; 

public class AndroidJSONParsingActivity extends Activity { 

    // url to make request 


    // JSON Node names 
    static final String TAG_CONTACTS = "comics"; 
    static final String TAG_ID = "id"; 
    static final String TAG_NAME = "title"; 
    static final String TAG_EMAIL = "id"; 
    static final String TAG_PHONE_MOBILE = "image"; 

    // contacts JSONArray 
    JSONArray contacts = null; 
    ListView list; 
    LazyAdapter adapter; 
    static int counter = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

         // call the function 
       LoadData(); 




    } 

    public void LoadData(){ 
     String url = "http://myurl.com/?start=" + counter; 
     // Hashmap for ListView 
       ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

       // Creating JSON Parser instance 
       JSONParser jParser = new JSONParser(); 

       // getting JSON string from URL 
       JSONObject json = jParser.getJSONFromUrl(url); 

       try { 
        // Getting Array of Contacts 
        contacts = json.getJSONArray(TAG_CONTACTS); 

        // looping through All Contacts 
        for(int i = 0; i < contacts.length(); i++){ 
         JSONObject c = contacts.getJSONObject(i); 

         // Storing each json item in variable 
         String id = c.getString(TAG_ID); 
         String name = c.getString(TAG_NAME); 
         String email = c.getString(TAG_EMAIL); 
         String mobile = c.getString(TAG_PHONE_MOBILE); 
         // Phone number is agin JSON Object 

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

         // adding each child node to HashMap key => value 
         map.put(TAG_ID, id); 
         map.put(TAG_NAME, name); 
         map.put(TAG_EMAIL, email); 
         map.put(TAG_PHONE_MOBILE, mobile); 

         // adding HashList to ArrayList 
         contactList.add(map); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 


       /** 
       * Updating parsed JSON data into ListView 
       * */ 
     list=(ListView)findViewById(R.id.list); 

       // Getting adapter by passing xml data ArrayList 
       adapter=new LazyAdapter(this, contactList);   
       list.setAdapter(adapter); 

       // Launching new screen on Selecting Single ListItem 
       list.setOnItemClickListener(new OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
         // getting values from selected ListItem 
         String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); 
         String cost = ((TextView) view.findViewById(R.id.email)).getText().toString(); 
         String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString(); 

         // Starting new intent 
         Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); 
         in.putExtra(TAG_NAME, name); 
         in.putExtra(TAG_EMAIL, cost); 
         in.putExtra(TAG_PHONE_MOBILE, description); 
         startActivity(in); 

        } 
       }); 


       list.setOnScrollListener(new OnScrollListener() { 



        @Override 
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
         if (view.getAdapter() != null && ((firstVisibleItem + visibleItemCount) >= totalItemCount)) { 
          //Log.v(TAG, "onListEnd, extending list"); 
          //mPrevTotalItemCount = totalItemCount; 
          counter = totalItemCount; 

          LoadData(); 
          adapter.notifyDataSetChanged();} 
        } 

        @Override 
        public void onScrollStateChanged(AbsListView view, int scrollState) { 
        } 

       }); 





    } 
} 

這是我LazyAdapter.java文件

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

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 

    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 



    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 


    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.list_row, null); 

     TextView title = (TextView)vi.findViewById(R.id.title); // title 
     TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name 
     TextView duration = (TextView)vi.findViewById(R.id.duration); // duration 
     ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image 

     HashMap<String, String> song = new HashMap<String, String>(); 
     song = data.get(position); 



     // Setting all values in listview 
     title.setText(song.get(AndroidJSONParsingActivity.TAG_NAME)); 
     artist.setText(song.get(AndroidJSONParsingActivity.TAG_ID)); 
     duration.setText(song.get(AndroidJSONParsingActivity.TAG_EMAIL)); 
     imageLoader.DisplayImage(song.get(AndroidJSONParsingActivity.TAG_PHONE_MOBILE), thumb_image); 
     return vi; 
    } 
} 
+0

我在這裏有同樣的問題。我已經閱讀了Joel給出的答案,但我沒有理解這一部分:'然後,只需在現有適配器中添加新的聯繫人列表,這意味着我應該這樣做:'adapter = new LazyAdapter(this,contactList )',但他也提到:「那麼你不必再次初始化列表視圖和適配器。」那麼,如何將新的contactList添加到現有的適配器?你是怎麼做到的? – azizbekian 2012-12-15 21:54:11

+0

我用'加'而不是'初始化'這個詞。當我說添加我的意思是追加。將新值添加到現有適配器。換句話說,修改基本adatper使用的列表,並調用notifyDataSetChanged()。或者使用一個ArrayAdapter,它有一個add()方法,可以方便地操縱listview – Joel 2012-12-16 00:22:32

回答

1

我認爲這個問題是在你的問題是,在loadData()方法。每次重新創建contactList時間..

ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

然後調用...

adapter=new LazyAdapter(this, contactList);   
list.setAdapter(adapter); 

這將創建一個新的適配器每次加載數據的時間,而不是將數據附加到當前的適配器。您已經擁有適配器作爲實例變量,因此您無需每次都初始化它。你應該在onCreate()方法中初始化你的listview和你的適配器,然後你再也不需要這樣做。然後,只需將新聯繫人列表添加到現有適配器中,然後致電notifyDataSetChanged();,那麼您應該很好。

+0

Joel,請閱讀我對答案的評論。 – azizbekian 2012-12-15 21:55:24