Listview中的EditText及其在android

时间:2015-10-13 11:20:51

标签: android listview

"不要在列表视图中使用edittext"这是大多数人所说的......现在我在想应该听取他们的意见。我之前在ListView中添加EditText并遇到与click相关的问题时遇到了问题,我还发布了一个关于此问题的问题(EditText and ListView are not working together),并在(Focus on EditText in ListView when block descendants (Android))中找到了解决方法。在遇到这么多障碍后,我能够实现这个目标,

但是现在我遇到了回收问题,我在ListView中为EditText添加了一个addTextChangedListener。大多数人都建议使用setOnFocusChangeListener,但这对我来说不是一个选项,因为我必须根据在另一行的EditText中输入的值更改一行的EditText值。 我目前面临的问题是当我在EditText的最后一行输入值时,同样的事情在第一行中反映出来。任何人都可以帮我解决这个问题。 这是代码片段(抱歉英文不好)

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

        }
    }
}

0 个答案:

没有答案