I have created this simple android Activity to demonstrate my problem.
I just want the screen to have a textInput, and a button. Below these two, I want to create a ListView if the button is pressed (The button basically calls some method and gets an array of String. I want the ListView to display this array.
So its a plain Screen, a button, a text input, when button pressed it calls a method and receives an array of string and want to Print the list underneath them.
public class TagListViewer extends ListActivity {
private Button clickBtn;
EditText textInput;
String[] resultStr = {"a", "b", "c"}; //Ideally would want this inside the button OnClickListener... but couldn't bcz I needed one for the Array adapter.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tagselection);
clickBtn = (Button) findViewById(R.id.CreatePL);
clickBtn.setText("Search");
textInput = (EditText) findViewById(R.id.textInput);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, resultStr);
setListAdapter(adapter);
clickBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
adapter.add("ABC"); //This I could use the array I get to add its elements
adapter.notifyDataSetChanged();
}
});
}
}
I’m not certain what your question is, but I noticed that you are trying to add items to a primitive String array and creating two different adapters… I have a hunch about the problem. Look at the simple changes below:
Added from comments
A ListActivity is required to have a ListView with the id
android.R.id.listin its layout: