我的仪表板活动中有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();
}
在我的信息中心活动中,我维护所选项目的数量,总金额。现在,如果我从listview中删除项目,自定义适配器中的代码将删除该项目,但如何在仪表板活动上获取通知/信号以更新数量。
答案 0 :(得分:30)
提供简单的回调。
为此,请在适配器中编写一个简单的界面
public interface OnDataChangeListener{
public void onDataChanged(int size);
}
并为侦听器(也在适配器中)添加一个setter
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
}
});
}
就是这样;)。
答案 1 :(得分:10)
主要思想是:
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()));
}
});
答案 2 :(得分:1)
覆盖适配器类中的notifyDataSetChanged()...并按照您的意愿执行...
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
// Your code to nofify
}
答案 3 :(得分:0)
据我所知,您对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