EditText not rendering correctly when added as a header of ListView

by Don Oleary » Sun, 07 Jun 2009 06:05:07 GMT


Sponsored Links
 Hi Guys

I have come across a problem where EditText is not rendering correctly when
it's added as a header in a ListView.
Basically, I have a search box and a list of results from a DB. The Activity
extends ListActivity and I add the search
box as a header using ListView.addHeaderView(View v).

On first load, the EditText view acts as normal. Every time I type a
character, it renders in the EditText. On any subsequent load of the
ListView, the EditText view "appears" unresponsive. If, for example, I type
another character, it does not render in the EditText view.  However,
when the ListView is updated, the EditText view updates with the relevant
changes. I get the same behaviour if I change the orientation
of the screen. When I type in EditText, the characters are not rendered to
the view. If I change orientation however, the EditText view is updated.

Any help in this regard would be very much appreciated.

Below is a code example to illustrate the problem.

==============================

public class ResultList extends ListActivity {

    DBAdapter mDb;
    EditText mQuery;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mDb = new DBAdapter(this);
        mDb.open();

        setContentView(R.layout.search_list);
        renderUI();
        populateResults("all");

    }

    protected void renderUI(){

        TableLayout headerTable = new TableLayout(this);
        headerTable.setBackgroundColor(Color.WHITE);
        headerTable.setStretchAllColumns(true);

        TableRow row = new TableRow(this);
        row.setMinimumHeight(8);
        headerTable.addView(row);

        row = new TableRow(this);
        row.setGravity(Gravity.CENTER_VERTICAL);

        mQuery = new EditText(this);
        mQuery.setWidth(180);
        mQuery.setHint("Search ");
        row.addView(mQuery);

        ImageButton search = new ImageButton(this);
        Bitmap bitmap =
BitmapFactory.decodeResource(getResources(),R.drawable.search);
        search.setImageBitmap(bitmap);
        search.setBackgroundColor(0);
        search.setPadding(10, 0, 0, 0);

        search.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                populateResults(mQuery.getText().toString());
            }
        });

        row.addView(search);
        headerTable.addView(row);


        ListView list = (ListView)findViewById(android.R.id.list);
        list.addHeaderView(headerTable);

    }

    private void populateResults(String search){

         if(!mDb.getDatabase().isOpen())
             mDb.open();

        Cursor expCur = mDb.getResults(search);

        startManagingCursor(expCur);

        String[] from = new String[] { mDb.KEY_DATE_STRING_VALUE};
        int[] to = new int[] { R.id.result_row_date};


        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter adapter =
            new SimpleCursorAdapter(this, R.layout.result_rows, expCur,
from, to);

       setListAdapter(adapter);

    }

}

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. ImageView.VISIBLE/INVISIBLE

I was wondering how this works,

I can set ImageViews VISIBLE or INVISIBLE in OnCreate, but i can't
seem to do it anywhere else properly?
using code like "imgView.setVisibility(ImageView.VISIBLE);"
any pointers/advice?

Thanks,

David
--~--~---------~--~----~------------~-------~--~----~

2. ACTION_GET_CONTENT and Gallery

My app takes a picture and stores it either on the flash card or in
the MediaStore. When retreiving the gallery content with an
ACTION_GET_CONTENT intent doesn't show the new picture in either case.
However, after switching the phone off and on again, they do show up.
How do I force the Gallery to show my new pic?


--~--~---------~--~----~------------~-------~--~----~

3. IT HAS BEGUN! (ADC 2 judging)

4. WikiNote -apps-for-android build error

5. How to make an activity destory when user presses 'Back button?

6. Why are the save/restore lifecycle events called twice when switching the emulator from landscape to portrait?

7. How to make an activity destory when user presses 'Back button?