0

"Don't use edittext inside a listview" this is what most of the people say.. now I am thinking should have listened to them. I had a problem earlier when I added an EditText inside a ListView and encountered a problem related to click and I also posted a question regarding this(EditText and ListView are not working together) and also found a solution for that in(Focus on EditText in ListView when block descendants (Android)). After having so many obstacles I have been able to implement this,

But now I am having a recycle issue problem, I have an addTextChangedListener for the EditText in the ListView. Most of the people are suggesting to use setOnFocusChangeListener, but that is not an option for me as i have to change the EditText value of one row depending the value entered in the EditText of the other row. The problem currently I am facing is when I enter the value in the last row of the EditText, the same thing is reflecting in the first row. Can anybody help me to solve this issue. Here is the code snippet (Sorry for bad english)

public class MyBaseAdapter extends BaseAdapter {
    ArrayList < ListData > myList = new ArrayList < > ();
    LayoutInflater inflater;
    Activity activity;
    ListView lvDetail;
    TextView total_amount;
    TextView spendAmount;
    Button proceed;
    HashMap < Integer, ListData > selectedRow = new HashMap < > ();

    public MyBaseAdapter(Activity activity, ArrayList < ListData > myList, ListView lvDetail) {
        this.myList = myList;
        this.activity = activity;
        this.lvDetail = lvDetail;
        inflater = LayoutInflater.from(this.activity);
        total_amount = (TextView) activity.findViewById(R.id.total_amount);
        spendAmount = (TextView) activity.findViewById(R.id.spendAmount);
        proceed = (Button) activity.findViewById(R.id.proceed);

    }

    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    public void setItem(int position, ListData data) {

        myList.set(position, data);
    }@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyViewHolder mViewHolder;
        final ListData currentListData = (ListData) getItem(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);

        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
            mViewHolder.enterAmount.setText("");
            mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
            mViewHolder.tvDesc.setText("");
            mViewHolder.tvTitle.setText("");
            mViewHolder.ivIcon.setImageResource(R.drawable.ic_launcher_1);
        }

        if (selectedRow.containsKey(position)) {


            mViewHolder.enterAmount.setVisibility(View.VISIBLE);
        mViewHolder.enterAmount.setText(currentListData.getEditTextValue());
            mViewHolder.ivIcon.setImageResource(R.drawable.ic_check_orange);

        } else if (currentListData.getEditTextValue().toString().length() == 0) {
            mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
            mViewHolder.enterAmount.setText(currentListData.getEditTextValue());
            mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());

        }

        mViewHolder.tvTitle.setText(currentListData.getTitle());
        mViewHolder.tvDesc.setText(currentListData.getDescription());

        mViewHolder.enterAmount.addTextChangedListener(new TextWatcher() {

            String enterAmount, enterDescption;@Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                enterAmount = s.toString();
                enterDescption = mViewHolder.tvDesc.getText().toString().trim();

                if ((count == 1 && start == 0) && (before == 1)) {

                    mViewHolder.enterAmount.setText("");
                    currentListData.setEditTextValue("");
                    setItem(position, currentListData);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String[] separated;
                int totalBal = 0;

                if ((enterAmount.length() != 0 && enterDescption.length() != 0)) {

                    separated = enterDescption.split(":");
                    totalBal = Integer.parseInt(separated[1]);

                    if ((Integer.parseInt(enterAmount) > totalBal)) {

                        mViewHolder.enterAmount.setText("");
                        currentListData.setEditTextValue("");
                        setItem(position, currentListData);

                        new AlertDialog.Builder(activity)
                            .setTitle("Alert")
                            .setMessage("You do not have sufficient balance")
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                dialog.dismiss();
                            }
                        })
                            .setIcon(android.R.drawable.ic_dialog_alert)
                            .setCancelable(false)
                            .show();


                    } else if (Integer.parseInt(enterAmount) >= 0) {
                        System.out.println(position + "positioninside else if enterAmount:" + Integer.parseInt(enterAmount));
                        currentListData.setEditTextValue(enterAmount);
                        setItem(position, currentListData);

                    }

                }

            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View view) {
                int i = selectedRow.size();
                if (selectedRow.containsKey(position)) {
                    selectedRow.remove(position);
                    mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
                    mViewHolder.enterAmount.setText("");
                mViewHolder.ivIcon.setImageResource(R.drawable.ic_launcher1);
                } else if (i < 2) {
                    ListData currentListData = (ListData) getItem(position);
                    selectedRow.put(position, currentListData);
                    mViewHolder.enterAmount.setVisibility(View.VISIBLE);
                mViewHolder.ivIcon.setImageResource(R.drawable.ic_check_orange);
                } else if (i >= 2) {
                    Toast.makeText(activity, "Select only two cards", Toast.LENGTH_SHORT).show();
                }
            }
        }

        );

        return convertView;
    }

    private static class MyViewHolder {
        TextView tvTitle, tvDesc;
        ImageView ivIcon;
        EditText enterAmount;

        public MyViewHolder(View item) {
            tvTitle = (TextView) item.findViewById(R.id.tvTitle);
            tvDesc = (TextView) item.findViewById(R.id.tvDesc);
            ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
            enterAmount = (EditText) item.findViewById(R.id.edittext);

        }
    }
}
Community
  • 1
  • 1
Rahul Hawge
  • 154
  • 4
  • 15
  • after changing the value of the `EditText` call `notifyDataSetChanged();` in your adapter. – Vipul Asri Oct 13 '15 at 11:35
  • 1
    thanks for your reply. I have tried notifyDataSetChanged(); but still having the issue – Rahul Hawge Oct 13 '15 at 11:37
  • Please post your complete code. – Vipul Asri Oct 13 '15 at 11:40
  • update value of `currentListData.setEditTextValue();` after updating value to `EditText` and then call `notifyDataSetChanged();`. see if it works – Vipul Asri Oct 13 '15 at 12:21
  • 1
    no its not working.The thing here is that, suppose list have number of rows that fills the screen (for ex 6), it works fine. when we add more rows and if it is scrollable then i am facing this issue – Rahul Hawge Oct 13 '15 at 12:33

0 Answers0