删除行时,Android列表视图更改文本

时间:2015-12-30 13:18:20

标签: android listview adapter

我有一个ListView,它有一个隐藏的button,用户长按一下就可以看到它。 如果有人单击此按钮,则会删除该行。

我的行由交易组成,在同一个Activity我有TextView显示金额。

当我添加交易时,我的文字会更改并且预算会更新。我的问题是当用户点击按钮并删除一行时更新它。

这是我的适配器类

public class HomePageListAdapter extends ArrayAdapter<TRANSAZIONE> {

    ArrayList<TRANSAZIONE> transazioni;
    public HomePageListAdapter(Context context, int textViewResourceId,
                               ArrayList<TRANSAZIONE> objects) {
        super(context, textViewResourceId, objects);
        transazioni = objects;
    }

    TRANSAZIONE transazione;
    NumberFormat formatter = new DecimalFormat("#0.00");

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.adapter_home_page_list, null);

        TextView tvDesc = (TextView) view.findViewById(R.id.tvDescription);
        TextView tvAmou = (TextView) view.findViewById(R.id.tvAmount);
        final Button btnElimina = (Button) view.findViewById(R.id.btnElimina);

        transazione = transazioni.get(position);

        String dText = transazione.getDescription();
        String aText = "";
        if(transazione.getAmount() != null) {
            aText = formatter.format(transazione.getAmount()) + " €";
            if (transazione.getAmount() > 0) {
                tvAmou.setTextColor(Color.GREEN);
            } else {
                tvAmou.setTextColor(Color.RED);
            }
        }
        tvDesc.setText(dText);
        tvAmou.setText(aText);

        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                btnElimina.setVisibility(View.VISIBLE);
                return false;
            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnElimina.getVisibility() == View.VISIBLE) {
                    btnElimina.setVisibility(View.GONE);
                }
            }
        });

        btnElimina.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    new TRANSAZIONE().Delete(TRANSAZIONE.class, getContext(), "where id = '" + transazioni.get(position).getId() + "'");
                    Toast.makeText(getContext(), "Transazione eliminata.", Toast.LENGTH_SHORT).show();
                    transazioni.remove(position);
                    notifyDataSetChanged();

                } catch (Exception ex) {
                    Toast.makeText(getContext(), "Errore non gestito.", Toast.LENGTH_SHORT).show();
                }
            }
        });

        return view;
    }
}

它工作正常,我没有更新布局的问题。

**这是更新TextView文本并调用适配器的方法。它在活动**

public void LoadTotal() throws Exception {

    @SuppressWarnings("unchecked")
    ArrayList<TRANSAZIONE> transazioni = (ArrayList<TRANSAZIONE>) new TRANSAZIONE().SelectAll(TRANSAZIONE.class, getContext(), "");

    Double totale = Settings.getLimitAmount();

    //prendo solo quelle del mese corrente
    if (transazioni.size() > 0) {
        int numeroAggiornamento = Settings.getResettingDay();

        Calendar today = Calendar.getInstance();

        Calendar transactionDate = Calendar.getInstance();

        Calendar lastChangeDate = Calendar.getInstance();
        lastChangeDate.set(Calendar.DAY_OF_MONTH, numeroAggiornamento);
        if (today.get(Calendar.DAY_OF_MONTH) < numeroAggiornamento) {
            lastChangeDate.add(Calendar.MONTH, -1);
        }

        for (TRANSAZIONE t : transazioni) {
            transactionDate.setTime(t.getDate());

            if (transactionDate.compareTo(lastChangeDate) == -1) {
                transazioni.remove(t);
            } else {
                totale += t.getAmount();
            }
        }
    }
    if (transazioni.size() == 0) {
        transazioni.add(new TRANSAZIONE("Nessuna transazione per il mese in corso.", null, null));
    }
    HomePageListAdapter adapter = new HomePageListAdapter(getContext(), R.layout.adapter_home_page_list, transazioni);
    lvTransactions.setAdapter(adapter);
    tvBudget.setText(formatter.format(totale));
}

我的问题如下:

  • 当我删除一行时,它会从列表中消失,但我无法在我的“活动”中拦截此内容。
  • 当我的行被删除时,我需要在某种程度上调用方法 LoadTotal()

任何帮助将不胜感激。

谢谢大家,抱歉我的英语不够完美。

2 个答案:

答案 0 :(得分:2)

最干净的方法是使用DataSetObserver

在您的活动中,您有这个对象:

private DataSetObserver adapterObserver = new DataSetObserver() {
   @Override
   public void onChanged(){
       // here you call your method
       LoadTotal();
   }
}

然后在onResume / onPause

期间注册/取消注册此观察者
@Override
public void onResume(){
   super.onResume();
   LoadTotal(); // update with latest values
   adapter.registerDataSetObserver(adapterObserver);
}

@Override
public void onPause(){
   super.onPause();
   adapter.unregisterDataSetObserver(adapterObserver);
}

编辑:操作的一些调试信息。

这是来自BaseAdapter的notifyDataSetChanged()的代码

public void notifyDataSetChanged() {
    mDataSetObservable.notifyChanged();
}

然后在DataSetObservable中只是循环遍历观察者列表

for (int i = mObservers.size() - 1; i >= 0; i--) {
   mObservers.get(i).onChanged();
}

这意味着那里真的出错很少。但了解正在发生的事情非常重要。所以我的建议是在所有方法调用上放置一个断点:onPause,onResume,onChanged和你调用的行notifyDataSetChanged。并使用调试器运行它,这样您就可以看到什么时候被调用,并找出它无法正常工作的原因。

答案 1 :(得分:1)

在您的适配器类

public class HomePageListAdapter extends ArrayAdapter<TRANSAZIONE> {
Context context;
    ArrayList<TRANSAZIONE> transazioni;
    public HomePageListAdapter(Context context, int textViewResourceId,
                               ArrayList<TRANSAZIONE> objects) {
        super(context, textViewResourceId, objects);
        transazioni = objects;
this.context=context;
    }

    TRANSAZIONE transazione;
    NumberFormat formatter = new DecimalFormat("#0.00");

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.adapter_home_page_list, null);

        TextView tvDesc = (TextView) view.findViewById(R.id.tvDescription);
        TextView tvAmou = (TextView) view.findViewById(R.id.tvAmount);
        final Button btnElimina = (Button) view.findViewById(R.id.btnElimina);

        transazione = transazioni.get(position);

        String dText = transazione.getDescription();
        String aText = "";
        if(transazione.getAmount() != null) {
            aText = formatter.format(transazione.getAmount()) + " €";
            if (transazione.getAmount() > 0) {
                tvAmou.setTextColor(Color.GREEN);
            } else {
                tvAmou.setTextColor(Color.RED);
            }
        }
        tvDesc.setText(dText);
        tvAmou.setText(aText);

        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                btnElimina.setVisibility(View.VISIBLE);
                return false;
            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnElimina.getVisibility() == View.VISIBLE) {
                    btnElimina.setVisibility(View.GONE);
                }
            }
        });

        btnElimina.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    new TRANSAZIONE().Delete(TRANSAZIONE.class, getContext(), "where id = '" + transazioni.get(position).getId() + "'");
                    Toast.makeText(getContext(), "Transazione eliminata.", Toast.LENGTH_SHORT).show();
                    transazioni.remove(position);
                    notifyDataSetChanged();

                } catch (Exception ex) {
                    Toast.makeText(getContext(), "Errore non gestito.", Toast.LENGTH_SHORT).show();
                }
            }
        });

        return view;
    }
}

您可以调用您的活动方法,如

((YourActivityName)context).LoadTotal();