2013-08-29 49 views
-3

我開發Android應用程序中,我想與負載更多按鈕列表視圖顯示JSON數據,但是當我點擊加載更多的按鈕舊數據被新的,我想這兩個老保換而新的數據是displayed.Lihstview應該顯示}它取代了數據什麼是錯在下面的代碼的ListView與loadmore按鈕替換舊數據

public class Newjava extends Activity { 
JSONObject jsonobject; 
JSONArray jsonarray; 
CustomAdapter1 adapter2; 
CustomAdapter1 adapter; 
ProgressDialog mProgressDialog; 
ArrayList<FeedAddress> arraylist; 
String url = "http://xxxx.vdv.com/api/fvn/page1"; 
String arraymovie = "all"; 
public String string; 
ListView listView; 
int i = 1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    listView = (ListView) findViewById(R.id.list); 
    arraylist = new ArrayList<FeedAddress>(); 
    // Retrive JSON Objects from the given website URL in 
    // JSONfunctions.class 

    jsonobject = JSONfunctions.getJSONfromURL(url); 

    try { 
     // Locate the array name 
     JSONObject jsonObject1 = jsonobject.getJSONObject("appname"); 
     jsonarray = jsonObject1.getJSONArray("kfnh"); 

     for (int i = 0; i < jsonarray.length(); i++) { 
      JSONObject post = (JSONObject) jsonarray.getJSONObject(i); 
      FeedAddress map = new FeedAddress(); 
      jsonobject = jsonarray.getJSONObject(i); 
      // Retrive JSON Objects 
      map.setMovieName(post.getString("movieName")); 
      map.setName(post.getString("movieActors")); 
      map.setMovieId(post.getString("movieId")); 
      String imageUrl = "http://www.xxhbc.com/upload/dv/thumbnail/" 
        + post.getString("moviePhoto") 
        + post.getString("moviePhotoExt"); 
      map.setImageUrl(imageUrl); 
      System.out.println("ldhslhdljh " + imageUrl); 
      // Set the JSON Objects into the array 
      arraylist.add(map); 
     } 
    } catch (JSONException e) { 
     Log.e("Error", e.getMessage()); 
     e.printStackTrace(); 
    } 

    Button btnLoadMore = new Button(this); 
    btnLoadMore.setText("Load More"); 

    // Adding Load More button to lisview at bottom 
    listView.addFooterView(btnLoadMore); 

    // Getting adapter 
    adapter = new CustomAdapter1(this, arraylist); 
    listView.setAdapter(adapter); 

    /** 
    * Listening to Load More button click event 
    * */ 
    btnLoadMore.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Starting a new async task 
      i += 1; 
      new DownloadJSON().execute(); 
     } 
    }); 
} 

private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    // Create a progressdialog 
     mProgressDialog = new ProgressDialog(Newjava.this); 
     // Set progressdialog title 
     mProgressDialog.setTitle("APP Name"); 
     mProgressDialog.setIcon(R.drawable.ic_launcher); 
     // Set progressdialog message 
     mProgressDialog.setMessage("Loading..."); 
     mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     mProgressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // Create the array 
     arraylist = new ArrayList<FeedAddress>(); 
     // Retrive JSON Objects from the given website URL in 
     // JSONfunctions.class 

     String URL = "http://zvzvs.vczx.com/api/v/page" 
       + i; 
     System.out.println("new url " + URL); 
     jsonobject = JSONfunctions.getJSONfromURL(URL); 

     try { 
      // Locate the array name 
      JSONObject jsonObject1 = jsonobject 
        .getJSONObject("fvvzx"); 
      jsonarray = jsonObject1.getJSONArray("allmovies"); 

      for (int i = 0; i < jsonarray.length(); i++) { 
       JSONObject post = (JSONObject) jsonarray.getJSONObject(i); 
       FeedAddress map = new FeedAddress(); 
       jsonobject = jsonarray.getJSONObject(i); 
       // Retrive JSON Objects 
       map.setMovieName(post.getString("movieName")); 
       // map.setName(post.getString("movieActors")); 
       map.setMovieId(post.getString("movieId")); 
       String imageUrl = "http://www.vsv.com/upload/vc/thumbnail/" 
         + post.getString("moviePhoto") 
         + post.getString("moviePhotoExt"); 
       map.setImageUrl(imageUrl); 
       System.out.println("ldhslhdljh " + imageUrl); 
       // Set the JSON Objects into the array 
       arraylist.add(map); 
      } 

     } catch (JSONException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void args) { 
     // Locate the listview in listview_main.xml 
     int currentPosition = listView.getFirstVisiblePosition(); 

     // Appending new data to menuItems ArrayList 
     adapter = new CustomAdapter1(Newjava.this, arraylist); 
     listView.setAdapter(adapter); 

     // Setting new scroll position 
     listView.setSelectionFromTop(currentPosition + 1, 0); 
     updateList(string); 
    } 
} 

public void updateList(final String string) { 
    // TODO Auto-generated method stub 

    listView.setVisibility(View.VISIBLE); 
    // mProgressDialog.dismiss(); 
    listView.setAdapter(new CustomAdapter1(Newjava.this, arraylist)); 
    listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, 
       int position, long arg3) { 
      // TODO Auto-generated method stub 
      // Toast.makeText(getApplicationContext(),"position[]" , 
      // Toast.LENGTH_SHORT).show(); 
      Object o = listView.getItemAtPosition(position); 
      FeedAddress newsData = (FeedAddress) o; 
      System.out.println("hgsh " + string); 
      System.out.println("next " + newsData.getMovieId()); 

      Intent intent = new Intent(Newjava.this, 
        SingleMenuItemActivity.class); 
      intent.putExtra("feed", newsData.getMovieId()); 
      intent.putExtra("actor", newsData.getName()); 
      startActivity(intent); 
     } 
    }); 
} 

所有數據

+1

可如果您使用的ArrayList來存儲數據,那麼你應該檢查你是不是將數據添加到it.For更多幫助前結算清單大家分享你的代碼... –

+0

,您需要發佈您的代碼。 –

+1

請告訴我們** [你嘗試過什麼(http://mattgemmell.com/2008/12/08/what-have-you-tried/)** – Leeeeeeelo

回答

0

,因爲你每次都重新創建適配器。您應該一次創建適配器,然後給它添加更多的數據,而不是重新創建。