I'm working on creating a dictionary and I'm trying to create ListMenu dynamically as it is created automatically while filtering words from database alphabetically. I created a ListMenu manually, but cannot turn it into dynamic one. Does anyone have an idea how to create the ListMenu dynamically?
Here's my code:
package ge.gtug;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Settings extends ListActivity{
String listItems[] = {"AboutUs", "AboutApp", "Feedback"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Settings.this, android.R.layout.simple_list_item_1, listItems));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
String koba = listItems[position];
super.onListItemClick(l, v, position, id);
try{
Class myClass1 = Class.forName("ge.gtug." + koba);
Intent myIntent1 = new Intent(Settings.this, myClass1);
startActivity(myIntent1);
}catch(ClassNotFoundException e){
e.printStackTrace();
try {
Class myClass2 = Class.forName("ge.gtug." + koba);
Intent myIntent2 = new Intent(Settings.this, myClass2);
startActivity(myIntent2);
}catch(ClassNotFoundException o){
o.printStackTrace();
}
}
}
}
Instead of using an Array for storing data use a List (ArrayList, Vector, or similar). And whenever you alter the data collection you need to inform the ArrayAdapter that the data has changed - to notify the array adapter that the list has changed you call the method
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Settings.this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
listItems.add("new string");
adapter.notifyDataSetChanged();
http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged%28%29