【发布时间】:2014-07-20 02:57:24
【问题描述】:
我有一个带有客户 ArrayAdapter 和自定义 ListItem-XML 的 ListView。在这个 ListView 的底部,我添加了一个带按钮的 FooterView。现在我想在单击此按钮时获取每个 ListItems 中三个 EditTexts 的所有值。
下面是我的代码:
activity_checklistresult.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
result_inner_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/tv_result_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin" />
<EditText
android:id="@+id/et_result_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin"
android:inputType="number" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/tv_result_product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:layout_margin="@dimen/default_margin" />
<AutoCompleteTextView
android:id="@+id/actv_search_product"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="true"
android:ellipsize="end"
android:layout_margin="@dimen/default_margin"
android:inputType="textAutoComplete" />
</LinearLayout>
<TextView
android:id="@+id/tv_result_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin" />
<EditText
android:id="@+id/et_result_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin"
android:inputType="numberDecimal" />
</LinearLayout>
ResultListActivity.java:
public class ChecklistResultActivity extends ListActivity
{
private MyResultAdapter adapt;
private Button confirmButton;
private ChecklistResultActivity rActivity;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklistresult);
// Set my Custom ArrayAdapter for this ListActivity
setAdapter();
rActivity = this;
}
private void setAdapter(){
adapt = new MyResultAdapter(this, R.layout.result_inner_view, Controller.getInstance().getOrderedProducts());
setListAdapter(adapt);
// Add the confirm button at the end of the list
addFooterConfirmButton();
}
private void addFooterConfirmButton(){
ListView lv = (ListView)findViewById(android.R.id.list);
FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.result_footer_view, null);
confirmButton = (Button)footerLayout.findViewById(R.id.confirm_result_button);
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
// Get all EditText values (and compare them with the Default Value)
}
});
lv.addFooterView(footerLayout);
}
...
MyResultAdapter.java:
public class MyResultAdapter extends ArrayAdapter<OrderedProduct>
{
private Context context;
private int layoutResourceId;
private LayoutInflater inflater;
public MyResultAdapter(Context c, int layoutResourceId, List<OrderedProduct> objects){
super(c, layoutResourceId, objects);
this.layoutResourceId = layoutResourceId;
this.context = c;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
if(view == null)
view = inflater.inflate(layoutResourceId, parent, false);
...
return view;
}
}
在// Get all EditText values (and compare them with the Default Value) 的ResultListActivity 中,我想遍历所有OrderedProducts 并将它们的默认值与两个EditTexts 和AutoCompleteTextView 的新填充值进行比较。那么如何从 Footer-Button 的 onClick 中获取这些值呢?
提前感谢您的回复。
【问题讨论】:
-
现在发生了什么?你只得到最后一个值吗?
-
在编辑文本上使用编辑文本更改侦听器,因为此时您将在编辑文本中输入值,然后根据位置将值保存到对象中,然后您可以从该对象中获取所有值。
标签: android list android-listview android-edittext android-arrayadapter