Android 清空ListView

Android ListView clear

我有一个 ListView。我想动态更改 ListView 的内容。我使用适配器将值传递给它。当我传递它接受并显示的值时,但是当我按下后退按钮并传递新数据时,它仍然显示旧数据和新数据。

我试过了

arraylist.clear() , adapter_object.notifyDataSetChanged(); , listview.setAdapter(null);

但其中 none 有效。

下面是我如何将值传递给 getter 和 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);

        }

这是适配器代码

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

这是gettersetterclass

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;


}
}

我哪里错了

编辑-1 ACTIVITY 设置值

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


}


}
public ArrayList<Flight_info_details> flight_one_way_detail; 

flight_one_way_detail 是静态的,不应该是静态的。

作为静态概念,只有 flight_one_way_detail 的一个对象是为您的 activity 初始化的,因此即使从其他 activity 返回后,您仍然会得到该对象并且您正在添加新的对象里面有航班详细信息,所以它显示了旧的和新的

另外 @Raghavendra 建议创建新的适配器不应该在循环中。

您应该只调用 new Flight_info_adapter() 一次。我建议先创建 ArrayList 然后将其传递给 Flight_info_adapter() 构造函数:

ArrayList<Flight_info_detail> flight_one_way_detail = new ArrayList<>();
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);

创建列表后无需立即调用clear()。新列表保证为空。

请注意,适配器中的值仅存储在内存中。如果用户退出您的应用程序,所有数据都将丢失。为了更永久地保存数据,我建议您创建一个 SQLite 数据库并使用 CursorAdapter。有几个很好的在线教程和博客可以说明如何执行此操作。

我不明白您设置适配器的逻辑究竟是什么。但是你永远不应该在循环中创建适配器,所以更正下面的内容。

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

第二件事是onbackPressed()。正如我所看到的,您没有完成单击后退按钮上的当前 activity,这是为什么?我认为你应该完成你的 activity 并且在这种情况下不需要重置适配器。
或者,如果您想保留它,way.Then 只需清除列表即可重置适配器。

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

首先,在您的适配器中,您的数组列表不应是静态的。

其次,为您的 activity 创建私有方法。像这样-

private ArrayList<modelclass> getdata(){

        ArrayList<modelclass> flight_one_way_detail = new ArrayList<>() ;
        flight_one_way_detail.clear();
        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()));
        return list ;
    }

然后将数组列表从 onCreate 方法传递给您的适配器。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.my_list_view);
        listViewAdapter = new ListViewAdapter(this, getdata()) ;
        listView.setAdapter(listViewAdapter);
    }

希望通过这两个步骤,你会得到你想要的结果。如果没有发生,则从适配器的构造函数中清除数组列表。像这样-

public ListViewAdapter(Activity activity, ArrayList<modelclass> list){
        this.list.clear();
        this.activity=activity;
        this.list=list;
        inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

但是,这不是一个好的做法。因为,每次您将全新的数组列表传递给您的适配器时。当且仅当您没有得到想要的结果时,您才可以从构造函数中清除列表。

首先,您不应该在 onBackPressed() 函数中启动新的活动。它的基本功能是return到前面的activity。在这个函数之后,之前 activity 的 onResume() 函数运行,你应该更新你的列表和适配器。