I am a beginner and I want to add items in my Predefined array
public class MainActivity extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
;
String listItem[]={"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE"};
ArrayAdapter<String> adapter;
EditText et;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItem);
setListAdapter(adapter);
et=(EditText) findViewById(R.id.editText);
}
public void addItems(View v) {
String data=et.getText().toString();
listItem.add(""+data);
adapter.notifyDataSetChanged();
}
}
Illusionist, I've felt your pain as a beginner and have struggled with these exercises myself. The above advice from MH regarding using a list and adding straight to the adapter is correct. I've included an altered version of the exercise, but it basically does what you want it to do. I've added a couple of buttons, one to add a new item to the list and one to exit the application. Both have "onClick" added in the xml layout file for the main activity.
See if you can follow what I've done and let me know if you have any questions or concerns...
}
The associated xml layout file looks like...
Note: be careful of the ListView id; it has to be the way you see it above when using ListActivity... http://www.vogella.com/articles/AndroidListView/article.html