public class MyActivity extends Activity {
Context context;
List<String> tasks;
ArrayAdapter<String> adapter;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
tasks = new ArrayList<String>();
Button add = (Button) findViewById(R.id.button);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
editText.setVisibility(1);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, 0);
String value = editText.getText().toString();
tasks.add(value);
adapter = new ArrayAdapter<String>(context,R.id.listView,tasks);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
});
}
}
Here, i am getting value from the user. and i am trying to add it to list view dynamically. But, it is showing an error called "Unfortunatly app is closed". it is failing in adding string value to the tasks variable. tasks is a list of string.
tasks.add(value);
if i try to add something else also it is failing. like,
tasks.add("something");
i don't know what is the problem. but i am sure that it is failing in this line, because if i remove this line, my app is working fine. if someone knows why it is failing please let me know. thanks in advance.
adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item1,tasks)also there is not need to set the adapter to listview everytime. just calladapter.notifydataSetChangedto refresh listview