2013-01-07 59 views
0

你好,我创建了一个自定义列表视图和更新使用notifyDataSetChanged()方法,但我的列表没有更新。请帮帮我。我的客户列表视图不会随新数据更新

这是我的源代码

public class fourthPage extends ListActivity { 
ListingFeedParser ls; 
List<Listings> data; 
EditText SearchText; 
Button Search; 
private LayoutInflater mInflater; 
private ProgressDialog progDialog; 
private int pageCount = 0; 
String URL; 
ListViewListingsAdapter adapter; 
Message msg; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Bundle b = getIntent().getExtras(); 
    URL = b.getString("URL"); 
    Log.i("Ran->URL", "->" + URL); 
    MYCITY_STATIC_DATA.fourthPage_main_URL = URL; 
    final ListingFeedParser lf = new ListingFeedParser(URL); 
    Search = (Button) findViewById(R.id.searchButton); 
    SearchText = (EditText) findViewById(R.id.search); 
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(SearchText.getWindowToken(), 0); 
    this.getWindow().setSoftInputMode(
      WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    try { 
     progDialog = ProgressDialog.show(this, "", 
       "Loading please wait....", true); 
     progDialog.setCancelable(true); 
     new Thread(new Runnable() { 

      @Override 
      public void run() { 

       try { 
        data = lf.parse(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       msg = new Message(); 
       msg.what = 1; 
       fourthPage.this._handle.sendMessage(msg); 

      } 
     }).start(); 

     Search.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       SearchText = (EditText) findViewById(R.id.search); 
       if (SearchText.getText().toString().equals("")) 
        return; 
       CurrentLocationTimer myLocation = new CurrentLocationTimer(); 
       LocationResult locationResult = new LocationResult() { 
        @Override 
        public void gotLocation(final Location location) { 
         Toast.makeText(
           getApplicationContext(), 
           location.getLatitude() + " " 
             + location.getLongitude(), 
           Toast.LENGTH_LONG).show(); 
         String URL = "http://75.125.237.76/phone_feed_2_point_0_test.php?" 
           + "lat=" 
           + location.getLatitude() 
           + "&lng=" 
           + location.getLongitude() 
           + "&page=0&search=" 
           + SearchText.getText().toString(); 
         Log.e("fourthPage.java Search URL :->", "" + URL); 
         Bundle b = new Bundle(); 
         b.putString("URL", URL); 
         Intent it = new Intent(getApplicationContext(), 
           fourthPage.class); 
         it.putExtras(b); 
         startActivity(it); 
        } 
       }; 
       myLocation.getLocation(getApplicationContext(), 
         locationResult); 
      } 
     }); 

    } catch (Exception e) { 
     Toast.makeText(getApplicationContext(), 
       "No data available for this request", Toast.LENGTH_LONG) 
       .show(); 
    } 
} 

private Handler _handle = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     progDialog.dismiss(); 
     if (msg.what == 1) { 
      if (data.size() == 0 || data == null) { 
       Toast.makeText(getApplicationContext(), 
         "No data available for this request", 
         Toast.LENGTH_LONG).show(); 
      } 
      mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      adapter = new ListViewListingsAdapter(getApplicationContext(), 
        R.layout.list1, R.id.title, data, mInflater);    
      setListAdapter(adapter);     
      getListView().setTextFilterEnabled(true); 
      adapter.notifyDataSetChanged(); 
     } else { 
      Toast.makeText(getApplicationContext(), 
        "Error in retrieving the method", Toast.LENGTH_SHORT) 
        .show(); 
     } 

    } 
}; 

public void onListItemClick(ListView parent, View v, int position, long id) { 
    // remember i m going from bookmark list 
    MYCITY_STATIC_DATA.come_from_bookmark = false; 
    Log.i("4thPage.java - MYCITY_STATIC_DATA.come_from_bookmark", 
      "set false - > check" + MYCITY_STATIC_DATA.come_from_bookmark); 
    Listings sc = (Listings) this.getListAdapter().getItem(position); 
    if (sc.getName().equalsIgnoreCase("SEE MORE...")) { 
     pageCount = pageCount + 1; 
     final ListingFeedParser lf = new ListingFeedParser((URL.substring(
       0, URL.length() - 1)) + pageCount); 
     try { 
      progDialog = ProgressDialog.show(this, "", 
        "Loading please wait....", true); 
      progDialog.setCancelable(true); 
      new Thread(new Runnable() { 
       @Override 
       public void run() { 

        data.remove(data.size() - 1); 
        data.addAll(lf.parse()); 

        Message msg = new Message(); 
        msg.what = 1; 
        fourthPage.this._handle.sendMessage(msg); 

       } 
      }).start(); 
     } catch (Exception e) { 
      pageCount = pageCount - 1; 
      // TODO: handle exception 
      Toast newToast = Toast.makeText(this, "Error in getting Data", 
        Toast.LENGTH_SHORT); 
     } 
    } else { 
     Bundle b = new Bundle(); 
     b.putParcelable("listing", sc); 
     Intent it = new Intent(getApplicationContext(), 
       FifthPageTabbed.class); 
     it.putExtras(b); 
     startActivity(it); 
    } 
} 

@Override 
public void onBackPressed() { 
    setResult(0); 
    finish(); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    Log.e("RESUME:-)", "4th Page onResume"); 
    try {   
     //adapter.notifyDataSetChanged(); 
     //setListAdapter(adapter); 
     //getListView().setTextFilterEnabled(true); 
    } catch (Exception e) { 
     Log.e("EXCEPTION in 4th page", 
       "in onResume msg:->" + e.getMessage()); 
    } 
} 

}

回答

0

不要重新创建要传递到适配器的ArrayList对象或数组,只需修改相同的ArrayList或阵列。并且在修改适配器后数组或arrylist大小不变时,那么notifydatasetchange将不起作用。

在shot中它只在array或arraylist大小增加或减少时才起作用。

0

哪个版本的Android,你定位?最新版本似乎修改了notifyDataSetChanged()的工作方式。如果你瞄准sdk 11,它可能会工作?

而且,似乎是一个不同的(而且非常完整的答案)这个问题在另一篇文章: notifyDataSetChanged example

+0

如果我的答案帮助你,你能接受吗?提前致谢。 – lynvie