Question

I've been trying to figure this out for a while now and I can't find any help online. Basically I just need to have an onItemClickListener for the items. All they will do is start a new intent to a new activity.

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

Any help?

Was it helpful?

Solution

In string list[] put down the names of your classes you want to appear on the 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();
    }
}   

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top