ListView,每行有两个编辑文本

时间:2014-12-15 13:58:47

标签: android listview android-listview android-edittext textwatcher

如何从listview中存储和检索editText,每行有2个editText?

使用此代码,当我向一个editText写入内容时,它会将值复制到所有其他内容。

public class CheckoutAdapter extends BaseAdapter{

    private Context mContext;
    private List<JsonObject> mObjs;
    private ViewHolder holder;
    private SessionManager session;
    private float Value = 0;
    private int tax = 0;
    private float Total = 0;
    private int viewID = 0;
    private EnhancedListView listview;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    static DisplayImageOptions options;
    private ArrayList<String> arrayFirstName = new ArrayList<String>();
    private ArrayList<String> arrayLastName = new ArrayList<String>();

    public CheckoutAdapter(Context context, List<JsonObject> objs, EnhancedListView mListView) {
        // TODO Auto-generated constructor stub
        this.mContext = context;
        this.mObjs = objs;
        this.listview = mListView;

        for (int i = 0; i < mObjs.size(); i++) {
            arrayFirstName.add("");
            arrayLastName.add("");
        }
    }
    //Classe ViewHolder
    public final class ViewHolder {
        public TextView ticketValue;
        public TextView ticketItem;
        public TextView ticketOpcao;
        public TextView ticketTax;
        public TextView ticketTitle;
        public TextView ticketDate;
        public TextView ticketFullName;
        public EditText ticketFirstName;
        public EditText ticketLastName;
        public ImageView ticketImageThumb;
        int position;
    }

    public void remove(int position) {
        mObjs.remove(position);

    }

    public void insert(int position, JsonObject item) {
        mObjs.add(position, item);

    }

    @Override
    //Contar a quntidade de linhas.
    public int getCount() {
        // TODO Auto-generated method stub
        return mObjs.size();
    }

    @Override
    //Pegar o item conforme a posição.
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mObjs.get(position);
    }

    @Override
    //ID do item (refere-se a posição em que ele se encontra).
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)  {
        // TODO Auto-generated method stub
         View vi=convertView;
         //Se não houver nenhuma View, uma nova view é criada.
            if(convertView==null) 
            {
                vi = LayoutInflater.from(mContext).inflate(R.layout.row_checkout, null);
                Typeface font = Typeface.createFromAsset(mContext.getAssets(), "fonts/Oswald-Bold.otf");
                final View origView = vi;
                vi.findViewById(R.id.action_delete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       listview.delete(((ViewHolder)origView.getTag()).position);
                    }
                });
            holder = new ViewHolder();
            holder.ticketItem = (TextView)vi.findViewById(R.id.tvItem);
            holder.ticketOpcao = (TextView)vi.findViewById(R.id.tvOpcao);
            holder.ticketTax = (TextView)vi.findViewById(R.id.tvTaxa);
            holder.ticketValue = (TextView)vi.findViewById(R.id.tvValue);
                holder.ticketValue.setTypeface(font);
            holder.ticketTitle = (TextView)vi.findViewById(R.id.tvTitle);
                holder.ticketTitle.setTypeface(font);
            holder.ticketDate = (TextView)vi.findViewById(R.id.tvDate);
                holder.ticketDate.setTypeface(font);
            holder.ticketFullName = (TextView)vi.findViewById(R.id.tvFullName);
                holder.ticketFullName.setTypeface(font);
            holder.ticketFirstName = (EditText)vi.findViewById(R.id.etFirstName);
            holder.ticketLastName = (EditText)vi.findViewById(R.id.etLastName);
            holder.ticketImageThumb = (ImageView)vi.findViewById(R.id.imgFlyerSub);
                holder.position = position;
                holder.ticketFirstName.addTextChangedListener(FirstNameChangedListener(position));
                holder.ticketLastName.addTextChangedListener(LastNameChangedListener(position));
                holder.ticketFirstName.setOnFocusChangeListener(FirstNameFocusChangeListener(position));
                holder.ticketLastName.setOnFocusChangeListener(LastNameFocusChangeListener(position));

            vi.setTag(holder);
            //Caso já exista uma view o objeto holder recebe o getTag da View vi.
        } else {
            holder = (ViewHolder)vi.getTag();
        }
            holder.ticketItem.setText(mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_ITEM).getAsString());
            holder.ticketOpcao.setText(mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_OPCAO).getAsString());
            holder.ticketTax.setText("Taxa de serviço:" + mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_TAX).getAsString() + "%");
            holder.ticketValue.setText("R$" + String.valueOf(mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_VALUE).getAsInt()) + ",00");
            holder.ticketDate.setText(mObjs.get(position).getAsJsonObject().get(Constants.KEY.KEY_EVENTDATE).getAsString());
            holder.ticketTitle.setText(mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_EVENTTITLE).getAsString());


            holder.ticketFirstName.setText(arrayFirstName.get(holder.position));
            holder.ticketLastName.setText(arrayLastName.get(holder.position));


        options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.ic_stub)
                .showImageForEmptyUri(R.drawable.ico_load_imagem)
                .cacheInMemory().cacheOnDisc()
                .displayer(new RoundedBitmapDisplayer(5))
                .build();
        imageLoader.displayImage(mObjs.get(position).getAsJsonObject().get("thumbPrincipal").getAsString(), holder.ticketImageThumb, options);




        return vi;
    }

    private View.OnFocusChangeListener LastNameFocusChangeListener(final int position) {
        return new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus){
                    viewID = holder.position;


                }
            }
        };
    }

    private View.OnFocusChangeListener FirstNameFocusChangeListener(final int position) {
        return new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus){

                    viewID = holder.position;


                }
            }
        };
    }

    private TextWatcher FirstNameChangedListener(final int position) {
        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                String itemName = s.toString();
                arrayFirstName.add(viewID, itemName);
            }
        };
    }

    private TextWatcher LastNameChangedListener(final int position) {
        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String itemName = s.toString();
                arrayLastName.add(viewID, itemName);
            }
        };
    }

}

编辑:删除了setId和JSON。

1 个答案:

答案 0 :(得分:0)

你的持有人是你班上的一员,这是错的。 从这个地方删除它。列表视图的每一行都有自己的持有者。 这个持有者通过setTag方法“贴在”视图上。

在使用holder的侦听器中,您必须从发送事件的视图中获取持有者。 为此,以下代码应该起作用:

private View.OnFocusChangeListener FirstNameFocusChangeListener(final int position) {
    return new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus){
                ViewHolder holder = (ViewHolder ) v.getTag();
                viewID = holder.position;
            }
        }
    };
}