Quantcast
Viewing latest article 4
Browse Latest Browse All 601

Android EditText field doesn't allow user input in ListView

I've searched for many post on the internet for my issue but no one is helping me, so I try a new question.I have an Activity with a ListView containing in each row two TextViews (pizza_name,pizza_price) one EditText (pizza_quantity) and two buttons (add_pizza,remove_pizza), I use a custom adapter to fill the ListView.

I have implemented all the methods to make the buttons work properly, my problem is with the pizza_quantity EditText view:I want that the number showed in could be edited both via buttons and user input, well when a user tap on it, the keyboard appears, but the content of the EditText doesn't change.Here are the code of:

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>

the code in main activity (called MenuActivity) in which I set the adapter

onCreate:

@Overrideprotected 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);}

and this is ListAdapter

constructor:

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 method:

@Overridepublic 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;}

finally, here are two app screenshot

Just started activity

after tap on the first row EditText field

thank you in advance.


Viewing latest article 4
Browse Latest Browse All 601

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>