Android EditText字段不允许在ListView中输入用户

时间:2016-07-04 08:49:10

标签: android listview android-edittext

我在互联网上为我的问题搜索过很多帖子,但没有人帮我,所以我尝试了一个新问题。 我有一个带有ListView的Activity,每行包含两个TextViews(pizza_name,pizza_price)一个EditText(pizza_quantity)和两个按钮(add_pizza,remove_pizza),我使用自定义适配器来填充ListView。

我已经实现了使按钮正常工作的所有方法,我的问题在于pizza_quantity EditText视图: 我希望显示的数字可以通过按钮和用户输入进行编辑,当用户点击它时,键盘会出现,但EditText的内容不会改变。 以下是代码:

single_row_layout:

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


<TextView
    android:id="@+id/pizzaname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:paddingLeft="8dp"
    android:textSize="18sp"
    android:textStyle="bold"
    android:hint="nome_pizza"
    android:layout_centerVertical="true"/>

<TextView
    android:id="@+id/pizzaprice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/editTextQuantity"
    android:textSize="18sp"
    android:textStyle="bold"
    android:hint="prezzo"
    android:layout_centerVertical="true"
    />

        <Button
            android:layout_width="50sp"
            android:layout_height="wrap_content"
            android:id="@+id/ib_addnew"
            android:layout_gravity="bottom"
            android:hint="+"
            android:layout_toLeftOf="@+id/ib_remove"
            android:background="#00ffffff"
            />

        <EditText
            android:layout_width="50dp"
            android:layout_height="48dp"
            android:text="0"
            android:id="@+id/editTextQuantity"
            android:layout_toLeftOf="@+id/ib_addnew"
            android:textAlignment="center"
            android:inputType="numberDecimal" />

        <Button
            android:layout_width="50sp"
            android:layout_height="wrap_content"
            android:id="@+id/ib_remove"
            android:hint="-"
            android:layout_marginRight="5sp"
            android:layout_alignParentRight="true"
            android:background="#00ffffff"

            />

</RelativeLayout>

主要活动中的代码(称为MenuActivity),我设置了适配器

的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_layout);
    Intent i = getIntent();

    order = new Order();
    Server server = StubServer.getIstance(this);

    pizzaNames = server.getPizzaNames();
    pizzaPrices = server.getPizzaPrices();

    if(i.getBooleanExtra("session",false)){
        order=(Order)i.getSerializableExtra("order");
    }

    listView = (ListView) findViewById(R.id.mylist);
    listAdapter = new ListAdapter(this, pizzaNames, pizzaPrices,order.pizzaQuantity);

    listView.setAdapter(listAdapter);
    listAdapter.setCustomButtonListener(this);


}

这是ListAdapter

构造函数:

public ListAdapter(Context context, String[] listViewItems,String[] prices,ArrayList<Integer> q) {
    this.context = context;
    this.listViewItems = listViewItems;
    this.prices=prices;
    this.quantity=q;

    if(quantity.size()==0) {
        for (int i = 0; i < listViewItems.length; i++) {
           quantity.add(0);
        }
    }
}

getView方法:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row;
    final ListViewHolder listViewHolder;
    if(convertView == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.activity_custom_listview,parent,false);
        listViewHolder = new ListViewHolder();
        listViewHolder.pizzaName = (TextView) row.findViewById(R.id.pizzaname);
        listViewHolder.price=(TextView) row.findViewById(R.id.pizzaprice);
        listViewHolder.btnPlus = (Button) row.findViewById(R.id.ib_addnew);
        listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editTextQuantity);
        listViewHolder.btnMinus = (Button) row.findViewById(R.id.ib_remove);
        row.setTag(listViewHolder);
    }
    else
    {
        row=convertView;
        listViewHolder= (ListViewHolder) row.getTag();
    }


    try{
        //HashMap<String, String> map = listQuantity.get(position);
        //quantity[position]+""

        listViewHolder.edTextQuantity.setText(quantity.get(position)+"");

        //holder.item_name.setText(map.get("name"));
        //holder.order_qty.setText(map.get("quantity"));

        //Log.v("Available or not", ""+map.get("available"));


    }catch(Exception e){
        e.printStackTrace();
    }

    listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (customButtonListener != null) {
                customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, 1);
                int tmp=quantity.get(position);
                quantity.set(position,tmp+1);
                //quantity[position]=[position]+1;
            }

        }
    });
    //listViewHolder.edTextQuantity.setText("0");
    listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (customButtonListener != null) {
                customButtonListener.onButtonClickListener(position,listViewHolder.edTextQuantity,-1);
                if(quantity.get(position)>0)
                    quantity.set(position, quantity.get(position) - 1);
            }
        }
    });
    listViewHolder.pizzaName.setText(listViewItems[position]);
    listViewHolder.price.setText(prices[position]);
    if(position%2==0){
        row.setBackgroundColor(Color.LTGRAY);
    }
    return row;
}
finilly,这里有两个应用截图

Just started activity

after tap on the first row EditText field

提前谢谢。

1 个答案:

答案 0 :(得分:0)

适配器中的

更改代码如下

  try{


    listViewHolder.edTextQuantity.setText(String.valueOf(quantity.get(position)));

    //holder.item_name.setText(map.get("name"));
    //holder.order_qty.setText(map.get("quantity"));

    //Log.v("Available or not", ""+map.get("available"));


}catch(Exception e){
    e.printStackTrace();
}

listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener()

{
    @Override
    public void onClick (View v){
    if (customButtonListener != null) {
        customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, 1);
        int tmp = Integer.parseInt(listViewHolder.edTextQuantity.gettext().tostring())
        quantity.set(position, tmp + 1);
        //quantity[position]=[position]+1;
        listViewHolder.edTextQuantity.settext(String.valueOf(temp + 1));
    }

}
}

);
//listViewHolder.edTextQuantity.setText("0");
listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener()

{
    @Override
    public void onClick (View v){
    if (customButtonListener != null) {
        customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, -1);
        if (quantity.get(position) > 0)
            int tmp = Integer.parseInt(listViewHolder.edTextQuantity.gettext().tostring())
        quantity.set(position, tmp - 1);
        //quantity[position]=[position]+1;
        listViewHolder.edTextQuantity.settext(String.valueOf(temp - 1));
    }
}
}

);
listViewHolder.pizzaName.setText(listViewItems[position]);
listViewHolder.price.setText(prices[position]);
if(position%2==0)

{
    row.setBackgroundColor(Color.LTGRAY);
}