ListView onItemClick混淆

时间:2012-07-26 03:21:27

标签: android listview adapter android-arrayadapter onitemclick

我一直试图解决这个问题一段时间,我在网上找不到任何帮助。基本上我只需要为项目设置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);
    }
}

任何帮助?

1 个答案:

答案 0 :(得分:0)

在字符串列表[]中,列出要在列表中显示的类的名称:

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

}