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());
    }
}

}

4

2 回答 2

0

你的目标是什么版本的安卓?最新版本似乎修改了 notifyDataSetChanged() 的工作方式。如果您以 sdk 11 为目标,它可能会起作用吗?

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

于 2013-01-10T04:45:37.107 回答
0

不要重新创建传递给适配器的 ArrayList 或 Array 对象,只需再次修改相同的 ArrayList 或 Array。并且当您修改适配器后数组或 arrylist 大小未更改时,在这种情况下 notifydatasetchange 将不起作用。

在镜头中,它仅在数组或数组列表大小增加或减少时才有效。

于 2013-01-30T10:31:53.110 回答