How to hide multiple cursors while displaying EditText in ListView?

I created a ListView whose children consist of one EditText. However, when I click on EditText, giving it focus, then notifyDataSetChanged() , updating ListView, then click on any of the EditTexts again, the cursor is drawn for each EditText in the list (Nexus 5, emulators, etc.),

In Nexus 5 Nexus 5

Listed below are my xml for list and list item.

activity_main.xml

 <ListView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.zlistfocus.MainActivity" tools:ignore="MergeRootFrame" /> 

listview_item.xml

 <?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="40dp" android:paddingBottom="4dp" android:paddingLeft="8dp" android:paddingRight="8dp" android:paddingTop="4dp" /> 

Since I want EditText to be able to get the focus, I took advantage of the tips and pointers here: https://stackoverflow.com/a/312220/ In particular, I added android:windowSoftInputMode="adjustNothing" to the Activity tag in AndroidManifest.xml so that list items can get focus at all, creating this problem. Then I tried to bind OnItemSelectedListener to the list, as suggested by the selected answer. However, these ghost cursors still exist.

@AnswerBot: I use an ArrayAdapter that contains a list of integers.

 public class MainActivity extends ActionBarActivity { private ListView lv; private ArrayAdapter<Integer> adapter; private List<Integer> data = new ArrayList<Integer>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); adapter = new ArrayAdapter<Integer>(this, R.layout.listview_item, R.id.tv, data); lv.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 0, "Refresh"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == 1) { getSomeData(); return true; } return super.onOptionsItemSelected(item); } private void getSomeData() { if (data.isEmpty()) { for (int i = 0; i < 20; i++) { data.add(i); } } else { int start = data.get(data.size() - 1) + 1; for (int i = 0; i < 20; i++) { data.set(i, start + i); } } adapter.notifyDataSetChanged(); } } 
+6
source share
1 answer

You can hide the cursor for EditText by calling setCursorVisible (false) in the EditText (s) question.

Edit: For this, you probably want to use a custom adapter ( BaseAdapter ), and in the getView method, do something like this:

 @Override public View getView(final int position, View convertView, ViewGroup parent) { ... editText.setCursorVisible(editText.hasFocus()); return convertView; } 
+3
source

All Articles