Android ArrayList丢失数据
在我的应用程序中,我有一个带有Android ArrayList丢失数据,android,android-listview,arraylist,baseadapter,Android,Android Listview,Arraylist,Baseadapter,在我的应用程序中,我有一个带有复选框的列表视图,并且有一个扩展BaseAdapter的适配器来填充列表视图。列表的基本功能是显示债务,用户可以通过复选框选择要支付的债务。当用户添加/删除某个项目时,列表底部的总额会更新。现在,一些债务与另一个债务相关,如果用户选中一个债务,那么任何其他相关债务也应该被标记,如果取消选中债务,则标记相同。我还有一个按钮,可以清除列表中所有选中的债务,这就是我的问题所在 我将所选债务记录在ArrayList上,除了“清除”按钮外,它似乎对所有程序都有效。按下按钮时,
复选框的列表视图
,并且有一个扩展BaseAdapter
的适配器来填充列表视图
。列表的基本功能是显示债务,用户可以通过复选框选择要支付的债务。当用户添加/删除某个项目时,列表底部的总额会更新。现在,一些债务与另一个债务相关,如果用户选中一个债务,那么任何其他相关债务也应该被标记,如果取消选中债务,则标记相同。我还有一个按钮,可以清除列表中所有选中的债务,这就是我的问题所在
我将所选债务记录在ArrayList
上,除了“清除”按钮外,它似乎对所有程序都有效。按下按钮时,所选债务列表似乎始终为空。知道会发生什么吗
这是我的适配器:
public class ServicesFinancialStatusAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener{
private Context context;
private List<Debts> debtsList;
private List<Debts> selectedDebts;
private LayoutInflater inflater;
private int tabPosition;
private float total;
private OnTotalChangedListener listener;
public ServicesFinancialStatusAdapter(Context context, List<Debts> debtsList, int tabPosition) {
this.context = context;
this.debtsList = debtsList;
this.tabPosition = tabPosition;
this.total = 0;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.selectedDebts = new ArrayList<Debts>();
}
@Override
public int getCount() {
return debtsList.size();
}
@Override
public Object getItem(int i) {
return debtsList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.item_services_financial_status, viewGroup, false);
holder.concept = (TextView) view.findViewById(R.id.payment_concept);
holder.descriptionOrDate = (TextView) view.findViewById(R.id.payment_description_date);
holder.amount = (TextView) view.findViewById(R.id.payment_amount);
holder.expirationDate = (TextView) view.findViewById(R.id.payment_expiration_date);
if (tabPosition > 4) {
holder.checkBox = (CheckBox) view.findViewById(R.id.check_box);
holder.checkBox.setOnCheckedChangeListener(this);
}
view.setTag(holder);
} else
holder = (ViewHolder) view.getTag();
Debts item = debtsList.get(i);
holder.concept.setText(item.getConcept());
holder.amount.setText(item.getAmountToString());
if (item.isExpired())
holder.expirationDate.setText(context.getString(R.string.expired_status_indicator));
else
holder.expirationDate.setText(context.getString(R.string.debts_expiration_date_indicator) + item.getExpirationDate());
if (tabPosition < 3)
holder.descriptionOrDate.setText(item.getDescription());
else if (tabPosition < 5)
holder.descriptionOrDate.setText(item.getDate());
else {
holder.descriptionOrDate.setVisibility(View.GONE);
holder.checkBox.setVisibility(View.VISIBLE);
holder.checkBox.setTag(i);
holder.checkBox.setChecked(item.isSelected());
}
return view;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Get the position of the item clicked and the data object
Integer position = (Integer) buttonView.getTag();
Debts item = debtsList.get(position);
//Change the status ob the object
item.setSelected(isChecked);
for (Debts debts : debtsList) {
//Check on the list for related objects and marks them as well
if (debts.getConceptId() == item.getRelatedDebt())
debts.setSelected(isChecked);
//Get the amount of the debt and add/remove it from
//the selectedDebts list and update the total
float amount = debts.getAmount();
if (debts.isSelected()) {
if (!selectedDebts.contains(debts)) {
selectedDebts.add(debts);
listener.onTotalChanged(addToTotal(amount), selectedDebts);
}
}
else {
if (selectedDebts.contains(debts)) {
selectedDebts.remove(debts);
listener.onTotalChanged(removeFromTotal(amount), selectedDebts);
}
}
}
//Finally update the UI
notifyDataSetChanged();
}
//Anywhere else in the code selectedDebts has the right data but here
//Here the size of the list is always 0
public void unMarkDebts() {
for (Debts debts : debtsList) {
//Get the amount of the debt and remove it from
//the selectedDebts list, set the data object as unselected
//and update the total
float amount = debts.getAmount();
if (selectedDebts.contains(debts)) {
debts.setSelected(false);
selectedDebts.remove(debts);
listener.onTotalChanged(removeFromTotal(amount), selectedDebts);
}
}
//Update the UI
notifyDataSetChanged();
}
private float addToTotal(float value) {
return total += value;
}
private float removeFromTotal(float value) {
return total -=value;
}
public interface OnTotalChangedListener{
public void onTotalChanged(float total, List<Debts> selectedDebts);
}
public void setOnTotalChangedListener(OnTotalChangedListener listener) {
this.listener = listener;
}
private class ViewHolder {
TextView concept, descriptionOrDate, amount, expirationDate;
CheckBox checkBox;
}
}
公共类服务FinancialStatusAdapter扩展BaseAdapter实现CompoundButton.OnCheckedChangeListener{
私人语境;
私人债务清单;
私人列表选择的电子标签;
私人充气机;
私人职位;
私人浮动总额;
私有OnTotalChangedListener侦听器;
公共服务FinancialStatusAdapter(上下文上下文、列表债务列表、int选项卡位置){
this.context=上下文;
this.debtsList=debtsList;
this.tabPosition=tabPosition;
这个总数=0;
this.inflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u inflater\u SERVICE);
this.selectedDebts=newarraylist();
}
@凌驾
public int getCount(){
返回debtsList.size();
}
@凌驾
公共对象getItem(int i){
返回债务人列表。获取(i);
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
视窗座;
如果(视图==null){
holder=新的ViewHolder();
视图=充气机。充气(R.layout.item\u services\u financial\u status,viewGroup,false);
holder.concept=(TextView)view.findViewById(R.id.payment\u concept);
holder.descriptionOrDate=(TextView)view.findViewById(R.id.payment\u description\u date);
holder.amount=(TextView)view.findViewById(R.id.payment\u amount);
holder.expirationDate=(TextView)view.findViewById(R.id.payment\u expiration\u date);
如果(位置>4){
holder.checkBox=(checkBox)view.findViewById(R.id.checkBox);
holder.checkBox.setOnCheckedChangeListener(此);
}
视图.设置标签(支架);
}否则
holder=(ViewHolder)view.getTag();
债务项目=债务列表获取(i);
holder.concept.setText(item.getConcept());
holder.amount.setText(item.getAmountToString());
if(item.isExpired())
holder.expirationDate.setText(context.getString(R.string.expired_status_indicator));
其他的
holder.expirationDate.setText(context.getString(R.string.debts\u expiration\u date\u indicator)+item.getExpirationDate());
如果(位置<3)
holder.descriptionOrDate.setText(item.getDescription());
否则如果(位置<5)
holder.descriptionOrDate.setText(item.getDate());
否则{
holder.descriptionOrDate.setVisibility(视图已消失);
holder.checkBox.setVisibility(View.VISIBLE);
holder.checkBox.setTag(i);
holder.checkBox.setChecked(item.isSelected());
}
返回视图;
}
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
//获取单击的项目和数据对象的位置
整数位置=(整数)按钮视图.getTag();
债务项目=债务列表获取(位置);
//更改对象的状态
项目.已选择(已检查);
用于(债务:债务列表){
//检查列表中的相关对象,并对其进行标记
if(debnts.getConceptId()==item.getRelatedDebt())
债务。已选择(已检查);
//获取债务金额并从中添加/删除
//显示selectedDebts列表并更新总数
浮动金额=债务。getAmount();
if(deborts.isSelected()){
如果(!selectedDebts.contains(债务)){
选择债务。添加(债务);
listener.onTotalChanged(AddToToTotal(金额),selectedDebts);
}
}
否则{
如果(所选债务包含(债务)){
选择债务。移除(债务);
listener.onTotalChanged(从总计(金额)中删除),selectedDebts;
}
}
}
//最后更新UI
notifyDataSetChanged();
}
//所选代码中的任何其他地方都有正确的数据,但此处除外
//这里列表的大小始终为0
公共债务无效{
用于(债务:债务列表){
//得到债务的数额,并将其从
//在selectedDebts列表中,将数据对象设置为未选中
//并更新总数
浮动金额=债务。getAmount();
如果(所选债务包含(债务)){
债务(虚假);
选择债务。移除(债务);
listener.onTotalChanged(从总计(金额)中删除),selectedDebts;
}
}
//更新用户界面
notifyDataSetChanged();
}
专用浮点加总(浮点值){
返回总+=值;
}
私有浮动从总计中移除(浮动值){
返回总计-=值;
}
公共接口OnTotalChangedListener{
已更改的公共void onTotalChanged(浮动总计,列表选择的debts);
}
public class CheckoutFragment extends BaseFragment implements View.OnClickListener, ServicesFinancialStatusAdapter.OnTotalChangedListener {
public static final String TAG = CheckoutFragment.class.getSimpleName();
private List<Debts> debtsList;
private List<Debts> selectedDebts;
private ServicesFinancialStatusAdapter adapter;
private TextView totalView, positiveBalanceView;
private View noDebtsView, header, footer;
private LinearLayout mainLayout;
private float total, positiveBalance;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
debtsList = new ArrayList<Debts>();
adapter = new ServicesFinancialStatusAdapter(getActivity(), debtsList, 5);
adapter.setOnTotalChangedListener(this);
webServices = ((MainActivity) getActivity()).getNetworkInstance();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Initialize the views
...
reloadData();
onTotalChanged(0, null);
return view;
}
@Override
public void onClick(View view) {
reloadData();
}
@Override
public void onTotalChanged(float total, List<Debts> selectedDebts) {
this.total = total == 0 ? total : total - positiveBalance;
this.selectedDebts = selectedDebts;
totalView.setText(getString(R.string.total_indicator) + Utilities.formatNumberAsMoney(this.total));
getActivity().invalidateOptionsMenu();
}
private void reloadData() {
debtsList.clear();
adapter = new ServicesFinancialStatusAdapter(getActivity(), debtsList, 5);
adapter.setOnTotalChangedListener(this);
loadData();
}
private void loadData() {
//Load debts from server
...
}
private void saveSelectedDebts() {
for (Debts selectedDebt : selectedDebts) {
long id = Debts.insert(getActivity(), selectedDebt);
//Log.d(TAG, "Inserted " + selectedDebt.getConcept() + " with ID " + id);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (!((MainActivity) getActivity()).drawerIsOpen) {
inflater.inflate(R.menu.checkout, menu);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.clear:
adapter.unMarkDebts();
break;
case R.id.checkout:
selectPaymentMode();
break;
}
return super.onOptionsItemSelected(item);
}
private void selectPaymentMode() {
...
}
}