自定义适配器notifyDataSetChanged不工作

问题描述:

我试图拨打adapter.notifydatasetchanged(),但它根本不起作用。我如何刷新列表?自定义适配器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; 
    } 


} 
+0

如果您希望其他人查看您的代码,请尝试遵循常规的java命名约定。类名称如'carArrayItem' UpperCamelCase,变量如'CarPriceItem' lowerCamelCase。 – zapl 2014-11-20 22:55:25

最终代码:它工作正常。感谢@Justin鲍威尔

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

} 

您正在创建一个新的适配器change(),但您并未将其分配给ListView。你需要做两件事情之一:

  1. 无论是在你的change()方法新的适配器分配使用CarListView.setAdapter(adapter);您的ListView,或
  2. 更新您的适配器数据而无需重新创建它。然后您可以拨打adapter.notifyDataSetChanged()刷新列表。
+0

我试过第一个,它不起作用。 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 第二个:请您解释更多?我无法得到它。 谢谢 – soroosh 2014-11-21 00:59:45

+0

这意味着您的代码中您的ListView引用为空。何时/在哪里调用'change()'? – 2014-11-21 15:19:22

测试并低于完美的工作代码给出,

适配器一样创建一个功能,

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

,并从活动调用的函数,只要你有通知。

adapter.notify(list);