单击android中自定义列表视图中的edittext时出现错误

时间:2012-04-23 11:35:57

标签: android android-listview

单击编辑按钮所有项目名称,desc,价格都会进入带有自定义列表视图的edittext。我无法在edittext中输入任何内容。如果我点击edittext它会显示应用程序没有响应错误。

//my code getview method in Base adapter
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     final ViewHolder holder;


        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.editappetisesrlistview, null);
            holder.caption = (EditText) convertView
                    .findViewById(R.id.editappetisertext);
            holder.price=(EditText)convertView.findViewById(R.id.editappetiserprice);
            holder.desc=(EditText)convertView.findViewById(R.id.editappetiserdesc);
            holder.caption1=(ImageView) convertView.findViewById(R.id.editappetiserimage);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //Fill EditText with the value you have in data source

        holder.caption.setText(itemnames[position]);
        holder.price.setText(prices[position]);
        holder.desc.setText(descs[position]);
        holder.caption.setId(position);
        holder.price.setId(position);
        holder.desc.setId(position);
        holder.caption.setFocusableInTouchMode(true);
        holder.price.setFocusableInTouchMode(true);
        holder.desc.setFocusableInTouchMode(true);

        holder.caption1.setImageBitmap(bmps[position]);    

        //we need to update adapter once we finish with editing
        holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus)
            {
                    final int position = v.getId();      
                    final EditText Caption = (EditText) v;
                    Caption.setFocusable(true);

                  holder.caption.setFocusable(true);
                  arr.remove(holder.caption.getId());
                    arr.add(holder.caption.getId(),holder.caption.getText().toString());


            }
                }

        });    
        holder.price.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus)
            {
                    final int position = v.getId();      
                    final EditText Caption1 = (EditText) v;
                 //   Caption1.setFocusable(true);

                    holder.price.setFocusable(true);
                    arr.remove(holder.price.getId());
                    arr.add(holder.price.getId(),holder.price.getText().toString());


            }
            holder.desc.setOnFocusChangeListener(new OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus)
                {
                        final int position = v.getId();      
                        final EditText Caption2 = (EditText) v;
                     //   Caption2.setFocusable(true);

                        holder.desc.setFocusable(true);
                        arr.remove(holder.desc.getId());
                        arr.add(holder.desc.getId(),holder.desc.getText().toString());


                }
                    }

            });  

                }

        });  




        return convertView;
    }
}

viewholder.java

class ViewHolder {
    EditText caption,price,desc;
    ImageView caption1;
}

class ListItem {
    String caption;
}

请帮助

1 个答案:

答案 0 :(得分:0)

mViewHolder.editText = (EditText)convertView.findViewById(R.id.editText1);
            mViewHolder.editText.setTag(position);
            mViewHolder.editText.setTextColor(0xFF000000);
            mViewHolder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus) {
                        EditText et =(EditText)v.findViewById(R.id.editText1);
                        editTextArrayList.set(position,et.getText().toString().trim());
                    }
                }
            });

您好,以下链接可能会对您有所帮助http://mylearnandroid.blogspot.in/2014/06/listview-problem-while-scrolling.html