Android - 使用自定义 ArrayAdapter 获取 ListView 的每个 EditTexts 值 [英] Android - Get each EditTexts values of ListView with custom ArrayAdapter

查看:25
本文介绍了Android - 使用自定义 ArrayAdapter 获取 ListView 的每个 EditTexts 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有客户 ArrayAdapter 和自定义 ListItem-XML 的 ListView.在这个 ListView 的底部,我添加了一个带有 Button 的 FooterView.现在,当我单击此按钮时,我想获取每个 ListItem 中三个 EditText 的所有值.

I've got a ListView with a customer ArrayAdapter and a custom ListItem-XML. At the bottom of this ListView I've added a FooterView with a Button. Now I want to get all the values of the three EditTexts in each of my ListItems when I click this Button.

这是我的代码:

activity_checklistresult.xml:

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:

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:

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:

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;
    }
}

//的 ResultListActivity 中获取所有 EditText 值(并将它们与默认值进行比较) 我想遍历所有 OrderedProducts 并比较这些默认值,并使用新的填充值 -在两个 EditTexts 和 AutoCompleteTextView 的值中.那么如何从页脚按钮的 onClick 中获取这些值?

In the ResultListActivity at // Get all EditText values (and compare them with the Default Value) I want to loop through all the OrderedProducts and compare the default values of those, with the new filled-in values of the two EditTexts and the AutoCompleteTextView. So how can I get those values from within the onClick of the Footer-Button?

预先感谢您的回复.

推荐答案

您的适配器必须为每个创建的 EditText 保存更改文本:

Your adapter has to save text on change for each created EditText :

  1. 创建地图以保存值
  2. 为每个 EditText 定义和设置一个通用的 TextWatcher
  3. 在 editText 上设置一个标签(使用它的位置或任何id 你想要)我们将使用这个标签作为 hashMap 中的键
  4. 创建一个方法来检索给定 EditText 的值

适配器:

public class MyResultAdapter extends ArrayAdapter<OrderedProduct>
{
    ...

    private HashMap<String, String> textValues = new HashMap<String, String>();

    ...

    public ViewView getView(int position, View convertView, ViewGroup parent){

        View view = convertView;
        boolean convertViewWasNull = false;
        if(view == null){
            view = inflater.inflate(layoutResourceId, parent, false);
            convertViewWasNull = true;
        }


        myEditText1 = findViewById...
        myEditText2 = findViewById...

        if(convertViewWasNull ){

            //be aware that you shouldn't do this for each call on getView, just once by listItem when convertView is null
            myEditText1.addTextChangedListener(new GenericTextWatcher(myEditText1));
            myEditText2.addTextChangedListener(new GenericTextWatcher(myEditText2));
        }

        //whereas, this should be called on each getView call, to update view tags.
        myEditText1.setTag("theFirstEditTextAtPos:"+position);
        myEditText2.setTag("theSecondEditTextAtPos:"+position);
    }


    private class GenericTextWatcher implements TextWatcher{

       private View view;
       private GenericTextWatcher(View view) {
            this.view = view;
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

        public void afterTextChanged(Editable editable) {

            String text = editable.toString();
            //save the value for the given tag :
            MyResultAdapter.this.textValues.put(view.getTag(), editable.toString());
        }
    }

    //you can implement a method like this one for each EditText with the list position as parameter :
    public String getValueFromFirstEditText(int position){
          //here you need to recreate the id for the first editText
         String result = textValues.get("theFirstEditTextAtPos:"+position);
         if(result ==null)
              result = "default value";

         return result;
    }

    public String getValueFromSecondEditText(int position){
          //here you need to recreate the id for the second editText
         String result = textValues.get("theSecondEditTextAtPos:"+position);
         if(result ==null)
              result = "default value";

         return result;
    }

}

这篇关于Android - 使用自定义 ArrayAdapter 获取 ListView 的每个 EditTexts 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆