I have a ListView. I want to dynamically change the contents of the ListView. I used adapter to pass the values to it. when I pass the values it accepts and displays but when I press back button and pass new data it still displays old data and along with new data.
I have tried
arraylist.clear() , adapter_object.notifyDataSetChanged(); , listview.setAdapter(null);
but none of those worked.
Here's how I pass values to getter and setter class
for (int i = 0; i <FlightTboSearch.flight_name_jeet.size(); i++) {
flight_one_way_detail.add(new Flight_info_details(FlightTboSearch.flight_name_jeet.get(i).toString(), FlightTboSearch.flight_departure_jeet.get(i), FlightTboSearch.flight_arrival_jeet.get(i), FlightTboSearch.flight_name_jeet.get(i).toString()));
flight_one_way_adapter = new Flight_info_adapter(View_fligh_info.this, flight_one_way_detail);
}
Here's the adapter code
public class Flight_info_adapter extends BaseAdapter {
Context context;
public static ArrayList<Flight_info_details> rowItems;
Flight_info_adapter(Context context, ArrayList<Flight_info_details> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position)
{
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
TextView flight;
TextView departur;
TextView arrival;
ImageView img_logo;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.flight_row, null);
holder = new ViewHolder();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.flight = (TextView) convertView.findViewById(R.id.flight);
holder.departur = (TextView) convertView.findViewById(R.id.departure);
holder.arrival = (TextView) convertView.findViewById(R.id.arrival);
holder.img_logo = (ImageView) convertView.findViewById(R.id.img_logo);
try
{
final Flight_info_details row_pos = rowItems.get(position);
String flight = String.valueOf(row_pos.getflight());
String departur=String.valueOf(row_pos.getdeparture());
String arrival=String.valueOf(row_pos.getarrival());
String flight_name=String.valueOf(row_pos.getflight_name());
if(flight_name.equals("SpiceJet"))
{
holder.img_logo.setImageBitmap(View_fligh_info.spicejet);
}else if(flight_name.equals("Indigo"))
{
holder.img_logo.setImageBitmap(View_fligh_info.indigo);
}else if(flight_name.equals("Jet Airways"))
{
Log.e("confirmation_Adapter-",flight_name);
holder.img_logo.setImageBitmap(View_fligh_info.jetairways);
}else if(flight_name.equals("Air India"))
{
holder.img_logo.setImageBitmap(View_fligh_info.ai);
}else if(flight_name.equals("Klm Uk"))
{
holder.img_logo.setImageBitmap(View_fligh_info.uk);
}else if(flight_name.equals("Air Asia"))
{
holder.img_logo.setImageBitmap(View_fligh_info.airasia);
}
holder.flight.setText(flight);
holder.departur.setText(departur);
holder.arrival.setText(arrival);
}
catch (Exception e)
{
Log.e("PASS_ADAP ERROR:", e.getMessage());
}
convertView.setTag(holder);
return convertView;
}
}
Here's the getter setter class
public class Flight_info_details {
String flight;
String departure;
String arrival;
String flight_name;
public String getflight(){return flight;}
public void setflight() {
this.flight = flight;
}
public String getdeparture() {return departure;}
public void setdeparture() {this.departure = departure;}
public String getarrival() {return arrival;}
public void setarrival() {this.arrival = arrival;}
public String getflight_name() {return flight_name;}
public void setflight_name() {this.flight_name = flight_name;}
public Flight_info_details(String flight, String departure, String arrival, String flight_name)
{
this.flight = flight;
this.departure=departure;
this.arrival=arrival;
this.flight_name=flight_name;
}
}
where am I going wrong
EDIT-1 ACTIVITY to set values
public class View_fligh_info extends AppCompatActivity{
ArrayList<ViewFlightDetails> newUsers;
public static Bitmap spicejet,indigo,ai,uk,SaudiArabianAirlines,EthiopianAirlinesSC,KenyaAirways,ThaiAirwaysInternational,jetairways,airasia,HahnAirSystems,TurkishAirlines,AirMauritius,EmiratesAirlines,OmanAviation,SriLankanAirlines,Alitalia,EtihadAirways,AirFacilities,GulfAir;
TextView return_text;
Button next;
LinearLayout retur_text_2;
ListView flight_information,return_list;
public static ArrayList<Flight_info_details> flight_one_way_detail;
public static Flight_info_adapter flight_one_way_adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.view_flight_info);
return_text=(TextView)findViewById(R.id.return_text);
next=(Button)findViewById(R.id.next);
flight_information = (ListView) findViewById(R.id.list_oneway);
spicejet = BitmapFactory.decodeResource(this.getResources(),R.drawable.spice);
indigo = BitmapFactory.decodeResource(this.getResources(),R.drawable.indigo);
ai = BitmapFactory.decodeResource(this.getResources(),R.drawable.ai);
uk = BitmapFactory.decodeResource(this.getResources(),R.drawable.uk);
jetairways = BitmapFactory.decodeResource(this.getResources(),R.drawable.jet);
airasia = BitmapFactory.decodeResource(this.getResources(),R.drawable.airasia);
HahnAirSystems = BitmapFactory.decodeResource(this.getResources(),R.drawable.h1);
Alitalia = BitmapFactory.decodeResource(this.getResources(),R.drawable.az);
EtihadAirways = BitmapFactory.decodeResource(this.getResources(),R.drawable.ey);
AirFacilities = BitmapFactory.decodeResource(this.getResources(),R.drawable.fz);
GulfAir = BitmapFactory.decodeResource(this.getResources(),R.drawable.gf);
SriLankanAirlines = BitmapFactory.decodeResource(this.getResources(),R.drawable.ul);
OmanAviation = BitmapFactory.decodeResource(this.getResources(),R.drawable.wy);
EmiratesAirlines = BitmapFactory.decodeResource(this.getResources(),R.drawable.ek);
AirMauritius = BitmapFactory.decodeResource(this.getResources(),R.drawable.mk);
TurkishAirlines = BitmapFactory.decodeResource(this.getResources(),R.drawable.tk);
EthiopianAirlinesSC = BitmapFactory.decodeResource(this.getResources(),R.drawable.et);
KenyaAirways = BitmapFactory.decodeResource(this.getResources(),R.drawable.kq);
ThaiAirwaysInternational = BitmapFactory.decodeResource(this.getResources(),R.drawable.tg);
SaudiArabianAirlines = BitmapFactory.decodeResource(this.getResources(),R.drawable.sv);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(View_fligh_info.this,"HELLO WORLD",Toast.LENGTH_LONG).show();
}
});
if (FlightHome.oneway.isChecked())
{
Toast.makeText(View_fligh_info.this,"Oneway Trip",Toast.LENGTH_LONG).show();
return_text.setVisibility(View.GONE);
retur_text_2.setVisibility(View.GONE);
//flight_information.setAdapter(null);
flight_one_way_detail =new ArrayList<Flight_info_details>();
flight_one_way_detail.clear();
for (int i = 0; i <FlightTboSearch.flight_name_jeet.size(); i++) {
flight_one_way_detail.add(new Flight_info_details(FlightTboSearch.flight_name_jeet.get(i).toString(), FlightTboSearch.flight_departure_jeet.get(i), FlightTboSearch.flight_arrival_jeet.get(i), FlightTboSearch.flight_name_jeet.get(i).toString()));
flight_one_way_adapter = new Flight_info_adapter(View_fligh_info.this, flight_one_way_detail);
}
flight_one_way_adapter.notifyDataSetChanged();
flight_information.setAdapter(flight_one_way_adapter);
//flight_one_way_detail.getAdapter().notify();
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(flight_information.getWidth(), View.MeasureSpec.AT_MOST);
for (int i = 0; i < flight_one_way_adapter.getCount(); i++) {
View listItem = flight_one_way_adapter.getView(i, null, flight_information);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = flight_information.getLayoutParams();
params.height = totalHeight + (flight_information.getDividerHeight() * (flight_one_way_adapter.getCount() - 1));
flight_information.setLayoutParams(params);
flight_information.requestLayout();
}
}
@Override
public void onBackPressed() {
flight_one_way_detail.clear();
// flight_information.setAdapter(null);
Intent i=new Intent(View_fligh_info.this,FlightTboSearch.class);
startActivity(i);
}
}
I don't what exactly your logic is to setting adapter . But you should never create adapter inside loop so correct the things below .
for (int i = 0; i <FlightTboSearch.flight_name_jeet.size(); i++) {
flight_one_way_detail.add(new Flight_info_details(FlightTboSearch.flight_name_jeet.get(i).toString(), FlightTboSearch.flight_departure_jeet.get(i), FlightTboSearch.flight_arrival_jeet.get(i), FlightTboSearch.flight_name_jeet.get(i).toString()));
}
flight_one_way_adapter = new Flight_info_adapter(View_fligh_info.this, flight_one_way_detail);
flight_information.setAdapter(flight_one_way_adapter);
Second thing is onbackPressed()
. As i am seeing you did not finish the current activity on back button click why is that ? I think you should finish your activity and there is is no need to reseting adapter in this case .
Or if you want to keep it that way.Then just clearing the list will reset the adapter .
@Override
public void onBackPressed() {
if(flight_one_way_adapter!=null){
flight_one_way_detail.clear();
flight_one_way_adapter.notifyDataSetChanged();
}
Intent i=new Intent(View_fligh_info.this,FlightTboSearch.class);
startActivity(i);
}