簡體   English   中英

如何一次從 Recyclerview Android 檢索所有視圖數據

[英]How to retrive all views data from Recyclerview Android at once

我在我的 android 項目中有一個 recyclerview,在 recyclerview 中我填充了任何項目的名稱,它的 EditText 對應於 recyclerview 中的每個名稱視圖,這意味着該人可以編輯 recyclerview 中每個項目的價格。 現在在 recyclerview 外面我有一個按鈕,當點擊一個 POST 請求時,它的名稱和相應的價格都會更新。 我找不到將所有數據從 recyclerview 獲取回我的片段的方法。

我的示例代碼來理解問題

public class TestsListAdapterLabsInside extends RecyclerView.Adapter<TestsListAdapterLabs.MyHolder> {

        Context context;
        ArrayList<TestListLabRetro> arrayList;
        double[] TOTAL;

        public TestsListAdapterLabsInside(Context context, ArrayList<TestListLabRetro> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
            this.TOTAL = new double[arrayList.size()];
        }

        @NonNull
        @Override
        public TestsListAdapterLabs.MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_test_labs2, viewGroup, false);

            return new TestsListAdapterLabs.MyHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull app.dezignoo.knockadoc.Adapters.TestsListAdapterLabs.MyHolder myHolder, final int i) {

            myHolder.name.setText(arrayList.get(i).getName());
            myHolder.slno.setText(String.valueOf(i + 1));

            myHolder.price.setText(arrayList.get(i).getPrice());

        }

        @Override
        public int getItemCount() {
            return arrayList.size();
        }


        public class MyHolder extends RecyclerView.ViewHolder {

            TextView slno, name;
            TextView price;

            public MyHolder(View itemView) {
                super(itemView);

                name = itemView.findViewById(R.id.testName);
                slno = itemView.findViewById(R.id.slno);
                price = itemView.findViewById(R.id.testPrice);
            }
        }

    }

這個適配器在我的片段下面定義。 我想要片段中 recyclerview 中所有 <name, price> 的最終列表以更新到服務器。

先感謝您。

在您的 POST 請求之后,將數據添加到 ArrayList 列表並調用 adapter.notifydatasetchaged()

第 1 步)首先您需要在您的 editText 上添加一個 textChangeListner 並在其中更新您的 arrayList

    myHolder.editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            TestListLabRetro testListLabRetro = arrayList.get(i);
            testListLabRetro.setPrice(editable.toString());
            arrayList.set(i, testListLabRetro);
        }
    });

步驟 2) 創建一個 getData 方法

public ArrayList<TestListLabRetro> getData(){
    return this.arrayList;
}

步驟 3) 從您發出 POST 請求的適配器調用 getData 方法以獲取更新的 arrayList

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM