用数据更新ListView的正确方法?

时间:2014-03-29 15:19:25

标签: java android listview android-listview parse-platform

我正在使用ParseQueryAdapter:

public class MyPostedSalesAdapter extends ParseQueryAdapter<Sale> {

    static String mTAG = "LocalSalesAdapter";
    static ParseUser mCurrentUser = ParseUser.getCurrentUser();

    public MyPostedSalesAdapter(Context context) {
        super(context, new ParseQueryAdapter.QueryFactory<Sale>() {
            @SuppressWarnings("unchecked")
            public ParseQuery<Sale> create() {
                @SuppressWarnings("rawtypes")
                ParseQuery query = new ParseQuery("Sales"); 
                query.orderByDescending("createdAt");
                query.whereEqualTo("postedBy", mCurrentUser.getUsername());
                try {
                    int salesAmount = query.count();
                     ProfileActivity.setTextView(salesAmount);
                } catch (ParseException e) {

                    e.printStackTrace();
                } 
                return query;
            }
        });
    }

    @Override
    public View getItemView(Sale sale, View v, ViewGroup parent) {

        if (v == null) {
            v = View.inflate(getContext(), R.layout.listview_cell, null);
        }

        super.getItemView(sale, v, parent);

        ParseImageView saleImage = (ParseImageView) v.findViewById(R.id.icon);
        ParseFile photoFile = sale.getParseFile("photo");
        if (photoFile != null) {
            saleImage.setParseFile(photoFile);
            saleImage.loadInBackground(new GetDataCallback() {
                @Override
                public void done(byte[] data, ParseException e) {

                }
            });
        } else { 
            // Clear ParseImageView if the object has no photo, set placeholder.
            saleImage.setImageResource(R.drawable.placeholder); 
        }
        TextView titleTextView = (TextView) v.findViewById(R.id.textView_listView_saleTitle);
        titleTextView.setText(sale.getSaleTitle());

        TextView priceTextView = (TextView) v.findViewById(R.id.textView_listView_salePrice);
        priceTextView.setText(sale.getSalePrice());
        return v;
    }

}

只需onResume,将适配器设置为ListView,如下所示:

@Override
    protected void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();

        // Setting up currentUser to current logged in user
        // If user is not logged in, present them with Login Activity
        if (mCurrentUser == null || !isUserOnline())
        {
            presentUserWithLogin();

        }
        else
        {
        mOwnSalesAdapter = new MyPostedSalesAdapter(this);
    mOwnSalesAdapter.loadObjects();
    mUserSales.setAdapter(mOwnSalesAdapter);
}
}

我在onResume上做了这个,每次加载Activity时都会想到,使用查询拉出新数据并显示在ListView中。然而事实并非如此,偶尔也会这样。通常我必须关闭整个应用程序以使ListView正确更新并显示对数据的任何更新。即使我要注销当前的解析用户,并在不关闭应用程序的情况下登录其他帐户,之前用户的帖子有时也会出现在列表视图中。我究竟做错了什么?我在iOS中工作时从未遇到过这样的问题。还注意到如果我要从Parse中删除一个Item并返回,通常该项仍然出现在ListView中。

1 个答案:

答案 0 :(得分:0)

你只需要打电话给onResume()

if(mOwnSalesAdapter!=null)
   mOwnSalesAdapter.notifyDataSetChanged();