我的自定义列表视图未使用新数据进行更新

时间:2013-01-07 13:58:54

标签: android-listview notifydatasetchanged

您好我创建了一个自定义列表视图,并且更新使用了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());
    }
}

}

2 个答案:

答案 0 :(得分:0)

您定位的是哪个Android版本?最新版本似乎修改了notifyDataSetChanged()的工作原理。如果你瞄准sdk 11它可能有用吗?

此外,在另一篇文章中似乎有一个不同的(并且非常彻底的答案): notifyDataSetChanged example

答案 1 :(得分:0)

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

在镜头中,只有当阵列或arraylist大小增加或减少时才有效。