soroosh soroosh - 4 years ago 166
Android Question

Custom adapter notifyDataSetChanged not working

I tried to call

adapter.notifydatasetchanged()
but it doesn't work at all. How can I refresh the list?

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;
}


}

Answer Source

Final code: It works fine. thanks to @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(); 

} 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download