Question

I could have sworn this worked before but now when I test it I have a problem. I have a custom listview that has a series of textviews of varying height. In Android 4.x it all works perfectly. On my old 2.3 phone the list appears but it is only one line high so most of the textview can't be seen. It behaves as it should though, it scrolls and I can delete items by long pressing.

I have tried just changing the TextView height to a fixed value and this works on 4.x but makes no difference on 2.3

Here are the relevant bits of code: notam_list.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listOfNotams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
 />

mylistviewstyle.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listviewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:textSize="12sp" />

snippets from the activity:

public class NotamList extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notam_list);

final ListView listOfNotams = (ListView) findViewById(R.id.listOfNotams);

....
}

final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.mylistviewstyle, NOTAMS);

    listOfNotams.setAdapter(adapter);

    listOfNotams.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, final View view, int position, long id) {
            final String item = (String) parent.getItemAtPosition(position);

            Pattern pattern;
            Matcher matcher;
            if (item.substring(4, 5).matches("-")) {
                pattern = Pattern.compile("[A-Z][A-Z][A-Z][A-Z]-[A-Z][0-9]+/[12]\\d");
                matcher = pattern.matcher(item);
                matcher.find();
                String notamSelected = matcher.group(0);
                HiddenNotam hiddenNotam = new HiddenNotam(notamSelected, 1);

                if (db.checkHiddenNotamExists(notamSelected)) {
                    db.deleteHiddenNotam(hiddenNotam);

                    Toast toast = Toast.makeText(getApplicationContext(), notamSelected + " Un-Hidden", Toast.LENGTH_SHORT);
                    toast.show();
                } else {
                    db.addHiddenNotam(hiddenNotam);
                    if (hideNotams) {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {

                            // POSSIBLE SOLUTION TO THIS
                            // http://nineoldandroids.com/

                            NOTAMS.remove(item);
                            adapter.notifyDataSetChanged();
                        } else {
                            view.animate().setDuration(2000).alpha(0).withEndAction(new Runnable() {

                                @Override
                                public void run() {
                                    NOTAMS.remove(item);
                                    adapter.notifyDataSetChanged();
                                    view.setAlpha(1);
                                }

                            });
                        }
                    }
                    Toast toast = Toast.makeText(getApplicationContext(), notamSelected + " Hidden", Toast.LENGTH_SHORT);
                    toast.show();
                }
            } else {
                Toast toast = Toast.makeText(getApplicationContext(), "This can't be hidden", Toast.LENGTH_SHORT);
                toast.show();
            }

            return true;
        }

    });
}

private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

}

Any ideas?

EDIT: I have also just discovered that in my help screen which is just a series of TextViews that the text isn't wrapping. Each TextView is just turning into one big line without horizontal scrolling of course. I have written other apps the exact same way (as far as I can tell) and I haven't had this problem before?

Was it helpful?

Solution

Looks like it was a phone problem. I have completely uninstalled it and then re-installed and it is working again. Thanks for looking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top