Focus on EditText in ListView when block descendants (Android)

后端 未结 5 1693
一整个雨季
一整个雨季 2020-12-01 21:36

I have a customized ListView. Each row has an EditText, Buttons & TextView. In order to make the ListView items clickable I have kept

5条回答
  •  暖寄归人
    2020-12-01 22:21

    main.xml

    
    
    

    Create ArrayList add data into the arraylist.

    lv=(ListView)findViewById(R.id.listView1);
    lv.setItemsCanFocus(true);
    
    for(int i=0;i<30 data-blogger-escaped-br="" data-blogger-escaped-i=""> list.add(i);
    }
    

    1)create adapter for the listview and set the position as tag for the edittext.

    2)Normally,when scrolling the item position will change.So,you have to get the edittext tag and set it into the edittext id.from that you can avoid the change of the item position.

    holder.caption = (EditText) convertView
    .findViewById(R.id.editText12);
    holder.caption.setTag(position); 
    holder.caption.setText(list.get(position).toString());
    convertView.setTag(holder);
    
    }else {
    holder = (ViewHolder) convertView.getTag();
    }
    int tag_position=(Integer) holder.caption.getTag();
    holder.caption.setId(tag_position); 
    

    Finally,add the text watcher to the edittext and store the changes into correct position in the list.

    holder.caption.addTextChangedListener(new TextWatcher() {
    
                 @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                          final int position2 = holder.caption.getId();
                          final EditText Caption = (EditText) holder.caption;
                          if(Caption.getText().toString().length()>0){
                           list.set(position2,Integer.parseInt(Caption.getText().toString()));
                          }else{
                           Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
                          }
    
                      }
    
                 @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub
                }
    
                 @Override
                public void afterTextChanged(Editable s) {
    
                }
    
             });
    

    Refer this link..http://velmuruganandroidcoding.blogspot.in/2014/08/edittext-in-listview-android-example.html

提交回复
热议问题