Letting User add to ListView from EditText
<p>Hi I've been having some trouble trying to get a list view I created to add whatever it is the user inputs into the EditText view. The contents should be added when the user presses the Add button but the program just crashes, not to mention running really slowly. I haven't added the entire code below only the piece I think is relevant.</p>

<pre><code>public class ListViewActivity_Two extends Activity 
{
EditText edit;          Button add;
final List&lt;String&gt; data = new ArrayList&lt;String&gt;();

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

    data.add( "Hello" );

    edit = ( EditText )findViewById( R.id.editTxt ); 
    add = ( Button ) findViewById( R.id.add );
    add.setOnClickListener( new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            data.add( edit.getText().toString() );
        }
    }); 

    /**
    for( int i = 1; i &lt;= 10; i++ )
    {
        data.add( String.format( "Item %d", i ) );
    }
    */

    // Declare an instance of our custom adapter class
    CustomAdapter adapter = new CustomAdapter( this, data );

    ListView listView = ( ListView )findViewById( android.R.id.list ); 
    listView.setAdapter( adapter );

    listView.setOnItemClickListener( new OnItemClickListener()
    {

        @Override
        public void onItemClick( AdapterView listview, View v, int pos, long id ) 
        {
            TextView textView = (TextView)v.findViewById( android.R.id.text1 );
            toast( (String) textView.getText() );
        }

    });

}
</code></pre>

<p>The data.add() is just there to make sure I can actually add to the ListView, I can. I do the same thing in the onClickButton so why is this happening? Also I am right to declare the creation of the views above the onCreate method or do they have to be within it to work correctly?</p>

<p><em><strong>Edit</em></strong></p>

<p>Here's the CustomAdapter Class</p>

<pre><code>package com.stylingandroid.listview;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends BaseAdapter
{
private final Context context;
private final List&lt;String&gt; items;

public CustomAdapter( Context context, List&lt;String&gt; items )
{
    this.context = context;
    this.items = items;
}

@Override
public int getCount() 
{   
    return items.size();
}

@Override
public Object getItem(int position) 
{
    return items.get( position );
}

@Override
public long getItemId(int position) 
{
    return getItem( position ).hashCode();
}

/**
 * Method getView() below is inefficent because inflating XML layouts is memory expensive
 * because it involves parsing XML and then instantiating objects to represent all of the Views
 * in the view hierarchy
 * 
 * see: http://blog.stylingandroid.com/archives/632
 * 
 * for more details.
 * 
 * The more efficient code is below it!


@Override
public View getView( int position, View convertView, ViewGroup parent ) 
{
    LayoutInflater inflater = ( LayoutInflater )context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View v = inflater.inflate( R.layout.item, parent, false );

    final String item = ( String ) getItem( position );
    TextView tv = ( TextView )v.findViewById( android.R.id.text1 );
    tv.setText( item );

    ImageView iv = ( ImageView )v.findViewById( R.id.imageView );
    iv.setOnClickListener( new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            Toast.makeText( context, String.format( "ImageClicked: %s", item ), Toast.LENGTH_SHORT ).show();
        }

    });

    return v;
}

*/

@Override
public View getView( int position, View convertView, ViewGroup parent ) 
{
    /**
     * this method differs from the one above because, here, all we are doing is inflating a new view
     * if convertView is null. This significantly reduces the amount of object deletion and instantiation
     * that's required, and vastly improves our scrolling performance particularly on low powered devices
     */
    View v = convertView;

    if( v == null )
    {
        LayoutInflater inflater = ( LayoutInflater )context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        v = inflater.inflate( R.layout.item, parent, false );
    }

    final String item = ( String ) getItem( position );
    TextView tv = ( TextView )v.findViewById( android.R.id.text1 );
    tv.setText( item );

    ImageView iv = ( ImageView )v.findViewById( R.id.imageView );
    iv.setOnClickListener( new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            Toast.makeText( context, String.format( "ImageClicked: %s", item ), Toast.LENGTH_SHORT ).show();
        }

    });

    return v;
}

}
</code></pre>

<p>BTW i tried to change the emulator for the program to launch on to API level 7; it totally wrecked the program. Now at every R.whatever I get an error. does it matter that I changed it in the Manifest file to 7 too? Final thing I cleaned the project but that doesn't make a difference - what's the problem here?</p>
