如何在 customlistview 中维护每个 EditText 的值

Posted

技术标签:

【中文标题】如何在 customlistview 中维护每个 EditText 的值【英文标题】:How to maintain value of each EditText in customlistview 【发布时间】:2016-08-05 10:39:24 【问题描述】:

美好的一天,我在自定义列表视图中维护每个 EditText 的值时遇到问题,例如我旋转屏幕,每个值都消失了, 请帮助我应该采取什么方法,在此先感谢。

图片适配器

    public View getView(int position, View convertView, ViewGroup parent) 
        View row;
        row = convertView;
        final dataHandler handler;
        if(convertView == null)
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate( R.layout.row_layout,parent, false);
            handler = new dataHandler();
            handler.pictures = (ImageView) row.findViewById(R.id.pictures);
            handler.name = (TextView) row.findViewById(R.id.picturename);
            handler.qty= (EditText) row.findViewById(R.id.qty);
            handler.add = (Button) row.findViewById(R.id.btnplus);
            handler.minus = (Button) row.findViewById(R.id.btnminus);
            row.setTag(handler);
        else
            handler = (dataHandler) row.getTag();
        
        final PSP psp;
        psp =(PSP) this.getItem(position);
    Picasso.with(getContext()).load(psp.getPicture()).resize(200, 155).into(handler.pictures);
    handler.name.setText(psp.getName() + "\n ₱" + psp.getPrice());
    handler.qty.setText("0");
    img = psp.getPicture();

    handler.add.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            String pricex = handler.qty.getText().toString();
            int xx = Integer.parseInt(pricex);
            int total = xx + 1;
            String totalx = Integer.toString(total);
            handler.qty.setText(totalx);

            map.put("" + handler.name.getText().toString(), " " + handler.qty.getText().toString());
            ShowHashMapValue();
        
    );

    handler.minus.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            String pricex = handler.qty.getText().toString();

            int xx = Integer.parseInt(pricex);
            if(xx > 0 )
                int total = xx-1;
                String totalx = Integer.toString(total);
                handler.qty.setText(totalx);
                map.put("" + handler.name.getText().toString(), " " + handler.qty.getText().toString());


            
    );



    return row;

【问题讨论】:

你可以在哪里设置你的适配器? 试试这个链接***.com/questions/16692536/… 谢谢它会有所帮助:) 如果您觉得我的帖子有帮助,请点赞。 【参考方案1】:

PSP 模型中添加 setQuantitygetQuantitygetter-setter 方法)。

    String pricex = psp.getQuanntity();
                int xx = Integer.parseInt(pricex);
                int total = xx + 1;
                String totalx = Integer.toString(total);
    handler.qty.setText(totalx);

     handler.add.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v)     
                //add
                psp.setQuantity(yourQuantity++);
                notifyDataSetChanged();
              
        );

 handler.minus.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v)     
                //add
                psp.setQuantity(yourQuantity--);
                notifyDataSetChanged();
              
        );

希望对你有帮助。

【讨论】:

很好用,我现在只需要学习如何使用 onSaveInstanceState,非常感谢 :) 如果您真的对我的回答感到满意,请接受。谢谢【参考方案2】:

希望这对你有用- 在清单文件中的活动标签中使用它-

        android:configChanges="orientation|keyboardHidden|screenSize"

如果这对您不起作用,请告诉我。

【讨论】:

【参考方案3】:

使用这种方法只存储价值

 public void onSaveInstanceState(Bundle savedState) 

        super.onSaveInstanceState(savedState);

        // Note: getValues() is a method in your ArrayAdapter subclass
        String[] values = mAdapter.getValues(); 
        savedState.putStringArray("myKey", values);

    

然后您可以在 onCreate 方法或onCreateViewonActivityCreated 中检索数据,如下所示:

public void onCreate (Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) 
        String[] values = savedInstanceState.getStringArray("myKey");
        if (values != null) 
           mAdapter = new MyAdapter(values);
        
    

    ...


【讨论】:

以上是关于如何在 customlistview 中维护每个 EditText 的值的主要内容,如果未能解决你的问题,请参考以下文章

CustomListView 中的向下滚动问题

CustomListView

android CustomListView - 图像视图覆盖

如何在 monorepo 中维护每个模块的自动更改日志?

如何维护每个 ngFor 项目的按钮状态

如何在每个操作栏选项卡中维护多个Fragment堆栈或多个子片段