如何在Android中使用数组适配器动态创建列表菜单?

时间:2015-05-23 14:54:02

标签: android android-arrayadapter android-adapter

我正在创建一个列表菜单,每次调用我创建它的类时都需要更改。但一旦创建,它仍然是相同的。我尝试使用adapter.clear()但它也不起作用。有人可以在下面的代码中找到编辑。

public class Menu extends ListActivity {
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
 /*   for( int i = 0; i< j ; i++) {
        classes[i] = Http.sarr[i];
        ref[i] = Http.name[i];
    }*/
    super.onCreate(savedInstanceState);
    adapter = new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, Http.sarr);
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String cheese = Http.name[position];
    String link = "http://www.codeforces.com/problemset/problem/";
    link = link.concat(cheese);
    Intent ourIntent = new Intent("android.intent.action.VIEW", Uri.parse(link));
    startActivity(ourIntent);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    //finish();
    adapter.clear();
    adapter.notifyDataSetChanged();
    Class ourClass = null;
    try {
        ourClass = Class.forName("com.example.avinash.myapplication.Mainapp");
        Intent ourIntent = new Intent(Menu.this, ourClass);
        startActivity(ourIntent);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

}

0 个答案:

没有答案