专注于listview中的错误编辑文本

时间:2017-01-03 07:18:44

标签: android listview android-edittext focus

当我尝试点击EditText项内的ListView时。它获得焦点并失去焦点,而调试我发现,如果我点击第21项,位置值改变为21,19,1,2等。不确定列表是重新渲染本身还是其他正在发生的事情。我已经搜索了很多,并且已经尝试配置ListViewbeforeDescendantsafterDescendants)和ActivityadjustPan)。

我使用过自定义适配器。共享XML的{​​{1}}文件和列表项以及适配器的java代码。

XML列表:

ListView

list_item_selector的XML

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="10dp">

    <!-- submit progress -->
    <ProgressBar
        android:id="@+id/list_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone"/>

    <LinearLayout
        android:id="@+id/list_form"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp">
        <TextView
            android:id="@+id/nodata_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No data Found"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"
            android:visibility="gone"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/info_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        <Button
            android:id="@+id/send_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Create Order"
            android:textSize="14sp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:layout_weight="1"
            android:textColor="@android:color/white"
            android:background="@drawable/bg_button"/>


    </LinearLayout>
    <LinearLayout
        android:id="@+id/ly_item_list_form"
        android:layout_below="@+id/info_ll"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <ListView
            android:id="@+id/item_list_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="afterDescendants"/>
    </LinearLayout>

</RelativeLayout>

我的自定义适配器类

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="2dp"
    card_view:cardPreventCornerOverlap="true"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:text="ItemName"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="15sp"
            android:textStyle="bold"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:background="#FF0B38CC"></RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/uom_desc"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginStart="10dp"
                    android:text="uom"
                    android:textColor="@color/colorPrimary"
                    android:textSize="13sp"
                    />

            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.1"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:orientation="horizontal">

                    <ImageButton
                        android:id="@+id/button_minus"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:background="@null"
                        android:src="@drawable/ic_remove_black_24dp"/>

                    <EditText
                        android:id="@+id/number_of_item"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:text="0"
                        android:digits="0123456789"
                        android:maxLength="4"
                        android:inputType="number"
                        android:textColor="@color/colorPrimary"/>

                    <ImageButton
                        android:id="@+id/button_plus"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"
                        android:background="@null"
                        android:src="@drawable/ic_add_black_24dp"/>
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

提前致谢。

2 个答案:

答案 0 :(得分:0)

onCreate

之前的setContentView()中插入此行
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);

一定会帮到你

答案 1 :(得分:0)

最后,我想通了。问题是试图解决getview中的tex改变值。这是重新渲染列表项目,我得到了错误的焦点和不同的怪异行为。我创建了一个textwatcher类,并在初始化对象时添加它,这样它就不会影响列表中每个项目的每次。以下是MyCustom适配器的代码

public class OrderViewListAdapter extends ArrayAdapter<OrderItemList> {

    public OrderViewListAdapter(Context context, int resource) {
        super(context, resource);
    }

    private class ViewHolder {
        TextView sku_desc;
        TextView uom_desc;
        EditText order_qty;
        ImageButton plus;
        ImageButton minus;
        AutoCompleteTextView uom;
        MyCustomEditTextListener myCustomEditTextListener;

        public ViewHolder(View convertView, MyCustomEditTextListener myCustomEditTextListener) {
            this.myCustomEditTextListener = myCustomEditTextListener;
            this.order_qty = (EditText) convertView.findViewById(R.id.number_of_item);
            this.order_qty.addTextChangedListener(myCustomEditTextListener);

        }
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        OrderItemList vehicleTransit = mDataSet.get(position);
        //System.out.println("kamal123" +mDataSet.get(position).getOrderID());
        System.out.println("kamal123" +position);
        ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.list_item_selector, parent, false);
            viewHolder = new ViewHolder(convertView, new MyCustomEditTextListener());
            viewHolder.sku_desc = (TextView) convertView.findViewById(R.id
                    .item_name);
            viewHolder.uom_desc = (TextView) convertView.findViewById(R.id
                    .uom_desc);
            viewHolder.order_qty = (EditText) convertView.findViewById(R.id.number_of_item);
            viewHolder.plus= (ImageButton)convertView.findViewById(R.id.button_plus) ;
            viewHolder.minus= (ImageButton)convertView.findViewById(R.id.button_minus) ;
            convertView.setTag(viewHolder);
            viewHolder.plus.setTag(viewHolder);
            viewHolder.minus.setTag(viewHolder);
            viewHolder.order_qty.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if(vehicleTransit!= null) {
            System.out.println("kamal123" + vehicleTransit.getExpectedDeliveryDate()
                    .toString());


            if (vehicleTransit.getExpectedDeliveryDate() != null) {
                viewHolder.uom_desc.setText(vehicleTransit.getUOMDesc()
                        .toString());
                System.out.println("kamal123" +vehicleTransit.getExpectedDeliveryDate()
                        .toString());

            }if (vehicleTransit.getSkuDesc() != null) {
                viewHolder.sku_desc.setText(vehicleTransit.getSkuDesc().toString());
            }

            viewHolder.myCustomEditTextListener.updatePosition(position);
            viewHolder.order_qty.setText(vehicleTransit.getOrderQty()+"");
                viewHolder.order_qty.setId(position);

        }



        viewHolder.plus.setOnClickListener(new View.OnClickListener() {


            int count=0;

            @Override
            public void onClick(View v) {
                View p = (View) v.getParent();
                ViewHolder holder1 = (ViewHolder) v.getTag();
                count = Integer.valueOf(holder1.order_qty.getText().toString());
                count++;
                holder1.order_qty.setText(String.valueOf(count));
                mDataSet.get(position).setOrderQty(count);
                //mAdapter.notifyDataSetChanged();
            }

        });



        viewHolder.minus.setOnClickListener(new View.OnClickListener() {


            int count=0;

            @Override
            public void onClick(View v) {
                View p = (View) v.getParent();
                ViewHolder holder1 = (ViewHolder) v.getTag();
                count = Integer.valueOf(holder1.order_qty.getText().toString());
                count--;
                if(count>=0)
                {holder1.order_qty.setText(String.valueOf(count));
                mDataSet.get(position).setOrderQty(count);
                //mAdapter.notifyDataSetChanged();
                }

            }

        });



        return convertView;
    }



    private class MyCustomEditTextListener implements TextWatcher {
        private int position;

        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            if (!charSequence.toString().equals("")) {
                mDataSet.get(position).setOrderQty(Integer.valueOf(charSequence.toString()));
            } else {
                mDataSet.get(position).setOrderQty(0);
            }

        }

        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }
    }



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