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. Button Resource Invalid?

Ok, I am trying to get a button to pop up. When I add :

 b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                int z = 5;
            }

Logcat tells me the resource is invalid.
Code:

 b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                int z = 5;
            }
}

XML: <-- I have a strong hunch I'm doing something wrong here.
(Everything is right up to
the button entry)

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

    <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
    />

    <Gallery android:id="@+id/gallery"
        android:background="#55000000"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"

        android:gravity="center_vertical"
        android:spacing="16dp"
    />

 <Button android:id ="@+id/buttonator"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_below="@id/switcher"

      android:text="Close"
/>
</RelativeLayout>

Thanks

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

2. Using an autocomplete filled with information from a database query

Hi all!!

I want to create an autocomplete that gets its information from a
database query.  I have a database method that returns the Cursor
(with name, value and date fields) I need, but I dont know how to
actually fill it, so it works as expected as the Contacts API demo:
http://code.google.com/android/samples/ApiDemos/src/com/example/android/apis/view/AutoComplete4.html

I want the autocomplete to show the name field from the database
query.

I read the following thread, but still haven't been able to figure it
out.

3. Help: thread stuck with error "still suspended after undo (s=1 d=1)"

4. How to get the position of a specific element in a spinner based on the element's text

5. Facebook notification app while we wait for a facebook app

6. mark the location on the map

7. Problem with viewing map in tabhost