【问题标题】:Updating textview on activity once data in adapter class is changed适配器类中的数据更改后更新活动的文本视图
【发布时间】:2014-08-07 16:15:56
【问题描述】:

我的仪表板活动中有 textview txtQuantity。我为包含已售产品的自定义适配器编写了单独的类。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    list = (ListView) findViewById(R.id.listSoldItems);

    txtAmount = (TextView) findViewById(R.id.txtAmount);
    txtItems = (TextView) findViewById(R.id.txtItems);

    // init listview
    adapter = new Sold_item_adaptor(Dashboard.this, soldItemsList);
    list.setAdapter(adapter);

我可以使用适配器从列表中删除项目。移除项目的代码写在适配器类中。

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row_sold_item, null);

    TextView txtListItem = (TextView) vi.findViewById(R.id.txtListItem);
    final TextView txtQuantity = (TextView) vi.findViewById(R.id.txtQuantity);
    ImageView imgCancel = (ImageView) vi.findViewById(R.id.imgCancel);

    HashMap<String, String> mapData = new HashMap<String, String>();
    mapData = data.get(position);

    txtListItem.setText(mapData.get("product"));
    txtQuantity.setText(mapData.get("qty"));

    imgCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doButtonOneClickActions(txtQuantity, position);
        }
    });

    return vi;
}

private void doButtonOneClickActions(TextView txtQuantity, int rowNumber) {
    // Do the actions for Button one in row rowNumber (starts at zero)
    data.remove(rowNumber);
    notifyDataSetChanged();

}

在我的仪表板活动中,我正在维护所选项目的数量,总量。现在,如果我从列表视图中删除项目,来自自定义适配器的代码会删除该项目,但我如何才能在仪表板活动上获得通知/信号以更新数量。

【问题讨论】:

  • 将数量作为数据的一部分保留在arraylist中(列表视图更新)

标签: android listview android-activity android-listview


【解决方案1】:

通过提供一个简单的回调。

为此,在您的适配器中编写一个简单的接口

public interface OnDataChangeListener{
    public void onDataChanged(int size);
}

并为监听器添加一个设置器(也在适配器中)

OnDataChangeListener mOnDataChangeListener;
public void setOnDataChangeListener(OnDataChangeListener onDataChangeListener){
    mOnDataChangeListener = onDataChangeListener;
}

现在将附加代码添加到适配器中的以下块

private void doButtonOneClickActions(TextView txtQuantity, int rowNumber) {
    ...
    if(mOnDataChangeListener != null){
        mOnDataChangeListener.onDataChanged(data.size());
    }
}

然后您需要在仪表板活动中注册监听器

protected void onCreate(Bundle savedInstanceState) {
    ...
    adapter.setOnDataChangeListener(new Sold_item_adaptor.OnDataChangeListener(){
        public void onDataChanged(int size){
            //do whatever here
        }
    });
}

就是这样;)。

【讨论】:

  • 技术上看起来很正确,但变量 mOnDataChangeListener 经常变为空。为什么会发生这种情况?
  • @fivos 也许您在错误的地方寻找错误?上面的代码从不将变量本身设置为 null。如果您自己找不到错误,请在 SO 上创建一个新问题。也许有人或我可以帮助你。
【解决方案2】:

主要思想是:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);

list = (ListView) findViewById(R.id.listSoldItems);

txtAmount = (TextView) findViewById(R.id.txtAmount);
txtItems = (TextView) findViewById(R.id.txtItems);

// init listview
adapter = new Sold_item_adaptor(Dashboard.this, soldItemsList,txtAmount);
list.setAdapter(adapter);

在您的适配器中:

public class MyAdapter extends ArrayAdapter<SoldItemsList > {

private Context context;
private mTotalQty;
private TextView mTxtAmountAdapter;


 public OfferAdapter(Context context, int resource,SoldItemsList object,TextView txtAmount ) {
    super(context, resource, objects);
    this.context = context;
    this.mTxtAmountAdapter = txtAmount;


}

//...

imgCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    doButtonOneClickActions(position);
    // update totalAmount
     mTxtAmountAdapter.setText(Integer.valueOf(totalAmount).toString()));

}
});

imgPlus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    qtyClickAction(position);
     // update totalQty
    mTxtAmountAdapter.setText(Integer.valueOf(totalAmount).toString()));

}
});

【讨论】:

    【解决方案3】:

    在你的适配器类中覆盖 notifyDataSetChanged() ... 并做任何你想做的事 ...

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
       // Your code to nofify
    }
    

    【讨论】:

      【解决方案4】:

      据我了解,您对在 doButtonOneClickActions(); 之后更新适配器外部 UI 的方法感兴趣

      最简单的方法是使用https://github.com/greenrobot/EventBus

      可以在这里找到示例http://awalkingcity.com/blog/2013/02/26/productive-android-eventbus/

      如果不想这样做,可以创建回调http://android-er.blogspot.dk/2013/09/implement-callback-function-with.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多