簡體   English   中英

ListView不會使用notifyDataSetChanged刷新

[英]ListView won't refresh using notifyDataSetChanged

我正在嘗試從適配器類(非活動類)刷新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();
}


}

看一下裝載機。 我認為這是將數據從數據庫加載到視圖中的首選方法。 使用裝載程序,如果數據已更改並且視圖已為您更新,則將獲得回調。

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

我認為這是一個很好的教程: http : //code.tutsplus.com/tutorials/android-fundamentals-properly-loading-data--mobile-5673

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM