ListView不会使用notifyDataSetChanged刷新

时间:2015-04-25 11:32:57

标签: android sqlite listview android-adapter

我正在尝试从适配器类(非活动类)刷新listview我尝试notifyDataSetChanged()但它不能工作,我也尝试在适配器类中调用displayListView()但它崩溃了以及

这是 ActivityClass

中的displayListView()方法
            Cursor cursor = dbHelper.fetchAllCountries();

            // The desired columns to be bound
            String[] columns = new String[]{
                   DBAdapter.qty,
                   DBAdapter.price,
                   DBAdapter.PRODUCT_NAME
            };

            // the XML defined views which the data will be bound to
            int[] to = new int[]{
                    R.id.qtyL,
                    R.id.priceL,
                    R.id.PRODUCT_NAMEL
            };

            // create the adapter using the cursor pointing to the desired data
            //as well as the layout information
            dataAdapter = new SimpleCursorAdapter(
                    this, R.layout.country_inf,
                    cursor,
                    columns,
                    to,
                    0);

            HorizontalListView listView = (HorizontalListView) findViewById(R.id.cartCeckOutList);
            // Assign adapter to ListView
                    listView.setAdapter(dataAdapter);
                    dataAdapter.notifyDataSetChanged();

这是适配器类我使用

        package com.abdullahadhaim.finegrillresturant.adater;



public class CustomListAdapter extends BaseAdapter {
Context c;
// DBHelper myDataBaseHelper ;
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

DBAdapter myADapterDB;

WaiterActivity waiterAct;

public CustomListAdapter(Activity activity, List<Movie> movieItems) {
    this.activity = activity;
    this.movieItems = movieItems;
}

@Override
public int getCount() {
    return movieItems.size();
}

@Override
public Object getItem(int location) {
    return movieItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);

    final TextView title = (TextView) convertView.findViewById(R.id.title);
    TextView price = (TextView) convertView.findViewById(R.id.rating);
    // myDataBaseHelper=new DBHelper(activity, "CDB", null, 1);
    myADapterDB = new DBAdapter(activity);
    myADapterDB.open();
    waiterAct = new WaiterActivity();

    // getting movie data for the row
    final Movie m = movieItems.get(position);

    // thumbnail image
    thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

    // title
    title.setText(m.getTitle());

    // price
    price.setText("Price: " + "SR " + String.valueOf(m.getPrice()));

    Button add = (Button) convertView.findViewById(R.id.button1);
    Button plus = (Button) convertView.findViewById(R.id.plus_butt);
    Button minus = (Button) convertView.findViewById(R.id.minus_butt);
    final EditText qty = (EditText) convertView
            .findViewById(R.id.editText1);

    plus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String the_qty = qty.getText().toString();
            int my_qty = Integer.parseInt(the_qty) + 1;
            qty.setText(my_qty + "");

        }
    });

    minus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String the_qty = qty.getText().toString();
            int my_qty = Integer.parseInt(the_qty) - 1;
            qty.setText(my_qty + "");

        }
    });

    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int myQTY = Integer.parseInt(qty.getText().toString());
            String myTitle = m.getTitle().toString();
            String myCategory = m.getCategory().toString();
            int myCategoryN = m.getOrderNum();
            double myPrice = m.getPrice() * myQTY;

            if (myQTY == 0)
                Toast.makeText(activity, "Orders Must be 1 Atleast",
                        Toast.LENGTH_SHORT).show();

            else {

                myADapterDB.checkIfExcist(activity, "productName", myTitle,
                        myQTY, myPrice, m.getPrice(), myTitle, myCategory,
                        myCategoryN + "", myQTY);
                myADapterDB.deleteZeroOrLessValues();


            }

        }
    });

    return convertView;
}

public void notifyDataSetChanged() {
    // TODO Auto-generated method stub
    super.notifyDataSetChanged();
}


}

1 个答案:

答案 0 :(得分:0)

看看装载机。我认为这是将数据从datebase加载到视图中的首选方式。如果数据已更改并且视图已为您更新,则使用Loaders可以获得回调。

http://developer.android.com/guide/components/loaders.html

我认为这是一个很好的教程: http://code.tutsplus.com/tutorials/android-fundamentals-properly-loading-data--mobile-5673