我有一个列表,显示每行多个项目。 两个按钮和四个textview。
我正在使用ArrayAdapter来修改我的自定义布局并填写每行中的数据。
显示所有想要的信息,我可以与按钮进行交互。 (每个按钮更改其行的文本视图。)
一切正常,直到我再次向下滚动。比什么都没有了。 所有项目仍然存在,但通过单击按钮,文本不再更改。
(我将所有创建的TextView保存在Vector中,因为使用getView方法的position属性,我得到了我在列表中单击的项目的位置,因此我可以从向量中选择一个特定的一个。它可以很好地工作,直到as我写道,我滚动列表)
这是我的ArrayAdapter的代码
public class LocationlistAdapter extends ArrayAdapter<Location> {
// GUI elements
private Button btnCheckin;
private Button btnExit;
private Vector<TextView> locationList = new Vector<TextView>();
private Vector<TextView> checkInList = new Vector<TextView>();
private Vector<TextView> checkOutList = new Vector<TextView>();
private Vector<TextView> lastVisitList = new Vector<TextView>();
private boolean checkedIn = false;
private int activeListPosition = -1; // selected ListView item
private static LocationTrackingDB mDBHandler;
public LocationlistAdapter(Context context, List<Location> locations) {
super(context, R.layout.locationlistrow, locations);
mDBHandler = LocationTrackingDB.getInstance(getContext());
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.locationlistrow, null);
if (position % 2 == 0) {
RelativeLayout layout = (RelativeLayout) v
.findViewById(R.id.rowLayout);
layout.setBackgroundColor(Color.DKGRAY);
} else {
RelativeLayout layout = (RelativeLayout) v
.findViewById(R.id.rowLayout);
layout.setBackgroundColor(Color.BLACK);
}
TextView txtLocationName = (TextView) v
.findViewById(R.id.txtLocationName);
TextView txtStartTime = (TextView) v.findViewById(R.id.txtStartTime);
TextView txtEndTime = (TextView) v.findViewById(R.id.txtEndTime);
TextView txtLastDate = (TextView) v.findViewById(R.id.txtLastDate);
if (locationList.size() < mDBHandler.getAllLocations().size()) {
locationList.add(txtLocationName);
checkInList.add(txtStartTime);
checkOutList.add(txtEndTime);
lastVisitList.add(txtLastDate);
}
txtLocationName.setText(mDBHandler.getLocation(position + 1).getName());
txtStartTime.setText(mDBHandler.getLocation(position + 1)
.getStartTime());
txtEndTime.setText(mDBHandler.getLocation(position + 1).getEndTime());
txtLastDate
.setText(mDBHandler.getLocation(position + 1).getLastVisit());
btnCheckin = (Button) v.findViewById(R.id.btnCheckIn);
btnExit = (Button) v.findViewById(R.id.btnExit);
btnCheckin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!checkedIn && activeListPosition == -1) {
checkedIn = true;
activeListPosition = position;
Location tmpLocation = mDBHandler.getLocation(position + 1);
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
Date currentTime = new Date();
checkInList.elementAt(position).setText(
time.format(currentTime));
lastVisitList.elementAt(position).setText(
date.format(currentTime));
checkOutList.elementAt(position).setText("waiting ...");
tmpLocation.setStartTime(time.format(currentTime));
tmpLocation.setLastVisit(date.format(currentTime));
mDBHandler.updateLocation(tmpLocation);
}
}
});
btnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (checkedIn && activeListPosition == position) {
Location tmpLocation = mDBHandler.getLocation(position + 1);
checkedIn = false;
activeListPosition = -1;
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
Date currentTime = new Date();
checkOutList.elementAt(position).setText(
time.format(currentTime));
tmpLocation.setEndTime(time.format(currentTime));
tmpLocation.setLastVisit(date.format(currentTime));
mDBHandler.updateLocation(tmpLocation);
}
}
});
return v;
}
}
有没有人知道为什么会这样?或者我如何才能做到更好的方式呢?
答案 0 :(得分:0)
尝试使用getView函数参数中的convertView(在您的情况下为视图),如:
@Override
public View getView(final int position, View view, ViewGroup parent) {
View v=view;
if(v==null){
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.locationlistrow, null);
}
...
...
}
还尝试使用此适配器代码:
public class LocationlistAdapter extends ArrayAdapter<Location> {
// GUI elements
private Button btnCheckin;
private Button btnExit;
private boolean checkedIn = false;
private int activeListPosition = -1; // selected ListView item
private static LocationTrackingDB mDBHandler;
public LocationlistAdapter(Context context, List<Location> locations) {
super(context, R.layout.locationlistrow, locations);
mDBHandler = LocationTrackingDB.getInstance(getContext());
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.locationlistrow, null);
}
if (position % 2 == 0) {
RelativeLayout layout = (RelativeLayout) v
.findViewById(R.id.rowLayout);
layout.setBackgroundColor(Color.DKGRAY);
} else {
RelativeLayout layout = (RelativeLayout) v
.findViewById(R.id.rowLayout);
layout.setBackgroundColor(Color.BLACK);
}
final TextView txtLocationName = (TextView) v
.findViewById(R.id.txtLocationName);
final TextView txtStartTime = (TextView) v
.findViewById(R.id.txtStartTime);
final TextView txtEndTime = (TextView) v.findViewById(R.id.txtEndTime);
final TextView txtLastDate = (TextView) v
.findViewById(R.id.txtLastDate);
txtLocationName.setText(mDBHandler.getLocation(position + 1).getName());
txtStartTime.setText(mDBHandler.getLocation(position + 1)
.getStartTime());
txtEndTime.setText(mDBHandler.getLocation(position + 1).getEndTime());
txtLastDate
.setText(mDBHandler.getLocation(position + 1).getLastVisit());
btnCheckin = (Button) v.findViewById(R.id.btnCheckIn);
btnExit = (Button) v.findViewById(R.id.btnExit);
btnCheckin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!checkedIn && activeListPosition == -1) {
checkedIn = true;
activeListPosition = position;
Location tmpLocation = mDBHandler.getLocation(position + 1);
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
Date currentTime = new Date();
txtLocationName.setText(time.format(currentTime));
txtLastDate.setText(date.format(currentTime));
txtEndTime.setText("waiting ...");
tmpLocation.setStartTime(time.format(currentTime));
tmpLocation.setLastVisit(date.format(currentTime));
mDBHandler.updateLocation(tmpLocation);
}
}
});
btnExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (checkedIn && activeListPosition == position) {
Location tmpLocation = mDBHandler.getLocation(position + 1);
checkedIn = false;
activeListPosition = -1;
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
Date currentTime = new Date();
txtEndTime.setText(time.format(currentTime));
tmpLocation.setEndTime(time.format(currentTime));
tmpLocation.setLastVisit(date.format(currentTime));
mDBHandler.updateLocation(tmpLocation);
}
}
});
return v;
}
}