2012-02-28 8 views
0

私は辞書の作成に取り組んでいます。データベースからアルファベット順に単語をフィルタリングしながら自動的に作成されるので、ListMenuを動的に作成しようとしています。 私はListMenuを手作業で作成しましたが、それを動的なものにすることはできません。誰もどのようにListMenuを動的に作成するアイデアを持っていますか?ListMenuを動的に作成する方法は?

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(); 
      } 
    } 
} 

} 

答えて

0

代わりのデータがリスト(ArrayListに、ベクトル、または類似)を使用する格納するための配列を使用して:

は、ここに私のコードです。そして、あなたはデータが変更されたことArrayAdapterを知らせるために必要なデータの収集変えるたび - リストは、あなたが

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

メソッドを呼び出して変更された配列のアダプタに通知するを
関連する問題