I am very new to android and ask to develop an Android application. I am working on Android 2.2. The problem that I encountered is that the client name should appear after clicking the "Add" button, as in the basic tutorial, but this does not work. After clicking the button, the name does not appear in the list.
Here are the codes:
public class MainActivity extends Activity implements OnClickListener, OnKeyListener { ArrayList<String> appointment; ArrayAdapter<String> aa; EditText editText1; Button addButton; ListView listView1; @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 help me. thanks in advance
source share