我想使用水平数字选择器,所以我做了自定义小部件。
在适配器中,当我使用自定义窗口小部件的按钮点击和设置计数时,我想更改适配器中的EditText
文本。
这是我的自定义组件
HorizontalNumberPicker.java
public class HorizontalNumberPicker extends LinearLayout {
Button plus;
Button minus;
EditText num;
int count;
OnCustomEventListener mListener;
public HorizontalNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.horizontal_number_picker, this);
plus = (Button) findViewById(R.id.btn_plus);
minus = (Button) findViewById(R.id.btn_minus);
num = (EditText) findViewById(R.id.edit_text);
plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (num.getText().toString().equals("10")) {
if (mListener != null)
mListener.onEvent(count);
return;
} else {
count = Integer.parseInt(num.getText().toString()) + 1;
num.setText(String.valueOf(count));
if (mListener != null)
mListener.onEvent(count);
}
}
});
minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (num.getText().toString().equals("0")) {
if (mListener != null)
mListener.onEvent(count);
return;
} else {
count = Integer.parseInt(num.getText().toString()) - 1;
num.setText(String.valueOf(count));
if (mListener != null)
mListener.onEvent(count);
}
}
});
}
public interface OnCustomEventListener {
public abstract void onEvent(int count);
}
public void setCustomEventListener(OnCustomEventListener eventListener) {
mListener = eventListener;
}
}
这是我的自定义适配器
private Context context;
LayoutInflater inflater;
View itemView;
ArrayList<MenuItemData> list;
// Declare Variables
NetworkImageView iv_menu_img = null;
TextView tv_menu_name = null;
TextView tv_menu_price = null;
// Progress dialog
private ProgressDialog pDialog;
int price;
//imageloader using volley
ImageLoader mImageLoader;
HorizontalNumberPicker pick_menu_grid;
public MenuListGridAdapter(Context context, ArrayList<MenuItemData> list, ProgressDialog pDialog) {
this.context = context;
this.list = new ArrayList<MenuItemData>();
this.list.addAll(list);
this.pDialog = pDialog;
//image loader
mImageLoader = AppController.getInstance().getImageLoader();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.menulist_grid_item, parent, false);
iv_menu_img = (NetworkImageView) itemView.findViewById(R.id.iv_menu_img);
tv_menu_name = (TextView) itemView.findViewById(R.id.tv_menu_name);
tv_menu_price = (TextView) itemView.findViewById(R.id.tv_menu_price);
pick_menu_grid = (HorizontalNumberPicker)itemView.findViewById(R.id.pick_menu_grid);
} else {
itemView = (View) convertView;
}
// Set the result into ImageView
iv_menu_img.setImageUrl(url + list.get(position).getImageFilePathIndex(), mImageLoader);
// Set the results into TextViews
tv_menu_name.setText(list.get(position).getMenuCHNName());
tv_menu_price.setText(list.get(position).getMenuPrice());
//set price to listener
if(list.get(position).getMenuPrice().equals("")){
price = 0;
}
pick_menu_grid.setCustomEventListener(new HorizontalNumberPicker.OnCustomEventListener() {
@Override
public void onEvent(int count) {
list.get(position).setCount(count);
price = Integer.parseInt(list.get(position).getMenuPrice());
tv_menu_price.setText(String.valueOf(price*count));
notifyDataSetChanged();
}
});
pDialog.dismiss();
return itemView;
}
点击HorizontalNumberPicker
按钮后 - &gt;计数正在变化(ArrayList
中的价格) - &gt; tv_menu_price.setText(String.valueOf(price*count));
- &gt; EditText(tv_menu_price)
正在改变(在适配器中)。
我不知道在哪里可以添加或编辑此代码。只是它没有用。我该怎么办?
答案 0 :(得分:0)
它不起作用,因为您必须在dataSet中设置要更改的所有数据,并将数据设置为text.setText(dataSet.get(i)blabla)等文本。所以在你的主要活动中,你喜欢这样:adapter.getData()。set(i).blabla,然后在对dataSet进行更改后调用mAdapter.notifyDataSetChanged()。