Guest User

Untitled

a guest
May 4th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. Android how to make EditText editable inside a ListView
  2. @Override
  3. protected void layoutChildren() {
  4. super.layoutChildren();
  5. final EditText tv = (EditText)this.getTag();
  6. if (tv != null)
  7. {
  8. Log.d("RUN", "posting delayed");
  9. this.post(new Runnable() {
  10.  
  11. @Override
  12. public void run() {
  13. Log.d("RUN", "requesting focus in runnable");
  14. tv.requestFocusFromTouch();
  15. tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , tv.getWidth(), tv.getHeight(), 0));
  16. tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , tv.getWidth(), tv.getHeight(), 0));
  17. }
  18. });
  19. }
  20. }
  21.  
  22. public class EditTextListAdapter extends BaseAdapter {
  23.  
  24. /* notes list */
  25. private ArrayList<SomeData> mSomeData = null;
  26. /* layout inflater instance */
  27. private LayoutInflater mInflater;
  28. /* activity context */
  29. private Context mContext;
  30.  
  31. /* ensure that this constant is greater than the maximum list size */
  32. private static final int DEFAULT_ID_VALUE = -1;
  33. /* used to keep the note edit text row id within the list */
  34. private int mNoteId = DEFAULT_ID_VALUE;
  35.  
  36. /**
  37. * EditTextListAdapter adapter class constructor
  38. *
  39. * @param context activity context
  40. */
  41. public FirstTimesListAdapter(Context context) {
  42.  
  43. /* get a layout inflater instance */
  44. mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  45. /* load the data */
  46. mSomeData = SomeData.instance().getData();
  47. /* keep the context */
  48. mContext = context;
  49. }
  50.  
  51. /**
  52. * Returns the selected item
  53. *
  54. * @return selected item
  55. */
  56. public int getSelectedItem() {
  57. return mSelectedItem;
  58. }
  59.  
  60. public int getCount() {
  61. return mSomeData.size();
  62. }
  63.  
  64. public Object getItem(int i) {
  65. return mSomeData.get(i);
  66. }
  67.  
  68. public long getItemId(int i) {
  69. return i;
  70. }
  71.  
  72. public View getView(final int index, View recycledView, ViewGroup viewGroup) {
  73.  
  74. ViewHolder viewHolder;
  75.  
  76. if (recycledView == null) {
  77. /* inflate the list item */
  78. recycledView = mInflater.inflate(R.layout.listview_item_with_edit_text, viewGroup, false);
  79. /* get link to the view holder views */
  80. viewHolder = new ViewHolder();
  81. viewHolder.note = (EditText) recycledView.findViewById(R.id.first_times_note);
  82. recycledView.setTag(viewHolder);
  83. } else {
  84. /* reuse the same views we load the first time */
  85. viewHolder = (ViewHolder) recycledView.getTag();
  86. }
  87.  
  88. /* display some notes */
  89. viewHolder.note.setText(mSomeData.getNotes());
  90.  
  91. /* if the last id is set, the edit text from this list item was pressed */
  92. if (mNoteId == index) {
  93.  
  94. /* make the edit text recive focus */
  95. viewHolder.note.requestFocusFromTouch();
  96. /* make the edit text's cursor to appear at the end of the text */
  97. viewHolder.note.setSelection(viewHolder.note.getText().length());
  98.  
  99. /* reset the last id to default value */
  100. mNoteId = DEFAULT_ID_VALUE;
  101. }
  102.  
  103. /* set a touch listener on the edit text just to record the index of the edit text that was pressed */
  104. viewHolder.note.setOnTouchListener(new View.OnTouchListener() {
  105. public boolean onTouch(View view, MotionEvent motionEvent) {
  106. if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
  107. /* get the index of the touched list item */
  108. mNoteId = index;
  109. }
  110. return false;
  111. }
  112. });
  113.  
  114. return recycledView;
  115. }
  116.  
  117. static class ViewHolder {
  118. /* note input */
  119. EditText note;
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment