zxcetera
zxcetera

Reputation: 115

How to maintain value of each EditText in customlistview

Good Day, I have a problem with maintaining value on each EditText in a custom listview for example I rotate the screen, each value has gone, Please Help me what approach should I do, Thanks in advance.

enter image description here

Picture Adapter

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

Upvotes: 1

Views: 184

Answers (3)

Arpit Patel
Arpit Patel

Reputation: 8067

Just store value using this method

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

    }

You can then retrieve the data in your onCreate method or onCreateView or onActivityCreated like this:

public void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

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

    ...

}

Upvotes: 0

Hardy Android
Hardy Android

Reputation: 865

add setQuantity and getQuantity (getter-setter method in your PSP model).

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

I hope it's really help you.

Upvotes: 2

Devender Kumar
Devender Kumar

Reputation: 89

Hope this will work for you- Use this in your activity tag in manifest file-

        android:configChanges="orientation|keyboardHidden|screenSize"

please let me know if this does not work for you.

Upvotes: 0

Related Questions