How can I add items in a listview for Android App?

15,713

You don't need to keep an ArrayList and an ArrayAdapter

the ArrayAdapter will actually hold the List of Strings inside of it.

get rid of your ArrayList and change this line

this.appointment.add(item);

to

this.aa.add(item);

and you should be good to go.

I am also interested in why you use the fully qualified names of all your variables. I definitely get that sometimes the scope of your various classes will make it necessary to use the "this." infront of things, but it seems to me from your example that you would be fine without the "this."

Is this just something you do for clarity when reading your code, or does it serve some other purpose?

EDIT: p.s. @Selvin totally nailed this one, if they post an answer please accept theirs instead of mine.

Share:
15,713
Cindy Chong
Author by

Cindy Chong

Updated on June 30, 2022

Comments

  • Cindy Chong
    Cindy Chong almost 2 years

    I am quite new in android and being requested to develop an app for android. I am working on Android 2.2. The problem I have encountered is that the name of the client should appear once I click the button 'Add' as in the basic tutorial, but it do not work that way. The name did not show on the listview after clicking the button.

    Here the codes:

    public class MainActivity extends Activity implements OnClickListener,       
    OnKeyListener  {
    
        ArrayList<String> appointment;
        ArrayAdapter<String> aa;
    
        EditText editText1;
        Button addButton;
        ListView listView1;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            editText1 = (EditText)findViewById(R.id.editText1);
            addButton = (Button)findViewById(R.id.addButton);
            listView1 = (ListView)findViewById(R.id.listView1);
    
            addButton.setOnClickListener(this);
            editText1.setOnKeyListener(this);
    
            appointment = new ArrayList<String>();
            aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,  
                    appointment);
            listView1.setAdapter(aa);
    
        }
    
        private void addItems(String item){
    
            if (item.length()>0){
                this.appointment.add(item);
                this.aa.notifyDataSetChanged();
                this.editText1.setText("");
            }
    
        }
    
        public void onClick(View v) {
    
            if(v==this.addButton){
                this.addItems(this.editText1.getText().toString());
            }
    
        }
    
    
    
        public boolean onKey(View v, int keyCode, KeyEvent event) {
    
            if (event.getAction()==KeyEvent.ACTION_DOWN &&   
                    keyCode==KeyEvent.KEYCODE_DPAD_CENTER)
                this.addItems(this. editText1.getText().toString());
    
            return false;
        }
    
    
    } 
    

    main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="wrap_content"     
    android:weightSum="1">
    
    <ImageView 
    
    android:id="@+id/phones_icon"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/user"
    android:layout_margin="20dp"
    />
    
    <Button
    android:text="Add New Appoinments"
    android:id="@+id/addButton"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:typeface="sans"
      ></Button>
    
    <EditText android:id="@+id/editText1" android:layout_height="wrap_content"   
    android:layout_width="match_parent" android:inputType="textPersonName">
        <requestFocus></requestFocus>
    </EditText>
    
    <ListView android:id="@+id/listView1" android:layout_width="wrap_content"  
    android:layout_height="252dp"></ListView>  
    
    </LinearLayout>
    </ScrollView>
    

    Please guide me. Thanks in advance