【发布时间】:2014-05-21 16:48:00
【问题描述】:
我在理解一个概念时遇到了问题。我正在制作一个应用程序,我将在其中拥有一组自定义对象。然后这些对象应该在布局中表示。
在此简化以说明重点:
假设我有一个 MyProducts 列表:
public class MyProduct {
private string m_name;
private float m_price;
private int m_amount;
public string getName() {
return name;
}
public void setName(string name) {
m_name = name;
}
etc...
}
然后我想要一个显示这些信息的复合视图(?) - 并且还允许我设置其中的一些。就像一个带有名称、价格和金额等的 TextView 的框。
因此,当其中一个控件发生更改时,列表中的对象应该被更新。我该怎么做呢?
我的自发想法是直接在自定义视图中实现所有逻辑。让 MyProduct 扩展让我们说 LinearLayout。然后我想要的对象的所有逻辑都直接在那里实现。并且视图将实现 View.OnClickListener 并处理它的所有更改。
类似这样的:
public class MyProduct extends LinearLayout {
private TextView m_nameTextView;
private EditText m_priceEditText;
private Spinner m_amountSpinner;
private string m_name;
private float m_price;
private int m_amount;
public ExerciseView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.MyProduct, this);
m_nameTextView = (TextView) findViewById(R.id.name);
m_priceEditText = (EditText) findViewById(R.id.price);
m_amountSpinner = (Spinner) findViewById(R.id.amount);
}
public string getName() {
return name;
}
public void setName(string name) {
m_name = name;
}
etc..
}
那是犹太洁食吗? 我想错了吗?
【问题讨论】:
标签: java android xml user-interface mobile