2

I am implementing a form using a ListView. Each row in the ListView contains an EditText associated with a TextView that describes the input type, and is hence defined:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

    <TextView android:id="@+id/textFormItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="16sp"
        android:ellipsize="end" android:singleLine="true" />

    <EditText android:id="@+id/editFormItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollHorizontally="true" />
</LinearLayout>

I am implementing a custom Adapter that subclasses SimpleAdapter. So far, the code looks like this:

public class FormAdapter extends SimpleAdapter {
    public static final String KEY = "key";

    static final String[] FROM = { KEY };
    static final int[] TO = { R.id.textFormItem };

    private static ICUApplication app;

    private FormAdapter(Context context, List<? extends Map<String, ?>> data) {
        super(context, data, R.layout.form_item, FROM, TO);

        setViewBinder(VIEW_BINDER);
    }

    public static FormAdapter createAdapter (Context ctx, Set<String> dataTypeSet) {
        List<Map<String,String>> data = 
                new ArrayList<Map<String,String>>(dataTypeSet.size());

        for (String dataType : dataTypeSet) {
            Map<String,String> map = new HashMap<String,String>();
            map.put(KEY, dataType);
            data.add(map);
        }

        app = (ICUApplication) ctx.getApplicationContext();

        return new FormAdapter(ctx, data);
    }

    static final SimpleAdapter.ViewBinder VIEW_BINDER = new SimpleAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Object data,
                String textRepresentation) {
            int viewId = view.getId();
            switch (viewId){
            case R.id.textFormItem:
                String key = (String) data;
                ((TextView) view).setText(app.getTypeNameForKey(key));
            }

            return true;
        }
    };
}

Some notes: the reason I implemented the view binder is that I want the ListView to display the user-friendly name for a data type (as given by app.getTypeNameForKey(key)) while storing each row internally by its KEY. Also, the Activity that uses these two classes, at the moment, only has code that sets a ListView to use this adapter.

Earlier, it was harder to figure out exactly what was happening because of EditText focus issues, which I solved using a question asked here earlier: Focusable EditText inside ListView. Currently, I have three items in the form. So, here is the odd bug:

  • If I tap the first EditText, the on-screen keyboard opens and focus shifts to the third EditText. Anything I type into this third EditText will then get moved back to the first EditText after I close the keyboard.
  • If I tap the second EditText, the keyboard opens and focus again shifts to the third EditText. Anything I type into this third box will get moved, not to the second EditText, but the first one again.
  • If I tap the third EditText, focus doesn't shift, but after closing the keyboard, the input gets moved to the first EditText.
  • And a whole host of other odd issues related to the above three that I won't list here because it would make this already long question excessively long.

Any ideas as to why this is happening and how I can fix it?

Community
  • 1
  • 1
kibibyte
  • 3,007
  • 5
  • 30
  • 40

0 Answers0