自定义适配器notifyDataSetChanged无法正常工作

时间:2014-11-20 22:17:52

标签: android android-listview android-adapter

我试着致电adapter.notifydatasetchanged(),但它根本不起作用。如何刷新列表?

public class CarListMenu extends Fragment{


    public CarListAdapter adapter= null;
    static ArrayList<carArrayItem> CarPriceItem ;
    private static ListView CarListView;



     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            CarListView = (ListView) inflater.inflate(R.layout.car_price_list_menu, container, false);

            CarPriceItem.add(new carArrayItem("No","No","NO","NO" , "NO"));

            adapter = new CarListAdapter(getActivity().getApplicationContext(),CarPriceItem);
            CarListView.setAdapter(adapter);
            return CarListView;
        }

     public static class App extends Application{

            private static Context mContext;

            @Override
            public void onCreate() {
                super.onCreate();
                mContext = this;
            }

            public static Context getContext(){
                return mContext;
            }
        }


public static void change(ArrayList<HashMap<String, String>> resultback){



    CarPriceItem = new ArrayList<carArrayItem>();
    //CarPriceItem.clear();

    for (int i = 0; i < (resultback.size()); i+=6) {

        CarPriceItem.add(new carArrayItem((resultback.get(i).get("table").toString()),
                (resultback.get(i+1).get("table").toString()),
                (resultback.get(i+2).get("table").toString()), 
                (resultback.get(i+3).get("table").toString()), 
                (resultback.get(i+5).get("table").toString())));

        }

    CarListAdapter adapter =  new CarListAdapter(App.getContext(),CarPriceItem);
    adapter.notifyDataSetChanged();

    } 




}
class CarListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<carArrayItem> CarPriceItem = new ArrayList<carArrayItem>();

    public CarListAdapter(Context context, ArrayList<carArrayItem> CarPriceItem){
        this.context = context;
        this.CarPriceItem = CarPriceItem;

    }



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

    @Override
    public Object getItem(int position) {       
        return CarPriceItem.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.car_list_item, null);
        }


        TextView CarmodelName = (TextView) convertView.findViewById(R.id.CarModel);
        TextView MarketP = (TextView) convertView.findViewById(R.id.MarketPriceData);
        TextView ResellerP = (TextView) convertView.findViewById(R.id.ResellerPricesData);
        TextView Description = (TextView) convertView.findViewById(R.id.DescriptionData);
        TextView LastUpdate = (TextView) convertView.findViewById(R.id.LastUpdateData);


        CarmodelName.setText(CarPriceItem.get(position).getCarname());
        MarketP.setText(CarPriceItem.get(position).getMarketPrice());
        ResellerP.setText(CarPriceItem.get(position).getResellerPrice());
        Description.setText(CarPriceItem.get(position).getDescription());
        LastUpdate.setText(CarPriceItem.get(position).getUpdateTime());



        return convertView;
    }


}

3 个答案:

答案 0 :(得分:5)

最终代码:工作正常。感谢@Justin Powell

public static void change(ArrayList<HashMap<String, String>> resultback){

    ArrayList<carArrayItem> CarPriceItemnew = new ArrayList<carArrayItem>();

    for (int i = 0; i < (resultback.size()); i+=6) {

            CarPriceItemnew.add(new carArrayItem((resultback.get(i).get("table").toString()),
            (resultback.get(i+1).get("table").toString()),
            (resultback.get(i+2).get("table").toString()), 
            (resultback.get(i+3).get("table").toString()), 
            (resultback.get(i+5).get("table").toString())));

    }

    CarPriceItem.clear();
    CarPriceItem.addAll(CarPriceItemnew);
    adapter.notifyDataSetChanged(); 

} 

答案 1 :(得分:1)

您正在change()中创建新适配器,但您没有将其分配给ListView。你需要做两件事之一:

  1. 使用CarListView.setAdapter(adapter);方法中的change()将新适配器分配给ListView,或
  2. 更新适配器中的数据,而无需重新创建。然后,您可以致电adapter.notifyDataSetChanged()刷新列表。

答案 2 :(得分:0)

下面给出了经过测试和完美工作的代码,

在适配器中创建一个函数,如

public void notify(ArrayList<String> list){
        this.list= list;
        notifyDataSetChanged();
    }

并在必须通知时从Activity调用该函数。

adapter.notify(list);