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. runs on emulator crashs on galaxy s

I have an app the runs fine on my emulator and my incredible with
android 2.2. Users say my app won't run on the galaxy s. What options
exist to troubleshooting this? Is there a special agreement for galaxy?

-- 

2. Audio Track play music problem

Hi all
I'm an android starter
I tused AudioTrack to play music
But it just tell me filenotfoundexeception
I try to play mp3 wav ogg (all in raw dir)
I've no idea where is wrong?
here are my code ~
Could any one tell me why?
TKS in advance :)

public class mainActivity extends Activity
{
        private static final String Tag = "audioRecorder";
        private static final boolean log = true;

        AudioTrack track = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //if(AudioTrack.MODE_STATIC != mode && AudioTrack.MODE_STREAM !
= mode)
                //throw new InvalidParameterException();

                //String audioFilePath = "R.raw.music";
                long fileSize = 0;
                long bytesWritten = 0;
                int bytesRead = 0;
                int bufferSize = 0;
                byte[] buffer;
                //AudioTrack track = null;
                int sampleRateHz = 8000;//22050
                int audioFormat = AudioFormat.CHANNEL_CONFIGURATION_MONO;
                //int audioFormat = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
                //int audioEncodingFormat= MediaRecorder.AudioEncoder.AMR_NB;
                int audioEncodingFormat= AudioFormat.ENCODING_PCM_16BIT;
                //int audioEncodingFormat= AudioFormat.ENCODING_PCM_8BIT;
                //int mode = AudioTrack.MODE_STATIC;
                int mode = AudioTrack.MODE_STREAM;

                File audioFile = new File("R.raw.test.ogg");
                fileSize = audioFile.length();

                if(log)Log.d(Tag,"Buffer size("+ mode +")");
                if(AudioTrack.MODE_STREAM == mode)
                  bufferSize =
AudioTrack.getMinBufferSize(sampleRateHz,audioFormat,audioEncodingFormat);
                else //AudioTrack.MODE_STATIC
                  bufferSize = (int)fileSize;

                buffer = new byte[(int)bufferSize];

                if(log)Log.d(Tag,"track setting");
                track = new
AudioTrack(AudioManager.STREAM_MUSIC,sampleRateHz,
                                audioFormat,audioEncodingFormat,
                                bufferSize,mode);

                // in stream mode,
                // 1. start track playback
                // 2. write data to track

                if(AudioTrack.MODE_STREAM == mode)
                {
                  if(log)Log.d(Tag,"MODE_STREAM:track.play()");
                  track.play();
                }
                FileInputStream audioStream = null;
                try{
                audioStream = new FileInputStream(audioFile);//"R.raw.music"
                }catch(FileNotFoundException e) {
                e.printStackTrace();
                Log.e(Tag,"Error:" + e.toString());
                }

                if(log)Log.d(Tag,"audioStream read");
                while(bytesWritten < fileSize)
                {
                  try
                  {
                     bytesRead = audioStream.read(buffer,0,bufferSize);
                  }catch(IOException e){
                          Log.e(Tag,"Error:" + e.toString());
                   }
                   if(log)Log.d(Tag,"track write");
                   bytesWritten += track.write(buffer,0,bytesRead);
                }
        // in static mode,
                // 1. write data to track
                // 2. start track playback

                if(AudioTrack.MODE_STATIC == mode)
                {
                  if(log)Log.d(Tag,"MODE_STATIC:track.play()");
                  track.play();
                }

        }
    }

-- 

3. Tabs in android

4. soap in android

5. Android App Licensing Mechanism Still Easy to Break!!

6. Converting binary JPEG data to Bitmap

7. Application Update