在android开发中的Listview

时间:2014-03-15 21:41:02

标签: android android-listview

我试图在菜单中创建列表视图。我怎样才能让他们工作?我的意思是点击它们我想去其他活动。这是源代码:

public class MenuActivity extends Activity {


    private ListView MenuList;

    String classes[] = {"NewReviewActivity" };
    public void onCreate(Bundle menuinstance){
        super.onCreate(menuinstance);
        setContentView(R.layout.menu);

        //getActionBar().setDisplayHomeAsUpEnabled(true);

        MenuHelper menu_data[] = new MenuHelper[] {
                new MenuHelper(R.drawable.ic_menu_1, this.getString(R.string.menuItemTitle_1)),
                new MenuHelper(R.drawable.ic_menu_2, this.getString(R.string.menuItemTitle_2)),
                new MenuHelper(R.drawable.ic_menu_3, this.getString(R.string.menuItemTitle_3)),
                new MenuHelper(R.drawable.ic_menu_4, this.getString(R.string.menuItemTitle_4)),
                new MenuHelper(R.drawable.ic_menu_5, this.getString(R.string.menuItemTitle_5)),
                new MenuHelper(R.drawable.ic_menu_6, this.getString(R.string.menuItemTitle_6))
        };

        final MenuAdapter menuAdapter = new MenuAdapter(this, R.layout.menu_row, menu_data);


        MenuList = (ListView) findViewById(R.id.menuListView);
        MenuList.setAdapter(menuAdapter);
        MenuList.setOnItemSelectedListener(new  OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {

                String classchosen = classes[position];
                Class ourClass;
                try {
                    ourClass = Class.forName("com.project.simplify." + classchosen);
                    Intent ourIntent = new Intent(MenuActivity.this, ourClass);
                      startActivity(ourIntent);
                } catch (ClassNotFoundException e) {
                    System.out.print("Class not found");
                    e.printStackTrace();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_bar, menu);
        inflater.inflate(R.menu.main, menu);

        return super.onCreateOptionsMenu(menu);
    }

}

我的MenuHelper课程:

public class MenuHelper {
    public int icon;
    public String title;
    public MenuHelper(){
        super();
    }

    public MenuHelper(int icon, String title) {
        super();
        this.icon = icon;
        this.title = title;
    }
}

还有MenuAdapter类:

public class MenuAdapter extends ArrayAdapter<MenuHelper>{

    Context context; 
    int layoutResourceId;    
    MenuHelper data[] = null;

    public MenuAdapter(Context context, int layoutResourceId, MenuHelper[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MenuHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MenuHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.menuItemIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.menuItemTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (MenuHolder)row.getTag();
        }

        MenuHelper menu = data[position];
        holder.txtTitle.setText(menu.title);
        holder.imgIcon.setImageResource(menu.icon);

        return row;
    }

    static class MenuHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

1 个答案:

答案 0 :(得分:0)

您应在onItemClickListener上设置MenuList,如下所示:

final ListView list = (ListView) findViewById(R.id.menuListView);
list.setAdapter(menuAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
      // Start your activities
   } 
});