0

我一直在尝试解决这个问题,但在网上找不到任何帮助。基本上我只需要为这些项目设置一个 onItemClickListener。他们所要做的就是开始一项新活动的新意图。

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MyAppActivity extends Activity {
/** Called when the activity is first created. */
private ListView list1;
private String Cats[] = { "Hardware", "Commands", "Libraries","Tutorials", "Arduino License"};
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list1 = (ListView) findViewById(R.id.ListView01);
    // By using setAdpater method in listview we an add string array in
    // list.
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, Cats);
    list1.setAdapter(adapter);
    }
}

有什么帮助吗?

4

1 回答 1

0

在字符串 list[] 中,输入您希望出现在列表中的类的名称:

public class Menu extends ListActivity{

String list[] = {"(names of classes)"};


protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1,list));
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    try{
    String classlist = list[position];
    Class myclass = Class.forName("(project name)" + classlist);
    Intent intent = new Intent(Menu.this,myclass);
    startActivity(intent);
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }
}   

}

于 2012-07-26T03:27:08.360 回答