我有一个ListView,它使用包含TextView的布局,TextView是一个分隔符。我有代码,所以如果分隔符应该显示,它将Visibility设置为View.VISIBLE,否则设置为View.GONE。这工作正常,但我注意到列表只是第一次加载正确。将列表滚动到底部并备份后,它会错误地显示/隐藏我的分隔符。我知道这是ListViews的已知编程错误,但我对如何处理它有点困惑。以下是我的代码。我该如何解决这个问题?
public class MaintenanceGrid extends Activity {
static final String KEY_DESCRIPTION = "description";
static final String KEY_SCHEDULEFLAG = "scheduleflag";
static final String KEY_LASTSERVICEDATE = "lastservicedate";
static final String KEY_LASTSERVICEMILEAGE = "lastservicemileage";
static final String KEY_LASTSERVICEVENDOR = "lastservicevendor";
static final String KEY_MILEAGEINTERVAL = "mileageinterval";
static final String KEY_CURRENTODOMETER = "currentodometer";
static final String KEY_MILEAGEOVERDUE = "mileageoverdue";
static final String KEY_NEXTMILEAGEDUE = "nextmileagedue";
AlertDialogManager alertDialog = new AlertDialogManager();
ArrayList<HashMap<String, String>> maintenanceGridList = new ArrayList<HashMap<String, String>>();
Globals globals;
MaintenanceGridData maintenanceGridData;
ProgressDialog progressDialog;
ListView list;
MaintenanceGridAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.maintenance_grid);
globals = (Globals) this.getApplication();
// Set our custom font
View rootView = findViewById(R.id.layoutMaintenanceGridContainer);
Globals.applyCustomFont((ViewGroup) rootView, FontClass.Arial(this));
// Execute main task to get the maintenance grid
new MaintenanceGridDetails().execute(globals.getCarID());
}
@Override
protected void onResume() {
super.onResume();
try {
globals = (Globals) this.getApplication();
if(globals.getDriverID()==null){
Intent i = new Intent(MaintenanceGrid.this, Splash.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
} catch(Exception e){
Intent i = new Intent(MaintenanceGrid.this, Splash.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
}
private class MaintenanceGridDetails extends AsyncTask<String, Integer, Integer>
{
static final int STATUS_SUCCESS = 1;
static final int STATUS_FAIL = 0;
static final int STATUS_NETWORK = 3;
static final int STATUS_ERROR = 2;
@Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(getParent(), "", getResources().getString(R.string.general_msg_please_wait));
}
@Override
protected Integer doInBackground(String... params)
{
if(!NetworkStatus.getInstance(getApplicationContext()).isOnline(getApplicationContext())) {
return STATUS_NETWORK;
}
maintenanceGridData = MaintenanceGridDetails(params[0]);
if(maintenanceGridData==null) { return STATUS_ERROR; }
if(maintenanceGridData.getMaintenanceGridDescription().size()!=0){
return STATUS_SUCCESS;
} else {
return STATUS_FAIL;
}
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Integer result)
{
if (result == STATUS_ERROR)
{
progressDialog.dismiss();
alertDialog.showAlertDialog(getParent(), getResources().getString(R.string.error), getResources().getString(R.string.error_connect), false);
}
else
if (result == STATUS_NETWORK)
{
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
progressDialog.dismiss();
Intent networkError = new Intent(MaintenanceGrid.this, NetworkError.class);
networkError.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(networkError);
finish();
}
}, 1000);
}
else
if (result == STATUS_FAIL )
{
progressDialog.dismiss();
alertDialog.showAlertDialog(getParent(), getResources().getString(R.string.error), getResources().getString(R.string.maintenance_grid_unavailable), false);
}
else
{
//set values in hashmap for listview
for(int x=0; x<maintenanceGridData.getMaintenanceGridDescription().size(); x++)
{
//create new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_DESCRIPTION, maintenanceGridData.getMaintenanceGridDescription().get(x));
map.put(KEY_SCHEDULEFLAG, maintenanceGridData.getMaintenanceGridScheduleFlag().get(x));
map.put(KEY_LASTSERVICEDATE, maintenanceGridData.getMaintenanceGridLastServiceDate().get(x));
map.put(KEY_LASTSERVICEMILEAGE, maintenanceGridData.getMaintenanceGridLastServiceMileage().get(x));
map.put(KEY_LASTSERVICEVENDOR, maintenanceGridData.getMaintenanceGridLastServiceVendor().get(x));
map.put(KEY_MILEAGEINTERVAL, maintenanceGridData.getMaintenanceGridMileageInterval().get(x));
map.put(KEY_CURRENTODOMETER, maintenanceGridData.getMaintenanceGridCurrentOdometer().get(x));
map.put(KEY_MILEAGEOVERDUE, maintenanceGridData.getMaintenanceGridMileageOverdue().get(x));
map.put(KEY_NEXTMILEAGEDUE, maintenanceGridData.getMaintenanceGridNextMileageDue().get(x));
//add history entry to main arraylist
maintenanceGridList.add(map);
}
list = (ListView) findViewById(R.id.listMaintenanceGrid);
// Get adapter by passing xml data ArrayList
adapter = new MaintenanceGridAdapter(getApplicationContext(), getParent(), maintenanceGridList);
list.setAdapter(adapter);
//close dialog
progressDialog.dismiss();
// If the Status Badge id visible, hide it
if (Main.statusTabBadge.getVisibility() == View.VISIBLE) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Main.statusTabBadge.setVisibility(View.GONE);
Globals.ackMaintenanceItemsDue=true;
}
}, 500);
}
}
}
}
public MaintenanceGridData MaintenanceGridDetails (final String carid) {
// method here gets the data for my adapter
}
}
适配器
static class ViewHolderItem {
TextView lblMaintenanceGridLastServiceDate, lblMaintenanceGridOdometer, lblMaintenanceGridCurrentOdometer, lblMaintenanceGridSeparator,
description, last_service_date, odometer, current_odometer;
ImageView imageMaintenanceItemIcon;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
View vi = convertView;
if(vi==null) {
vi = inflater.inflate(R.layout.maintenance_grid_item, parent, false);
viewHolder = new ViewHolderItem();
viewHolder.lblMaintenanceGridLastServiceDate = (TextView)vi.findViewById(R.id.lblMaintenanceGridLastServiceDate);
viewHolder.lblMaintenanceGridOdometer = (TextView)vi.findViewById(R.id.lblMaintenanceGridOdometer);
viewHolder.lblMaintenanceGridCurrentOdometer = (TextView)vi.findViewById(R.id.lblMaintenanceGridCurrentOdometer);
viewHolder.lblMaintenanceGridSeparator = (TextView)vi.findViewById(R.id.lblMaintenanceSeparator);
viewHolder.description = (TextView)vi.findViewById(R.id.lblMaintenanceGridDescription);
viewHolder.last_service_date = (TextView)vi.findViewById(R.id.txtMaintenanceGridLastServiceDate);
viewHolder.odometer = (TextView)vi.findViewById(R.id.txtMaintenanceGridOdometer);
viewHolder.current_odometer = (TextView)vi.findViewById(R.id.txtMaintenanceGridCurrentOdometer);
viewHolder.imageMaintenanceItemIcon = (ImageView)vi.findViewById(R.id.maintenanceGridItemIcon);
// Store the holder with the view
vi.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem) vi.getTag();
}
if(position % 2 == 0){
vi.setBackgroundResource(R.drawable.list_selector_even);
} else {
vi.setBackgroundResource(R.drawable.list_selector_odd);
}
// Make the items in this listview NOT clickable
vi.setEnabled(false);
vi.setOnClickListener(null);
HashMap<String, String> maintenanceGridItem = new HashMap<String, String>();
maintenanceGridItem = data.get(position);
if(data.get(position) != null) {
// Show/Hide the separator
if (!currentMileageInterval.equalsIgnoreCase(maintenanceGridItem.get(MaintenanceGrid.KEY_MILEAGEINTERVAL))) {
currentMileageInterval = maintenanceGridItem.get(MaintenanceGrid.KEY_MILEAGEINTERVAL);
viewHolder.lblMaintenanceGridSeparator.setText(context.getResources().getString(R.string.maintenance_grid_separator_text, currentMileageInterval));
viewHolder.lblMaintenanceGridSeparator.setVisibility(View.VISIBLE);
} else {
viewHolder.lblMaintenanceGridSeparator.setVisibility(View.GONE);
}
// Setting all values in listview
viewHolder.description.setText(maintenanceGridItem.get(MaintenanceGrid.KEY_DESCRIPTION));
viewHolder.last_service_date.setText(maintenanceGridItem.get(MaintenanceGrid.KEY_LASTSERVICEDATE));
viewHolder.odometer.setText(maintenanceGridItem.get(MaintenanceGrid.KEY_LASTSERVICEMILEAGE));
viewHolder.current_odometer.setText(maintenanceGridItem.get(MaintenanceGrid.KEY_CURRENTODOMETER));
// Set up the image
if (maintenanceGridItem.get(MaintenanceGrid.KEY_SCHEDULEFLAG).equalsIgnoreCase("overdue")) {
viewHolder.imageMaintenanceItemIcon.setImageResource(R.drawable.icon_alert_overdue);
} else if (maintenanceGridItem.get(MaintenanceGrid.KEY_SCHEDULEFLAG).equalsIgnoreCase("due")) {
viewHolder.imageMaintenanceItemIcon.setImageResource(R.drawable.icon_alert);
} else {
viewHolder.imageMaintenanceItemIcon.setImageResource(R.drawable.icon_check);
}
// Set our custom font
viewHolder.lblMaintenanceGridLastServiceDate.setTypeface(arialTypeface);
viewHolder.lblMaintenanceGridLastServiceDate.setTypeface(arialTypeface);
viewHolder.lblMaintenanceGridOdometer.setTypeface(arialTypeface);
viewHolder.lblMaintenanceGridCurrentOdometer.setTypeface(arialTypeface);
viewHolder.lblMaintenanceGridSeparator.setTypeface(arialTypeface, Typeface.BOLD);
viewHolder.description.setTypeface(arialTypeface, Typeface.BOLD);
viewHolder.last_service_date.setTypeface(arialTypeface);
viewHolder.odometer.setTypeface(arialTypeface);
viewHolder.current_odometer.setTypeface(arialTypeface);
}
return vi;
}