I have created a ListView whose children consist of a single EditText. However, when I click on the EditText, giving it focus, then notifyDataSetChanged()
, updating the ListView, then click on any of the EditTexts again, a cursor is drawn for every EditText in the list (Nexus 5, emulators, etc.)
On a Nexus 5
The following are my xml for the 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" />
Because I want the EditText to be able to gain focus, I followed some tips and pointers here: https://stackoverflow.com/a/4901683/499125. Specifically, I added android:windowSoftInputMode="adjustNothing"
to the Activity tag in AndroidManifest.xml so that list items can gain focus at all, creating this issue. Then I tried attaching the OnItemSelectedListener
to the listview as the selected answer suggested. However, these ghost cursors still exists.
@AnswerBot: I am using an ArrayAdapter, which holds 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();
}
}
You can hide the cursor for an EditText by calling setCursorVisible(false) on the EditText(s) in question.
Edit: For this you will 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With