【发布时间】:2017-12-28 04:59:37
【问题描述】:
我有一个列表视图。我想动态更改 ListView 的内容。我使用适配器将值传递给它。当我传递它接受并显示的值时,但当我按下返回按钮并传递新数据时,它仍然显示旧数据以及新数据。
我试过了
arraylist.clear() , adapter_object.notifyDataSetChanged(); , listview.setAdapter(null);
但这些都不起作用。
这是我将值传递给 getter 和 setter 类的方式
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;
}
}
这是 getter setter 类
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;
}
}
我哪里错了
EDIT-1 设定值的活动
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);
}
}
【问题讨论】:
-
请同时发布您的活动代码。
-
哪个活动 xml 文件?@SanjayKakadiya
-
不是 xml。从您设置适配器的位置发布 java 代码。
-
@Newlearner 我不知道你的逻辑,但为什么要在 for 循环中初始化适配器?
-
检查更多的可能性,或者值我是多个所以我添加了 for 循环。@Raghavendra
标签: android listview adapter getter-setter